Over a million developers have joined DZone.

Internationalization Using jquery.i18n.properties.js

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Internationalization refers to automatically showing localized text in your pages for users visiting your site from different regions in the world or localizing the site content based on the language preference chosen by the user.

These days most of the web applications are designed to provide rich user experience. With this, the use of JavaScript based UI components has increased many folds. We often need to support internationalization on these JavaScript based Rich Internet Applications (RIA). While looking for JavaScript based internationalization solution I came across a very good jQuery plugin jquery.i18n.properties.js. This plugin uses .properties files to localize the content into different languages. In this tutorial I will show you how we can use this plugin.

Getting jquery.i18n.properties.js

First of all we need to download the plugin. This is quite lightweight plugin. The file size is
around 17.4 KB, but this can be minified and size will reduce to around 4.3 KB.

The plugin can be downloaded from https://github.com/jquery-i18n-properties/jquery-i18n-properties.

A minified version of the same is available at http://code.google.com/p/jquery-i18n-properties/downloads/list

Internationalization Demo

The first step as with all JavaScript libraries is to include the JavaScript into HTML. Jquery.i18n.properties.js is a jQuery plugin; hence we need to include jQuery also into HTML before jquery.i18n.properties.js like shown below:

        <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="js/jquery.i18n.properties.js"></script>

Sample HTML Code

Before discussing on how to use jquery.i18n.properties.js, let us first create a sample HTML that we will use later. The sample HTML below has a dropdown which allows the user to choose a language. The sample HTML displays two messages which would be localized based on the language chosen from the dropdown.

        <h2 style="text-align: center; color:#777;">Internationalization using jQuery.i18n.properties</h2>

        <div style="margin-left: 20px; margin-bottom: 20px;" id="langBox">
            <select id="lang">
                <option value="browser" selected>Browser Default</option>
                <option value="en">en</option>
                <option value="de_DE">de_DE</option>
                <option value="es_ES">es_ES</option>
                <option value="fr">fr</option>

        <div style="margin-left: 20px;" id="msg_welcome">Welcome to the Demo Site!</div><br>
        <div style="margin-left: 20px;" id="msg_selLang">Your Selected Language is: Default </div>

Define .properties files

The jquery.i18n.properties.js plugin consumes .properties files for doing text translations. We will use the following .properties files in this demo.

msg_welcome = Welcome to the Demo Site!
msg_selLang = Your Selected Language is: {0}

msg_welcome = Bienvenido al sitio de demostración!
msg_selLang = El idioma seleccionado es: {0}

Loading localized strings from .properties

Now we have everything ready to use the plugin, let us see how we can use this plugin to load the translated strings from properties files. The below code sample is used to load the resource bundle properties file using jquery.i18n.properties.js

name: 'Messages', 
path: 'bundle/', 
mode: 'both', 
language: lang, 
callback: function() { 
$("#msg_welcome").text($.i18n.prop('msg_welcome')); $("#msg_selLang").text($.i18n.prop('msg_selLang', lang)); 

The below table provides details about the various options available for $.i18n.properties() (source: http://codingwithcoffee.com/?p=272)

Option Description Notes 
name Name (or names) of files representing resource bundles
(eg, ‘Messages’ or ['Msg1','Msg2'])
String or String[]
ISO-639 Language code and, optionally, ISO-3166
country code (eg, ‘en’, ‘en_US’, ‘pt_PT’). If not specified, language reported by the browser will be used instead.
pathPath to directory that contains ‘.properties‘files
to load.


Option to have resource bundle keys available as JavaScript
variables/functions OR as a map. Possible options: ‘vars’ (default), ‘map’ or ‘both’.
Callback function to be called upon script execution

Here mode is set to ‘both’ hence the messages can be fetched using map approach as well as JavaScript variables/functions. In the above code sample we used map to retrieve the translated text. The same can be achieved using JavaScript variables/functions as shown below:


String parameterization

Jquery.i18n.properties.js also supports parameterization of messages. This we have already used in the sample above for the second message.

$("#msg_selLang").text($.i18n.prop('msg_selLang', lang));

In the properties file the message is defined as

msg_selLang = Your Selected Language is: {0}

Here {0} is replaced by the argument ‘lang’ value. As in java resource bundles, we can use multiple {} to define custom messages with multiple parameters.

The final output

The following screen shots show the output of this demo. The below screen shot is of the default page

When the language in drop down is changed to es_ES the text from Message_es_ES.properties is read and displayed as shown below:

Advantages of jquery.i18n.properties.js

  • The main advantage of this plugin is that it uses .properties files for internationalization. This is helpful as same properties files could be shared with other parts of the program.
  • The support of parameterization of strings is also beneficial as this enables one to have     complex multilingual strings.
  • Has option to use map as well as JavaScript variables/functions for retrieving translated     strings.
  • The plugin is very lightweight and can be easily used with any HTML as it is jQuery based.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

css,javascript,html5,web dev

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}