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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.