Repository navigation
Expand file tree
/
Copy pathi18n.js
More file actions
366 lines (324 loc) · 12.3 KB
/
Copy pathi18n.js
File metadata and controls
366 lines (324 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
define(["./_base/kernel", "require", "./has", "./_base/array", "./_base/config", "./_base/lang", "./_base/xhr"],
function(dojo, require, has, array, config, lang, xhr) {
// module:
// dojo/i18n
// summary:
// This module implements the !dojo/i18n plugin and the v1.6- i18n API
// description:
// We choose to include our own plugin to leverage functionality already contained in dojo
// and thereby reduce the size of the plugin compared to various loader implementations. Also, this
// allows foreign AMD loaders to be used without their plugins.
var
thisModule= dojo.i18n=
// the dojo.i18n module
{},
nlsRe=
// regexp for reconstructing the master bundle name from parts of the regexp match
// nlsRe.exec("foo/bar/baz/nls/en-ca/foo") gives:
// ["foo/bar/baz/nls/en-ca/foo", "foo/bar/baz/nls/", "/", "/", "en-ca", "foo"]
// nlsRe.exec("foo/bar/baz/nls/foo") gives:
// ["foo/bar/baz/nls/foo", "foo/bar/baz/nls/", "/", "/", "foo", ""]
// so, if match[5] is blank, it means this is the top bundle definition.
// courtesy of http://requirejs.org
/(^.*(^|\/)nls)(\/|$)([^\/]*)\/?([^\/]*)/,
getAvailableLocales= function(
root,
locale,
bundlePath,
bundleName
){
// return a vector of module ids containing all available locales with respect to the target locale
// For example, assuming:
// * the root bundle indicates specific bundles for "fr" and "fr-ca",
// * bundlePath is "myPackage/nls"
// * bundleName is "myBundle"
// Then a locale argument of "fr-ca" would return
// ["myPackage/nls/myBundle", "myPackage/nls/fr/myBundle", "myPackage/nls/fr-ca/myBundle"]
// Notice that bundles are returned least-specific to most-specific, starting with the root.
//
// If root===false indicates we're working with a pre-AMD i18n bundle that doesn't tell about the available locales;
// therefore, assume everything is available and get 404 errors that indicate a particular localization is not available
//
for(var result= [bundlePath + bundleName], localeParts= locale.split("-"), current= "", i= 0; i<localeParts.length; i++){
current+= (current ? "-" : "") + localeParts[i];
if(!root || root[current]){
result.push(bundlePath + current + "/" + bundleName);
}
}
return result;
},
cache= {},
getL10nName= dojo.getL10nName = function(moduleName, bundleName, locale){
locale = locale ? locale.toLowerCase() : dojo.locale;
moduleName = "dojo/i18n!" + moduleName.replace(/\./g, "/");
bundleName = bundleName.replace(/\./g, "/");
return (/root/i.test(locale)) ?
(moduleName + "/nls/" + bundleName) :
(moduleName + "/nls/" + locale + "/" + bundleName);
},
doLoad = function(require, bundlePathAndName, bundlePath, bundleName, locale, load){
// get the root bundle which instructs which other bundles are required to construct the localized bundle
require([bundlePathAndName], function(root){
var
current= cache[bundlePathAndName + "/"]= lang.clone(root.root),
availableLocales= getAvailableLocales(!root._v1x && root, locale, bundlePath, bundleName);
require(availableLocales, function(){
for (var i= 1; i<availableLocales.length; i++){
cache[availableLocales[i]]= current= lang.mixin(lang.clone(current), arguments[i]);
}
// target may not have been resolve (e.g., maybe only "fr" exists when "fr-ca" was requested)
var target= bundlePathAndName + "/" + locale;
cache[target]= current;
load && load(lang.delegate(current));
});
});
},
normalize = function(id, toAbsMid){
// note: id may be relative
var match= nlsRe.exec(id),
bundlePath= match[1];
return /^\./.test(bundlePath) ? toAbsMid(bundlePath) + "/" + id.substring(bundlePath.length) : id;
},
noop = function(){},
checkForLegacyModules = noop,
waitForPreloads = noop,
load = function(id, require, load){
// note: id is always absolute
if(waitForPreloads(id, require, load)){
return;
}
var
match= nlsRe.exec(id),
bundlePath= match[1] + "/",
bundleName= match[5] || match[4],
bundlePathAndName= bundlePath + bundleName,
localeSpecified = (match[5] && match[4]),
targetLocale= localeSpecified || dojo.locale,
target= bundlePathAndName + "/" + targetLocale;
if(localeSpecified){
checkForLegacyModules(target);
if(cache[target]){
// a request for a specific local that has already been loaded; just return it
load(cache[target]);
}else{
// a request for a specific local that has not been loaded; load and return just that locale
doLoad(require, bundlePathAndName, bundlePath, bundleName, targetLocale, load);
}
return;
}// else a non-locale-specific request; therefore always load dojo.locale + config.extraLocale
// notice the subtle algorithm that loads targetLocal last, which is the only doLoad application that passes a value for the load callback
// this makes the sync loader follow a clean code path that loads extras first and then proceeds with tracing the current deps graph
var extra = config.extraLocale || [];
extra = lang.isArray(extra) ? extra : [extra];
extra.push(targetLocale);
var remaining = extra.length,
targetBundle;
array.forEach(extra, function(locale){
doLoad(require, bundlePathAndName, bundlePath, bundleName, locale, function(bundle){
if(locale == targetLocale){
targetBundle = bundle;
}
if(!--remaining){
load(targetBundle);
}
});
});
};
if(has("dojo-unit-tests")){
var unitTests = thisModule.unitTests = [];
}
has.add("dojo-v1x-i18n-Api",
// if true, define the v1.x i18n functions
1
);
if(has("dojo-v1x-i18n-Api")){
// this code path assumes the dojo loader and won't work with a standard AMD loader
var
evalBundle=
// use the function ctor to keep the minifiers away (also come close to global scope, but this is secondary)
new Function(
"__bundle", // the bundle to evalutate
"__checkForLegacyModules", // a function that checks if __bundle defined __mid in the global space
"__mid", // the mid that __bundle is intended to define
// returns one of:
// 1 => the bundle was an AMD bundle
// a legacy bundle object that is the value of __mid
// instance of Error => could not figure out how to evaluate bundle
// used to detect when __bundle calls define
"var define = function(){define.called = 1;},"
+ " require = function(){define.called = 1;};"
+ "try{"
+ "define.called = 0;"
+ "eval(__bundle);"
+ "if(define.called==1)"
// bundle called define; therefore signal it's an AMD bundle
+ "return 1;"
+ "if((__checkForLegacyModules = __checkForLegacyModules(__mid)))"
// bundle was probably a v1.6- built NLS flattened NLS bundle that defined __mid in the global space
+ "return __checkForLegacyModules;"
+ "}catch(e){}"
// evaulating the bundle was *neither* an AMD *nor* a legacy flattened bundle
// either way, re-eval *after* surrounding with parentheses
+ "try{"
+ "return eval('('+__bundle+')');"
+ "}catch(e){"
+ "return e;"
+ "}"
),
syncRequire= function(deps, callback){
var results= [];
array.forEach(deps, function(mid){
var url= require.toUrl(mid + ".js");
if(cache[url]){
results.push(cache[url]);
}else{
var bundle= require.syncLoadNls(mid);
// don't need to check for legacy since syncLoadNls returns a module if the module
// (1) was already loaded, or (2) was in the cache. In case 1, if syncRequire is called
// from getLocalization --> load, then load will have called checkForLegacyModules() before
// calling syncRequire; if syncRequire is called from preloadLocalizations, then we
// don't care about checkForLegacyModules() because that will be done when a particular
// bundle is actually demanded. In case 2, checkForLegacyModules() is never relevant
// because cached modules are always v1.7+ built modules.
if(bundle){
results.push(bundle);
}else{
xhr.get({
url:url,
sync:true,
load:function(text){
var result = evalBundle(text, checkForLegacyModules, mid);
if(result===1){
require.eval(text);
require([mid], function(bundle){
results.push(cache[url]= bundle);
});
}else{
if(result instanceof Error){
console.error("failed to evaluate i18n bundle; url=" + url, result);
result = {};
}
// nls/<locale>/<bundle-name> indicates not the root.
results.push(cache[url] = (/nls\/[^\/]+\/[^\/]+$/.test(url) ? result : {root:result, _v1x:1}));
}
},
error:function(){
results.push(cache[url]= {});
}
});
}
}
});
callback && callback.apply(null, results);
},
normalizeLocale = thisModule.normalizeLocale= function(locale){
var result = locale ? locale.toLowerCase() : dojo.locale;
if(result == "root"){
result = "ROOT";
}
return result;
},
forEachLocale = function(locale, func){
// this function is equivalent to v1.6 dojo.i18n._searchLocalePath with down===true
var parts = locale.split("-");
while(parts.length){
if(func(parts.join("-"))){
return true;
}
parts.pop();
}
return func("ROOT");
},
isXd = function(mid){
return require.isXdUrl(require.toUrl(mid + ".js"));
},
preloading = 0,
preloadWaitQueue = [];
checkForLegacyModules = function(target){
// legacy code may have already loaded [e.g] the raw bundle x/y/z at x.y.z; when true, push into the cache
for(var result, names = target.split("/"), object = dojo.global[names[0]], i = 1; object && i<names.length-1; object = object[names[i++]]){}
if(object){
result = object[names[i]];
if(!result){
// fallback for incorrect bundle build of 1.6
result = object[names[i].replace(/-/g,"_")];
}
if(result){
cache[target] = result;
}
}
return result;
};
waitForPreloads = function(id, require, load){
if(preloading){
preloadWaitQueue.push([id, require, load]);
}
return preloading;
};
thisModule.getLocalization= function(moduleName, bundleName, locale){
var result,
l10nName= getL10nName(moduleName, bundleName, locale).substring(10);
load(l10nName, (has("dojo-sync-loader") && !isXd(l10nName) ? syncRequire : require), function(result_){ result= result_; });
return result;
};
thisModule._preloadLocalizations = function(/*String*/bundlePrefix, /*Array*/localesGenerated){
// summary:
// Load built, flattened resource bundles, if available for all
// locales used in the page. Only called by built layer files.
//
// note: this function a direct copy of v1.6 function of same name
function preload(locale){
locale = normalizeLocale(locale);
forEachLocale(locale, function(loc){
for(var mid, i=0; i<localesGenerated.length; i++){
if(localesGenerated[i] == loc){
mid = bundlePrefix.replace(/\./g, "/")+"_"+loc;
preloading++;
(isXd(mid) ? require : syncRequire)([mid], function(){
if(!--preloading){
while(preloadWaitQueue.length){
load.apply(null, preloadWaitQueue.shift());
}
}
});
return true; // Boolean
}
}
return false; // Boolean
});
}
preload();
var extra = dojo.config.extraLocale||[];
for(var i=0; i<extra.length; i++){
preload(extra[i]);
}
};
if(has("dojo-unit-tests")){
unitTests.push(function(doh){
doh.register("tests.i18n.unit", function(t){
var check;
check = evalBundle("{prop:1}");
t.is({prop:1}, check); t.is(undefined, check[1]);
check = evalBundle("({prop:1})");
t.is({prop:1}, check); t.is(undefined, check[1]);
check = evalBundle("{'prop-x':1}");
t.is({'prop-x':1}, check); t.is(undefined, check[1]);
check = evalBundle("({'prop-x':1})");
t.is({'prop-x':1}, check); t.is(undefined, check[1]);
check = evalBundle("define({'prop-x':1})");
t.is(1, check);
check = evalBundle("this is total nonsense and should throw an error");
t.is(check instanceof Error, true);
});
});
}
}
return lang.mixin(thisModule, {
dynamic:true,
normalize:normalize,
load:load,
cache:function(mid, value){
cache[mid] = value;
}
});
});