Skip to main content

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: Tips4allCCNA FINAL EXAM

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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex