i18n framework for HTML pages – jQuery

The problem is quite simple. You have HTML pages as your front end and you are not using any server side scripting to display your pages i.e. you are not using jsp or php or servlets to get HTML content. Application is HTML pages and javascript which is using Ajax to talk to back end and getting and sending data to server according to user actions and on certain events and Multi-lingual interface is the requirement i.e. require i18n for front-end.

As most of the static content is coded in your HTML pages, one way to do this is figuring out user’s context and display him specific HTML page accordingly. That means maintaining different HTML pages for each language you want to support for your web application. This approach is little difficult because generally we get changes very frequently on HTML pages so for maintaining it you also need to make changes in rest of HTML files for other languages. This requires testing for all languages as HTML files will also have some functional code with it and it may break for some languages while making changes.

We tried to solve this problem in slightly different manner. We tried to separate the display strings from HTML controls and tried to replace them with translations by figuring out the user’s language dynamically. In this way we have only one HTML page to maintain but we do have multiple resource files for language translations which keeps all static display strings translated for a certain language. As we need to do it on client side these resource files are basically Java script files having JSON objects with key and values. Keys are used to retrieve the display string value and replaced on HTML page. Following are the steps while loading the HTML page:

Question is how to replace display strings in HTML page. Here is the way you should write your HTML page so that you can replace strings according to the language.

<span id="mylabel.id" class="i18n"></span>
<input type="button" id="form.submit.button" name="form.submit.button" class="i18n"></input>
<select id="someSelect" class="i18n">
    <option id="select.option1" value="1"></option>
    <option id="select.option2" value="2"></option>
</select>
function translate() {
	if (_translator) {
		$("span.i18n").each( function (i) {
			$(this).html(_translator[this.id]);
		});
		$("input.i18n").each( function (i) {
			$(this).val(_translator[this.id]);
		});
		$("select.i18n").each( function (i) {
			var opts = $(this)[0].options;
			if (opts)
				for(var i=0; i<opts.length; i++) {
					opts[i].text = _translator[opts[i].id];
				}
		});
	}
}

You can see in the function above that class is used to for replacing translations in HTML file. Here _translator is the JSON object which is already loaded with translations as described earlier. Here is the example of translation file

{
  "key":"value",
  "mylabel.id":"My Label",
  "form.submit.button":"Submit",
  "select.option1":"Option 1",
  "select.option2":"Option 2"
}

For all dynamic Strings which are required while your getting success or error from server, you can use following function to get translated string from _translator object

function getI18N(key) {
        var s = '';
	if(_translator[key]) {
		s = _translator[key];
	}
       return s;
}

Please post comments if you find it can be improved. Thanks

Most Commented Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] i18n framework for HTML pages – jQuery [...]

ben faceye girmek istiyorum

ben facebooka girmek istiyorum

Leave a comment

(required)

(required)