jQuery i18n Translation Plugin

I’ve recently had to do some translation on the client-side using JavaScript, and wrote this plugin to do it (based heavily on javascript i18n that almost doesn’t suck by Marcus).

Download jquery.i18n.js.

To try it out, download the sample files , or check them out using subversion :

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

The index.html file should be fairly self-explanatory… before you do any translation you have to initialise the plugin with a ‘dictionary’ (basically a property list mapping keys to their translations).

var my_dictionary = { 
    "some text"  : "a translation",
    "some more text"  : "another translation"
}
$.i18n.setDictionary(my_dictionary);

Once you’ve initialised it with a dictionary, you can translate strings using the $.i18n._() function, for example :
$('div#example').text($.i18n._('some text'));

If you want a solution that’s a bit more global, Keith Wood has written a localisation plugin that looks very nice (i haven’t actually tried it).



9 Responses to “jQuery i18n Translation Plugin”  

  1. 1 strony internetowe wroc?aw

    very interesting site, many useful and helpful informations, really good item, thx so mutch

  2. 2 dave

    ^ thanks!

  3. 3 amber shop

    great post!!

  4. 4 Mike Nicholson

    Good work on this very simple jquery implementation of i18n. Often simple is best.

    I’m planning to use it on a Grails project I’m working on.

    Please continue to post cool stuff like this for us lazy developers :)

  5. 5 johan

    Nice work – this has helped me.

    I found your example code shown here does not work unless the dictionary is named “i18n_dict” – not “my_dictionary” – this seems to be due to name collision in your plugin in the setDictionary(i18n_dict) function. Changing the parameter name to somethign other than “i18n_dict” solves it:

    setDictionary: function(i18n_dictionary) {
    i18n_dict = i18n_dictionary;
    }

  6. 6 johan

    Version of your plug-in here solves the dictionary problem – scope to “this” – http://github.com/wynst/jquery-18n-translate/tree/master

  7. 7 ege madra

    Thanks for such a nice plugin. I used it in a significant project. However;

    setDictionary: function(i18n_dict) {
    i18n_dict = i18n_dict;
    },

    part of the code doesn’t work, it should be

    setDictionary: function(i18n_dict) {
    this.i18n_dict = i18n_dict;
    },

    Having changed this, I also needed to correct _ function accordingly, ie, by adding “this.” in front of i18n_dict references.

  8. 8 roses delivery

    well done, good job!

  1. 1 Auto Translate Website Interface using JQuery-i18n « wynst


Leave a Reply