Multilingual Website Interface on the Fly With jQuery

Applying a multilingual interface on your webpages and allowing the change at the client side without the need for a server postback has never been easier using the technique I’ll describe in this post.
While developing Bloginto, I came across the situation of implementing an English-Arabic-French interface with the possibility to change the language on the fly (on the client side), so I used a trick so close to culture resource files in the .NET.
My first priority was to write the minimum of code using jQuery, and as you will see, it is *really* minimal.

1. Preparing the resources for the language

We will encapsulate the language resources with Javascript arrays, here is an example:
  1. function getLanguageResources() {
  2.     var fr = new Array(); var en = new Array();
  4.     fr['settings'] = "paramètres"; en['settings'] = "settings";
  5.     fr['default_feed'] = "Flux par défaut"; en['default_feed'] = "Default feed";
  6.     fr['hidden'] = "Masquer"; en['hidden'] = " Hidden";
  7.     fr['save_settings'] = "Enregistrer les paramètres"; en['save_settings'] = "Save settings";
  9.     var resources = new Array();
  10.     resources['fr'] = fr;
  11.     resources['en'] = en;
  13.     return resources;
  14. }
The getLanguageResources function returns an array that contains an associative key/value arrays of the desirable languages, notice how the same key is used for the different translations.

2. Preparing the HTML markup

We need to put placeholders for the multilingual text to show in the markup, I choose a <span> tag here, but obviously you can use whatever elements fits best. The trick is to have all these multilingual spans use the same name attribute (this is not a necessity neither, you can use whatever attribute you wish), and have them use another attribute, for example caption which value is the key of the text to show in the resource arrays.
We will also add two buttons to test the language change, here is the final markup :
  1. <input type="radio" id="radioEnglish" name="radio-language" value="en"/><label for="radioEnglish">English</label>
  2. <input type="radio" id="radioFrench" name="radio-language" value="fr"/><label for="radioFrench">Français</label><br/>
  4. Text for : settings : <b><span name="lbl" caption="settings"></span></b><br/>
  5. Text for : default_feed : <b><span name="lbl" caption="default_feed"></span></b><br/>
  6. Text for : hidden : <b><span name="lbl" caption="hidden"></span></b><br/>
  7. Text for : save_settings : <b><span name="lbl" caption="save_settings"></span></b><br/>
Notice how the radio buttons for changing the language have values which are the keys of the resource language (the arrays).

3. jQuery magic

Now we will apply some jQuery code to associate the spans with their corresponding text from the language selected by the radio buttons
  1. function changeLanguage(lang) {
  2.     var langResources = getLanguageResources()[lang];
  4.     $("span[name='lbl']").each(function (i, elt) {
  5.         $(elt).text(langResources[$(elt).attr("caption")]);
  6.     });
  7. }
  9. $(document).ready(function () {
  10.     $("input[name='radio-language']").click(function () {
  11.         changeLanguage($(this).val());
  12.     });
  13. });
Here we attach a click event to the radio buttons, and we simply call the function changeLanguage whenever a button is clicked, we pass the value of the “value” attribute of that button to the function which represents the specified language (‘fr’ or ‘en’)
The changeLanguage function loads the corresponding array of the languages from the getLanguageResources function, then iterate through every element that has the attribute namelbl”, and change its text to the value in the resource language array which key is the caption of that specified element (span), pretty simple!
As you can see, all the magic is done with 2 lines of jQuery, if you guessed that the “.attr()” method will do the same instead of “.each()” then you’ve guessed wrong, attr() applies only to the first element in the selection set. See .attr() specification here.

4. Demo

Her is a working demo

Posted in , . Bookmark the permalink. RSS feed for this post.

comments powered by Disqus

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by