1 if(!dojo._hasResource["dojo.i18n"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.i18n"] = true;
3 dojo.provide("dojo.i18n");
7 // summary: Utility classes to enable loading of resources for internationalization (i18n)
11 dojo.i18n.getLocalization = function(/*String*/packageName, /*String*/bundleName, /*String?*/locale){
13 // Returns an Object containing the localization for a given resource
14 // bundle in a package, matching the specified locale.
16 // Returns a hash containing name/value pairs in its prototypesuch
17 // that values can be easily overridden. Throws an exception if the
18 // bundle is not found. Bundle must have already been loaded by
19 // `dojo.requireLocalization()` or by a build optimization step. NOTE:
20 // try not to call this method as part of an object property
21 // definition (`var foo = { bar: dojo.i18n.getLocalization() }`). In
22 // some loading situations, the bundle may not be available in time
23 // for the object definition. Instead, call this method inside a
24 // function that is run after all modules load or the page loads (like
25 // in `dojo.addOnLoad()`), or in a widget lifecycle method.
27 // package which is associated with this resource
29 // the base filename of the resource bundle (without the ".js" suffix)
31 // the variant to load (optional). By default, the locale defined by
32 // the host environment: dojo.locale
34 locale = dojo.i18n.normalizeLocale(locale);
36 // look for nearest locale match
37 var elements = locale.split('-');
38 var module = [packageName,"nls",bundleName].join('.');
39 var bundle = dojo._loadedModules[module];
42 for(var i = elements.length; i > 0; i--){
43 var loc = elements.slice(0, i).join('_');
45 localization = bundle[loc];
50 localization = bundle.ROOT;
53 // make a singleton prototype so that the caller won't accidentally change the values globally
55 var clazz = function(){};
56 clazz.prototype = localization;
57 return new clazz(); // Object
61 throw new Error("Bundle not found: " + bundleName + " in " + packageName+" , locale=" + locale);
64 dojo.i18n.normalizeLocale = function(/*String?*/locale){
66 // Returns canonical form of locale, as used by Dojo.
69 // All variants are case-insensitive and are separated by '-' as specified in [RFC 3066](http://www.ietf.org/rfc/rfc3066.txt).
70 // If no locale is specified, the dojo.locale is returned. dojo.locale is defined by
71 // the user agent's locale unless overridden by djConfig.
73 var result = locale ? locale.toLowerCase() : dojo.locale;
77 return result; // String
80 dojo.i18n._requireLocalization = function(/*String*/moduleName, /*String*/bundleName, /*String?*/locale, /*String?*/availableFlatLocales){
82 // See dojo.requireLocalization()
84 // Called by the bootstrap, but factored out so that it is only
85 // included in the build when needed.
87 var targetLocale = dojo.i18n.normalizeLocale(locale);
88 var bundlePackage = [moduleName, "nls", bundleName].join(".");
90 // When loading these resources, the packaging does not match what is
91 // on disk. This is an implementation detail, as this is just a
92 // private data structure to hold the loaded resources. e.g.
93 // `tests/hello/nls/en-us/salutations.js` is loaded as the object
94 // `tests.hello.nls.salutations.en_us={...}` The structure on disk is
95 // intended to be most convenient for developers and translators, but
96 // in memory it is more logical and efficient to store in a different
97 // order. Locales cannot use dashes, since the resulting path will
98 // not evaluate as valid JS, so we translate them to underscores.
100 //Find the best-match locale to load if we have available flat locales.
102 if(availableFlatLocales){
103 var flatLocales = availableFlatLocales.split(",");
104 for(var i = 0; i < flatLocales.length; i++){
105 //Locale must match from start of string.
106 if(targetLocale.indexOf(flatLocales[i]) == 0){
107 if(flatLocales[i].length > bestLocale.length){
108 bestLocale = flatLocales[i];
117 //See if the desired locale is already loaded.
118 var tempLocale = availableFlatLocales ? bestLocale : targetLocale;
119 var bundle = dojo._loadedModules[bundlePackage];
120 var localizedBundle = null;
122 if(dojo.config.localizationComplete && bundle._built){return;}
123 var jsLoc = tempLocale.replace(/-/g, '_');
124 var translationPackage = bundlePackage+"."+jsLoc;
125 localizedBundle = dojo._loadedModules[translationPackage];
128 if(!localizedBundle){
129 bundle = dojo["provide"](bundlePackage);
130 var syms = dojo._getModuleSymbols(moduleName);
131 var modpath = syms.concat("nls").join("/");
134 dojo.i18n._searchLocalePath(tempLocale, availableFlatLocales, function(loc){
135 var jsLoc = loc.replace(/-/g, '_');
136 var translationPackage = bundlePackage + "." + jsLoc;
138 if(!dojo._loadedModules[translationPackage]){
139 // Mark loaded whether it's found or not, so that further load attempts will not be made
140 dojo["provide"](translationPackage);
141 var module = [modpath];
142 if(loc != "ROOT"){module.push(loc);}
143 module.push(bundleName);
144 var filespec = module.join("/") + '.js';
145 loaded = dojo._loadPath(filespec, null, function(hash){
146 // Use singleton with prototype to point to parent bundle, then mix-in result from loadPath
147 var clazz = function(){};
148 clazz.prototype = parent;
149 bundle[jsLoc] = new clazz();
150 for(var j in hash){ bundle[jsLoc][j] = hash[j]; }
155 if(loaded && bundle[jsLoc]){
156 parent = bundle[jsLoc];
158 bundle[jsLoc] = parent;
161 if(availableFlatLocales){
162 //Stop the locale path searching if we know the availableFlatLocales, since
163 //the first call to this function will load the only bundle that is needed.
169 //Save the best locale bundle as the target locale bundle when we know the
170 //the available bundles.
171 if(availableFlatLocales && targetLocale != bestLocale){
172 bundle[targetLocale.replace(/-/g, '_')] = bundle[bestLocale.replace(/-/g, '_')];
177 // If other locales are used, dojo.requireLocalization should load them as
180 // Override dojo.requireLocalization to do load the default bundle, then
181 // iterate through the extraLocale list and load those translations as
182 // well, unless a particular locale was requested.
184 var extra = dojo.config.extraLocale;
186 if(!extra instanceof Array){
190 var req = dojo.i18n._requireLocalization;
191 dojo.i18n._requireLocalization = function(m, b, locale, availableFlatLocales){
192 req(m,b,locale, availableFlatLocales);
194 for(var i=0; i<extra.length; i++){
195 req(m,b,extra[i], availableFlatLocales);
201 dojo.i18n._searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){
203 // A helper method to assist in searching for locale-based resources.
204 // Will iterate through the variants of a particular locale, either up
205 // or down, executing a callback function. For example, "en-us" and
206 // true will try "en-us" followed by "en" and finally "ROOT".
208 locale = dojo.i18n.normalizeLocale(locale);
210 var elements = locale.split('-');
212 for(var i = elements.length; i > 0; i--){
213 searchlist.push(elements.slice(0, i).join('-'));
215 searchlist.push(false);
216 if(down){searchlist.reverse();}
218 for(var j = searchlist.length - 1; j >= 0; j--){
219 var loc = searchlist[j] || "ROOT";
220 var stop = searchFunc(loc);
225 dojo.i18n._preloadLocalizations = function(/*String*/bundlePrefix, /*Array*/localesGenerated){
227 // Load built, flattened resource bundles, if available for all
228 // locales used in the page. Only called by built layer files.
230 function preload(locale){
231 locale = dojo.i18n.normalizeLocale(locale);
232 dojo.i18n._searchLocalePath(locale, true, function(loc){
233 for(var i=0; i<localesGenerated.length;i++){
234 if(localesGenerated[i] == loc){
235 dojo["require"](bundlePrefix+"_"+loc);
236 return true; // Boolean
239 return false; // Boolean
243 var extra = dojo.config.extraLocale||[];
244 for(var i=0; i<extra.length; i++){