July 4, 2006

Localization and JavaScript : best practices?

How do you do localization in a language which doesn’t allow you to import files?

The best possible way to do this would probably be to not keep any strings within your javascript files. By pushing all strings to the JSP/CGI layer, you allow a higher level language with internationalization support to take care of localization for you. However, that isn’t always possible and I’m going to talk about what to do in situations where you have to do this.

IBM has a nice page on internationalization and JavaScript that describes their suggestion : multiple versions of a file for each language. For the project I’ve been working on, I found this the best way to handle things. What I ended up doing was tokenizing all of the strings so that:

alert("Hello world");

became

alert ("@hello.message@");

Then, using ant and properties files, I was able to do token replacement to replace all tokens with corresponding key/value pairs from a properties file resource bundle. This allowed me to keep all of my internationalized strings in .properties files just like the rest of the application.

<loadfile srcfile="${src.file}" property="${src.file.replaced}">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<param type="propertiesfile" value="sample.properties"/>
</filterreader>
</filterchain>
</loadfile>
<filterchain>

With multiple languages, the above probably isn’t optimal - I used ant macrodefs to define a macro for doing the replacement, and then just executed the macro with the properties file name and the locale for each language.

What is required here is a patch to ant that is going into Ant 1.7. Ant already does property/key replacement, so the patch was pretty easy. All of this is being done at build time, so there’s no performance hit, and only one version of the file (the one with the tokenized strings) needs to be maintained.

Another strategy that I thought of was to do an xmlhttp request for each string. I decided not to use this approach because it seemed like it was expensive - an extra request per page at least.

April 13, 2006

Internet Explorer / Eolas Update Causes Confusing Bugs

February 2, 2006

Logging Abandoned Connections - possible with local-tx-datasource?

January 29, 2006

Maven vs. Ivy smackdown

January 19, 2006

Shale Investigations