0

I've run into a problem with internationalization and need to know the best way to move forward. we use java ResourceBundle with .properties files to internationalize the content on our software. There are areas where a dynamic value gets passed to a JavaScript alert, for example

#.properties file with ca_ES locale
warningText=regles d’apostrofació

#JavaScript with the property as parameter
<script>
    alert('warningText');
</script>

So the apostrophe in the value for warningText escapes the string and causes a fatal error. We can't really use double quotes instead because of our coding standards, and it would be a ton of work to go through and change all of the code to double quotes, and we can't use something like &quot; because html tags don't resolve inside a javascript alert.

Lastly if within the string in the properties file we did something like this:

warningText=regles d\’apostrofació

the escape is applied during compilation or in the JVM or somewhere prior to the javascript and we end up just escaping the string anyways

Adam
  • 85
  • 1
  • 8
  • Can you double up the escape char? i.e. warningText=regles d\\’apostrofació – James Oltmans Sep 02 '15 at 23:29
  • *"We can't really use double quotes instead because of our coding standards"* - If using double quotes solves the problem then you need to change (or make an exception to) your coding standards - no point sticking religiously to standards that stop your code working. However, the best solution is just to escape those characters. – nnnnnn Sep 02 '15 at 23:31
  • You may want to look at this: https://jawr.java.net/docs/messages_gen.html – James Oltmans Sep 02 '15 at 23:33

1 Answers1

1

You may simply use unicodes \uxxxx instead of unicode characters into properties file which are also supported by browsers, below are the unicodes for regles d’apostrofació:

#warningText=regles d’apostrofació
warningText=\u0072\u0065\u0067\u006c\u0065\u0073\u0020\u0064\u2019\u0061\u0070\u006f\u0073\u0074\u0072\u006f\u0066\u0061\u0063\u0069\u00f3

alert('\u0072\u0065\u0067\u006c\u0065\u0073\u0020\u0064\u2019\u0061\u0070\u006f\u0073\u0074\u0072\u006f\u0066\u0061\u0063\u0069\u00f3')
  • @JamesOltmans, IMHO its a good practice to keep property values as unicodes, BTW those are messages not logic, however you can still have comments in properties file using `#` as a side note –  Sep 02 '15 at 23:54
  • Managing that by hand is a nightmare. Yes properties files must be ISO 8859-1 compatible but make sure you're using an IDE plugin to manage it: http://stackoverflow.com/questions/863838/java-properties-utf-8-encoding-in-eclipse – James Oltmans Sep 03 '15 at 00:32