views:

3674

answers:

3

For server side generated content, i18n support is usually pretty easy, for example, Java provides extensive i18n support.

But, these rich server side libraries are not available within the browser, which causes a problem when trying to format data client side (AJAX style.)

What javascript libraries and techniques are recommended for performing client side formatting and time-zone calculations? Also - beyond simple client side formatting, how can consistency be achieved when performing both server side and client side formatting?

+3  A: 

This is a complicated topic and no simple answer will probably suffice.

For formatting (numbers, money fields, dates and times) I usually pass datatype information along with the JSON data. One possibility is to follow the conventions used by Exhibit in MIT's SIMILE project. This allows the data to be processed further on the client, if necessary. Formatting is then applied in javascript.

I have not needed to do time zone conversions on the client. If your time zones are specific to users or sessions (and you do not need to display several time zones on the same page at the same time), you might find it easier to do time zone conversions on the server, and send the dates and times in ISO 8601 format with time zone difference information attached. That way you can still use the times for calculations before display.

If you have a limited list of status and error messages, you might let the server localize a "hidden" list into your HTML, and then use AJAX data to select the correct message to display (rather than delivering a localized message via AJAX).

Eero
I've done something similar. Store the localization configuration server-side and send it down as a JS object that can be used to figure out how to display dates, times, money, etc.
Ryan Doherty
+2  A: 

Dojo toolkit provides advanced tools for localizing web applications, plus also a wide range of widgets that are i18n out of the box. As an example of i18n widget, please have a look at date textbox widget test page. In general an i18n widget should load locale from your browser, but you can force a certain locale by passing locale config param:

<script type="text/javascript" src="../../../dojo/dojo.js"
  djConfig="isDebug: true, parseOnLoad: true, locale:'de'"></script>

And internationalizing any widget is also simple - I'll use data grid widget test as an example. First, you require a library by adding dojo.require("dojo.number"); among other requires. Then you'll add a formatter for a column containing numbers:

{name: 'Column 5', field: 'col5', formatter: dojo.number.format },

That's it - your widgets are internationalized for the whole globe thanks to Unicode Common Locale Data Repository included in the Dojo toolkit. For translating your own web apps, you can use Dojo's Translatable Resource bundles.

Maine
+1  A: 

I'm also looking for a package like that. Found a small jquery plugin i18n

This solves part of a problem and includes a simple handing of placeholder, dates and numbers.

http://svn.recurser.com/misc/jquery/i18n/jquery.i18n.js

<script>
    i18n_dict = {'how old':'Ich bin %s Jahre alt'};
    function test_i18n(){
        alert($.i18n._('how old','5');
    }
</script>
<a onclick="test_i18n()">clickme</a>

Will return "Ich bin 5 Jahre alt". Parameters have to be strings though.

Would be nice if plugin handled strings with named parameters like "Ich bin %(age)s Jahre alt", then it would allow change order of parameters in the translation.

Evgeny
@John Vasileff. I'd add "proper handing of placeholders" to your question.
Evgeny