This chapter provides a brief synopsis of the tools Dojo provides for internationalizing a module. The key topics are defining bundles on a locale basis and using Core's facilities for formatting and parsing dates, currency, and numbers. In case it wasn't quite obvious, internationalization is usually abbreviated as i18n simply because it is such a long word to type; thus, the shorthand is the first and last letters, with the number 18 between them to take place of the 18 letters in between.
If you have the good fortune of developing an application that becomes even mildly popular, you will certainly want to consider supporting more than one language. Although Dijit, which you'll learn about in Part II of this book, is already internationalized with several common locales, custom modules and widgets of your own devising will require some special attention. Fortunately, the toolkit provides techniques for supporting more than one language in a highly uniform way, saving you the headache of inventing your own system of mapping tokens back and forth; because Dojo manages how the loading takes place, you are also freed from thinking of ways to optimize the loading. Additional utilities also support common operations involving numeric formatting, currency, and more.
It's worth pointing out that the i18n facilities are technically
part of Core, not Base. XDomain builds; however, include the dojo.i18n
module as part of
dojo.xd.js at the expense of an extra 2KB to
workaround some tricky loading issues involving i18n bundles.
Regardless, you should still dojo.require("dojo.i18n")
into your page to
be explicit about your intention to use these facilities as a matter
of good form.
Internationalizing a module you've developed is simple because
the details of your string table stay compartmentalized inside of a
special nls
directory that appears
in your module directory with your JavaScript source files; nls
stands for native language
support. The nls
directory itself breaks down all of the various translations by
abbreviations for locales as defined in RFC 3066 (Tags for the
Identification of Languages).[14]
For example, the abbreviation for generic English is
en, the abbreviation for the dialect of English
as it is spoken in the United States is en-us,
and the abbreviation for generic Spanish is es. During the bootstrap process, Dojo
queries your browser for your particular locale, and stores it
internally. You can check it by typing dojo.locale
in Firebug. The value of
dojo.locale
is what Dojo uses to
determine the most appropriate translation when loading a module that
has been internationalized.
Get Dojo: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.