-
Notifications
You must be signed in to change notification settings - Fork 21
/
locales.js
112 lines (100 loc) · 3.02 KB
/
locales.js
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
/**
* Plugin to pre-load the locales the app has specified via:
*
* require.config({
* config: {
* "ecma402/locales": /^(ar-(TN|SA)|en|es|hi|ja|de)$/
* }
* });
*/
define([
"module",
"require",
"./impl/common",
"./impl/load"
], function (module, require, common, load) {
// Build variable
var writeFile;
// Compute locales to pre-load. Use hash to remove duplicates.
var localeHash = {};
localeHash.root = true;
localeHash[common.DefaultLocale()] = true;
var config = module.config();
if (config instanceof RegExp) {
common.availableLocalesList.forEach(function (locale) {
if (config.test(locale)) {
localeHash[locale] = true;
}
});
} else {
if (typeof config === "string") {
config = [ config ];
}
if (config instanceof Array) {
config.forEach(function (locale) {
var bestFitPreload = common.BestFitAvailableLocale(common.availableLocalesList, locale);
if (bestFitPreload) {
localeHash[bestFitPreload] = true;
}
});
}
}
var locales = Object.keys(localeHash);
var localeDataHash = {};
// Compute dependencies to require()
function getDependency(locale) {
return load.id + "!" + locale;
}
function isObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
return {
load: function (path, callerRequire, onload, loaderConfig) {
if (isObject(config) && loaderConfig.isBuild) {
localeHash = {};
common.availableLocalesList.forEach(function (locale) {
localeHash[locale] = true;
});
locales = Object.keys(localeHash);
}
var dependencies = locales.map(getDependency);
// Load the locale data for every requested locale, and then return it in a hash
require(dependencies, function () {
var localeDataArray = arguments;
locales.forEach(function (locale, idx) {
localeDataHash[locale] = localeDataArray[idx];
});
onload(localeDataHash);
});
},
writeFile: function (pluginName, resource, callerRequire, write) {
writeFile = write;
},
addModules: function (pluginName, resource, addModules) {
var modulesToAdd = [];
locales.forEach(function (locale) {
var localeData = localeDataHash[locale];
var calendarsDeps = localeData.calendars.map(function (cal) {return "./calendars/" + cal; });
modulesToAdd = modulesToAdd.concat(calendarsDeps);
delete localeData.calendars;
});
addModules(modulesToAdd);
},
onLayerEnd: function (write, data) {
// Calculate layer path
var match = data.path.match(/^(.*\/)?(.*)\.js$/);
var partialLayerPath = (match[1] || "") + "cldr/" + match[2] + "_";
// Calculate layer mid
match = data.name.match(/^(.*\/)?(.*)$/);
var layerMid = (match[1] || "") + "cldr/" + match[2];
locales.forEach(function (locale) {
var path = partialLayerPath + locale + ".js";
writeFile(path, "define(" + JSON.stringify(localeDataHash[locale]) + ")");
});
localeHash._layerMid = layerMid;
write("require.config({config:{'" + load.id + "':" + JSON.stringify(localeHash) + "}});");
// Reset
localeDataHash = {};
}
};
});