]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/i18n.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / i18n.js
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");
4
5 /*=====
6 dojo.i18n = {
7         // summary: Utility classes to enable loading of resources for internationalization (i18n)
8 };
9 =====*/
10
11 dojo.i18n.getLocalization = function(/*String*/packageName, /*String*/bundleName, /*String?*/locale){
12         //      summary:
13         //              Returns an Object containing the localization for a given resource
14         //              bundle in a package, matching the specified locale.
15         //      description:
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.
26         //      packageName:
27         //              package which is associated with this resource
28         //      bundleName:
29         //              the base filename of the resource bundle (without the ".js" suffix)
30         //      locale:
31         //              the variant to load (optional).  By default, the locale defined by
32         //              the host environment: dojo.locale
33
34         locale = dojo.i18n.normalizeLocale(locale);
35
36         // look for nearest locale match
37         var elements = locale.split('-');
38         var module = [packageName,"nls",bundleName].join('.');
39         var bundle = dojo._loadedModules[module];
40         if(bundle){
41                 var localization;
42                 for(var i = elements.length; i > 0; i--){
43                         var loc = elements.slice(0, i).join('_');
44                         if(bundle[loc]){
45                                 localization = bundle[loc];
46                                 break;
47                         }
48                 }
49                 if(!localization){
50                         localization = bundle.ROOT;
51                 }
52
53                 // make a singleton prototype so that the caller won't accidentally change the values globally
54                 if(localization){
55                         var clazz = function(){};
56                         clazz.prototype = localization;
57                         return new clazz(); // Object
58                 }
59         }
60
61         throw new Error("Bundle not found: " + bundleName + " in " + packageName+" , locale=" + locale);
62 };
63
64 dojo.i18n.normalizeLocale = function(/*String?*/locale){
65         //      summary:
66         //              Returns canonical form of locale, as used by Dojo.
67         //
68         //  description:
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.
72
73         var result = locale ? locale.toLowerCase() : dojo.locale;
74         if(result == "root"){
75                 result = "ROOT";
76         }
77         return result; // String
78 };
79
80 dojo.i18n._requireLocalization = function(/*String*/moduleName, /*String*/bundleName, /*String?*/locale, /*String?*/availableFlatLocales){
81         //      summary:
82         //              See dojo.requireLocalization()
83         //      description:
84         //              Called by the bootstrap, but factored out so that it is only
85         //              included in the build when needed.
86
87         var targetLocale = dojo.i18n.normalizeLocale(locale);
88         var bundlePackage = [moduleName, "nls", bundleName].join(".");
89         // NOTE: 
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.
99         
100         //Find the best-match locale to load if we have available flat locales.
101         var bestLocale = "";
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];
109                                 }
110                         }
111                 }
112                 if(!bestLocale){
113                         bestLocale = "ROOT";
114                 }               
115         }
116
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;
121         if(bundle){
122                 if(dojo.config.localizationComplete && bundle._built){return;}
123                 var jsLoc = tempLocale.replace(/-/g, '_');
124                 var translationPackage = bundlePackage+"."+jsLoc;
125                 localizedBundle = dojo._loadedModules[translationPackage];
126         }
127
128         if(!localizedBundle){
129                 bundle = dojo["provide"](bundlePackage);
130                 var syms = dojo._getModuleSymbols(moduleName);
131                 var modpath = syms.concat("nls").join("/");
132                 var parent;
133
134                 dojo.i18n._searchLocalePath(tempLocale, availableFlatLocales, function(loc){
135                         var jsLoc = loc.replace(/-/g, '_');
136                         var translationPackage = bundlePackage + "." + jsLoc;
137                         var loaded = false;
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]; }
151                                 });
152                         }else{
153                                 loaded = true;
154                         }
155                         if(loaded && bundle[jsLoc]){
156                                 parent = bundle[jsLoc];
157                         }else{
158                                 bundle[jsLoc] = parent;
159                         }
160                         
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.
164                                 return true;
165                         }
166                 });
167         }
168
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, '_')];
173         }
174 };
175
176 (function(){
177         // If other locales are used, dojo.requireLocalization should load them as
178         // well, by default. 
179         // 
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.
183
184         var extra = dojo.config.extraLocale;
185         if(extra){
186                 if(!extra instanceof Array){
187                         extra = [extra];
188                 }
189
190                 var req = dojo.i18n._requireLocalization;
191                 dojo.i18n._requireLocalization = function(m, b, locale, availableFlatLocales){
192                         req(m,b,locale, availableFlatLocales);
193                         if(locale){return;}
194                         for(var i=0; i<extra.length; i++){
195                                 req(m,b,extra[i], availableFlatLocales);
196                         }
197                 };
198         }
199 })();
200
201 dojo.i18n._searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){
202         //      summary:
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".
207
208         locale = dojo.i18n.normalizeLocale(locale);
209
210         var elements = locale.split('-');
211         var searchlist = [];
212         for(var i = elements.length; i > 0; i--){
213                 searchlist.push(elements.slice(0, i).join('-'));
214         }
215         searchlist.push(false);
216         if(down){searchlist.reverse();}
217
218         for(var j = searchlist.length - 1; j >= 0; j--){
219                 var loc = searchlist[j] || "ROOT";
220                 var stop = searchFunc(loc);
221                 if(stop){ break; }
222         }
223 };
224
225 dojo.i18n._preloadLocalizations = function(/*String*/bundlePrefix, /*Array*/localesGenerated){
226         //      summary:
227         //              Load built, flattened resource bundles, if available for all
228         //              locales used in the page. Only called by built layer files.
229
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
237                                 }
238                         }
239                         return false; // Boolean
240                 });
241         }
242         preload();
243         var extra = dojo.config.extraLocale||[];
244         for(var i=0; i<extra.length; i++){
245                 preload(extra[i]);
246         }
247 };
248
249 }