Sunday, May 20, 2012

Best JavaScript i18n techniques / Ajax - dates, times, numbers, currency


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?


Source: Tips4all

5 comments:

  1. Using i18next might be a good option:

    http://jamuhl.github.com/i18next/

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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).

    ReplyDelete
  4. 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.

    ReplyDelete
  5. If you are using Java, check out JAWR http://jawr.java.net - it offers a very nice way to handle JavaScript internationalization.

    ReplyDelete