2150 |
mathias |
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 |
dojo.i18n.getLocalization = function(/*String*/packageName, /*String*/bundleName, /*String?*/locale){
|
|
|
6 |
// summary:
|
|
|
7 |
// Returns an Object containing the localization for a given resource
|
|
|
8 |
// bundle in a package, matching the specified locale.
|
|
|
9 |
// description:
|
|
|
10 |
// Returns a hash containing name/value pairs in its prototypesuch
|
|
|
11 |
// that values can be easily overridden. Throws an exception if the
|
|
|
12 |
// bundle is not found. Bundle must have already been loaded by
|
|
|
13 |
// dojo.requireLocalization() or by a build optimization step. NOTE:
|
|
|
14 |
// try not to call this method as part of an object property
|
|
|
15 |
// definition (var foo = { bar: dojo.i18n.getLocalization() }). In
|
|
|
16 |
// some loading situations, the bundle may not be available in time
|
|
|
17 |
// for the object definition. Instead, call this method inside a
|
|
|
18 |
// function that is run after all modules load or the page loads (like
|
|
|
19 |
// in dojo.adOnLoad()), or in a widget lifecycle method.
|
|
|
20 |
// packageName:
|
|
|
21 |
// package which is associated with this resource
|
|
|
22 |
// bundleName:
|
|
|
23 |
// the base filename of the resource bundle (without the ".js" suffix)
|
|
|
24 |
// locale:
|
|
|
25 |
// the variant to load (optional). By default, the locale defined by
|
|
|
26 |
// the host environment: dojo.locale
|
|
|
27 |
|
|
|
28 |
locale = dojo.i18n.normalizeLocale(locale);
|
|
|
29 |
|
|
|
30 |
// look for nearest locale match
|
|
|
31 |
var elements = locale.split('-');
|
|
|
32 |
var module = [packageName,"nls",bundleName].join('.');
|
|
|
33 |
var bundle = dojo._loadedModules[module];
|
|
|
34 |
if(bundle){
|
|
|
35 |
var localization;
|
|
|
36 |
for(var i = elements.length; i > 0; i--){
|
|
|
37 |
var loc = elements.slice(0, i).join('_');
|
|
|
38 |
if(bundle[loc]){
|
|
|
39 |
localization = bundle[loc];
|
|
|
40 |
break;
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
if(!localization){
|
|
|
44 |
localization = bundle.ROOT;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
// make a singleton prototype so that the caller won't accidentally change the values globally
|
|
|
48 |
if(localization){
|
|
|
49 |
var clazz = function(){};
|
|
|
50 |
clazz.prototype = localization;
|
|
|
51 |
return new clazz(); // Object
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
throw new Error("Bundle not found: " + bundleName + " in " + packageName+" , locale=" + locale);
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
dojo.i18n.normalizeLocale = function(/*String?*/locale){
|
|
|
59 |
// summary:
|
|
|
60 |
// Returns canonical form of locale, as used by Dojo.
|
|
|
61 |
//
|
|
|
62 |
// description:
|
|
|
63 |
// All variants are case-insensitive and are separated by '-' as specified in RFC 3066.
|
|
|
64 |
// If no locale is specified, the dojo.locale is returned. dojo.locale is defined by
|
|
|
65 |
// the user agent's locale unless overridden by djConfig.
|
|
|
66 |
|
|
|
67 |
var result = locale ? locale.toLowerCase() : dojo.locale;
|
|
|
68 |
if(result == "root"){
|
|
|
69 |
result = "ROOT";
|
|
|
70 |
}
|
|
|
71 |
return result; // String
|
|
|
72 |
};
|
|
|
73 |
|
|
|
74 |
dojo.i18n._requireLocalization = function(/*String*/moduleName, /*String*/bundleName, /*String?*/locale, /*String?*/availableFlatLocales){
|
|
|
75 |
// summary:
|
|
|
76 |
// See dojo.requireLocalization()
|
|
|
77 |
// description:
|
|
|
78 |
// Called by the bootstrap, but factored out so that it is only
|
|
|
79 |
// included in the build when needed.
|
|
|
80 |
|
|
|
81 |
var targetLocale = dojo.i18n.normalizeLocale(locale);
|
|
|
82 |
var bundlePackage = [moduleName, "nls", bundleName].join(".");
|
|
|
83 |
// NOTE:
|
|
|
84 |
// When loading these resources, the packaging does not match what is
|
|
|
85 |
// on disk. This is an implementation detail, as this is just a
|
|
|
86 |
// private data structure to hold the loaded resources. e.g.
|
|
|
87 |
// tests/hello/nls/en-us/salutations.js is loaded as the object
|
|
|
88 |
// tests.hello.nls.salutations.en_us={...} The structure on disk is
|
|
|
89 |
// intended to be most convenient for developers and translators, but
|
|
|
90 |
// in memory it is more logical and efficient to store in a different
|
|
|
91 |
// order. Locales cannot use dashes, since the resulting path will
|
|
|
92 |
// not evaluate as valid JS, so we translate them to underscores.
|
|
|
93 |
|
|
|
94 |
//Find the best-match locale to load if we have available flat locales.
|
|
|
95 |
var bestLocale = "";
|
|
|
96 |
if(availableFlatLocales){
|
|
|
97 |
var flatLocales = availableFlatLocales.split(",");
|
|
|
98 |
for(var i = 0; i < flatLocales.length; i++){
|
|
|
99 |
//Locale must match from start of string.
|
|
|
100 |
if(targetLocale.indexOf(flatLocales[i]) == 0){
|
|
|
101 |
if(flatLocales[i].length > bestLocale.length){
|
|
|
102 |
bestLocale = flatLocales[i];
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
if(!bestLocale){
|
|
|
107 |
bestLocale = "ROOT";
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
//See if the desired locale is already loaded.
|
|
|
112 |
var tempLocale = availableFlatLocales ? bestLocale : targetLocale;
|
|
|
113 |
var bundle = dojo._loadedModules[bundlePackage];
|
|
|
114 |
var localizedBundle = null;
|
|
|
115 |
if(bundle){
|
|
|
116 |
if(djConfig.localizationComplete && bundle._built){return;}
|
|
|
117 |
var jsLoc = tempLocale.replace(/-/g, '_');
|
|
|
118 |
var translationPackage = bundlePackage+"."+jsLoc;
|
|
|
119 |
localizedBundle = dojo._loadedModules[translationPackage];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
if(!localizedBundle){
|
|
|
123 |
bundle = dojo["provide"](bundlePackage);
|
|
|
124 |
var syms = dojo._getModuleSymbols(moduleName);
|
|
|
125 |
var modpath = syms.concat("nls").join("/");
|
|
|
126 |
var parent;
|
|
|
127 |
|
|
|
128 |
dojo.i18n._searchLocalePath(tempLocale, availableFlatLocales, function(loc){
|
|
|
129 |
var jsLoc = loc.replace(/-/g, '_');
|
|
|
130 |
var translationPackage = bundlePackage + "." + jsLoc;
|
|
|
131 |
var loaded = false;
|
|
|
132 |
if(!dojo._loadedModules[translationPackage]){
|
|
|
133 |
// Mark loaded whether it's found or not, so that further load attempts will not be made
|
|
|
134 |
dojo["provide"](translationPackage);
|
|
|
135 |
var module = [modpath];
|
|
|
136 |
if(loc != "ROOT"){module.push(loc);}
|
|
|
137 |
module.push(bundleName);
|
|
|
138 |
var filespec = module.join("/") + '.js';
|
|
|
139 |
loaded = dojo._loadPath(filespec, null, function(hash){
|
|
|
140 |
// Use singleton with prototype to point to parent bundle, then mix-in result from loadPath
|
|
|
141 |
var clazz = function(){};
|
|
|
142 |
clazz.prototype = parent;
|
|
|
143 |
bundle[jsLoc] = new clazz();
|
|
|
144 |
for(var j in hash){ bundle[jsLoc][j] = hash[j]; }
|
|
|
145 |
});
|
|
|
146 |
}else{
|
|
|
147 |
loaded = true;
|
|
|
148 |
}
|
|
|
149 |
if(loaded && bundle[jsLoc]){
|
|
|
150 |
parent = bundle[jsLoc];
|
|
|
151 |
}else{
|
|
|
152 |
bundle[jsLoc] = parent;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if(availableFlatLocales){
|
|
|
156 |
//Stop the locale path searching if we know the availableFlatLocales, since
|
|
|
157 |
//the first call to this function will load the only bundle that is needed.
|
|
|
158 |
return true;
|
|
|
159 |
}
|
|
|
160 |
});
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
//Save the best locale bundle as the target locale bundle when we know the
|
|
|
164 |
//the available bundles.
|
|
|
165 |
if(availableFlatLocales && targetLocale != bestLocale){
|
|
|
166 |
bundle[targetLocale.replace(/-/g, '_')] = bundle[bestLocale.replace(/-/g, '_')];
|
|
|
167 |
}
|
|
|
168 |
};
|
|
|
169 |
|
|
|
170 |
(function(){
|
|
|
171 |
// If other locales are used, dojo.requireLocalization should load them as
|
|
|
172 |
// well, by default.
|
|
|
173 |
//
|
|
|
174 |
// Override dojo.requireLocalization to do load the default bundle, then
|
|
|
175 |
// iterate through the extraLocale list and load those translations as
|
|
|
176 |
// well, unless a particular locale was requested.
|
|
|
177 |
|
|
|
178 |
var extra = djConfig.extraLocale;
|
|
|
179 |
if(extra){
|
|
|
180 |
if(!extra instanceof Array){
|
|
|
181 |
extra = [extra];
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
var req = dojo.i18n._requireLocalization;
|
|
|
185 |
dojo.i18n._requireLocalization = function(m, b, locale, availableFlatLocales){
|
|
|
186 |
req(m,b,locale, availableFlatLocales);
|
|
|
187 |
if(locale){return;}
|
|
|
188 |
for(var i=0; i<extra.length; i++){
|
|
|
189 |
req(m,b,extra[i], availableFlatLocales);
|
|
|
190 |
}
|
|
|
191 |
};
|
|
|
192 |
}
|
|
|
193 |
})();
|
|
|
194 |
|
|
|
195 |
dojo.i18n._searchLocalePath = function(/*String*/locale, /*Boolean*/down, /*Function*/searchFunc){
|
|
|
196 |
// summary:
|
|
|
197 |
// A helper method to assist in searching for locale-based resources.
|
|
|
198 |
// Will iterate through the variants of a particular locale, either up
|
|
|
199 |
// or down, executing a callback function. For example, "en-us" and
|
|
|
200 |
// true will try "en-us" followed by "en" and finally "ROOT".
|
|
|
201 |
|
|
|
202 |
locale = dojo.i18n.normalizeLocale(locale);
|
|
|
203 |
|
|
|
204 |
var elements = locale.split('-');
|
|
|
205 |
var searchlist = [];
|
|
|
206 |
for(var i = elements.length; i > 0; i--){
|
|
|
207 |
searchlist.push(elements.slice(0, i).join('-'));
|
|
|
208 |
}
|
|
|
209 |
searchlist.push(false);
|
|
|
210 |
if(down){searchlist.reverse();}
|
|
|
211 |
|
|
|
212 |
for(var j = searchlist.length - 1; j >= 0; j--){
|
|
|
213 |
var loc = searchlist[j] || "ROOT";
|
|
|
214 |
var stop = searchFunc(loc);
|
|
|
215 |
if(stop){ break; }
|
|
|
216 |
}
|
|
|
217 |
};
|
|
|
218 |
|
|
|
219 |
dojo.i18n._preloadLocalizations = function(/*String*/bundlePrefix, /*Array*/localesGenerated){
|
|
|
220 |
// summary:
|
|
|
221 |
// Load built, flattened resource bundles, if available for all
|
|
|
222 |
// locales used in the page. Only called by built layer files.
|
|
|
223 |
|
|
|
224 |
function preload(locale){
|
|
|
225 |
locale = dojo.i18n.normalizeLocale(locale);
|
|
|
226 |
dojo.i18n._searchLocalePath(locale, true, function(loc){
|
|
|
227 |
for(var i=0; i<localesGenerated.length;i++){
|
|
|
228 |
if(localesGenerated[i] == loc){
|
|
|
229 |
dojo["require"](bundlePrefix+"_"+loc);
|
|
|
230 |
return true; // Boolean
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
return false; // Boolean
|
|
|
234 |
});
|
|
|
235 |
}
|
|
|
236 |
preload();
|
|
|
237 |
var extra = djConfig.extraLocale||[];
|
|
|
238 |
for(var i=0; i<extra.length; i++){
|
|
|
239 |
preload(extra[i]);
|
|
|
240 |
}
|
|
|
241 |
};
|
|
|
242 |
|
|
|
243 |
}
|