Dojo i18n: A Pure JavaScript Localization Solution | 2 | WebReference

Dojo i18n: A Pure JavaScript Localization Solution | 2


Dojo i18n: a Pure JavaScript Localization Solution [con't]

Accessing Localized Content

Before we can use localized strings, we have to use a special kind of require() function called requireLocalization(). It declares usage of localized resources and loads them in the same way that dojo.requires() pulls in JavaScript packages, but using the translation appropriate to the caller. The location of the bundle is specified using two arguments:

  1. The directory structure containing the nls directory
  2. The name of the file in that directory containing the localized resources (omitting the .js extension)

Ours points to the AppRoot module path:

Obtaining a reference to a bundle is then a matter of calling the getLocalization() method. It returns an object of name/value pairs representing the localized resources. We need to pass it the packageName, which is the package associated with this resource, and the bundleName, which is the base filename of the resource bundle (without the ".js" suffix). It throws an exception if the bundle is not found, so enclosing it within a try/catch block is recommended:

Reading Individual Values from the Localized Bundle Object

Recall that the bundle format is that of a JavaScript object literal. Once assigned to a variable using the getLocalization() method, individual values can be accessed directly using dot notation. Suppose the language bundle for "en_ca" strings in the gravelleconsulting.js file contains the following:

To read in the value for the page_title key, we would refer to it using nlsStrings.page_title. Here's some code that reads in some of our values above and calls the dojo.string.substitute() method to create a greeting:

Because we assigned "en_ca" to the djConfig variable to be the default locale, we can see the Canadian spelling of "neighbour" in the greeting: (see Figure 2).

Figure 2: Greeting Using en_ca Locale

The getLocalization() method accepts an optional third parameter for setting the locale to something other than the default. We can use it to fetch the "en_us" labels:

Thus displaying an American greeting (see Figure 3).

Figure 3: Greeting Using en_us Locale

For those of you who require localization services for small to medium applications, I think that you'll find Dojo's pure JavaScript i18n module to be an excellent solution. It's lightweight, easy to use, and works very well. While the sample in this article was quite simple, there's a lot more you can do. If you'd like to learn more about the Dojo Toolkit, some useful references follow.


Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .

Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest release is called "The Rabbit of Seville". Loosely based on Rossini's The Barber of Seville overture, Rob's amazing rendition includes a full orchestra and numerous guitar tracks. It is sold on his site as a high bitrate MP3 for only $0.99 cents! Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: September 28, 2010