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
- The directory structure containing the nls directory
- The name of the file in that directory containing the localized resources (omitting the
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
getLocalization()method, individual values can be accessed directly using dot notation. Suppose the language bundle for "en_ca" strings in the
gravelleconsulting.jsfile 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).
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).
- More information on djConfig
- Dojo Translatable Resource Bundle Reference Guide
- Globalization Guidelines
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