Skip to content

Commit 116704f

Browse files
committed
added build mecanism
1 parent 9254268 commit 116704f

File tree

1 file changed

+93
-80
lines changed

1 file changed

+93
-80
lines changed

i18n.js

Lines changed: 93 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Plugin based on requirejs i18n
33
* see: http://github.com/requirejs/i18n for details
44
*/
5-
define(['./i18n/common', "module"], function (common, module) {
5+
define(['./i18n/common', "module", "require"], function (common, module, requirejs) {
66

77
// regexp for reconstructing the master bundle name from parts of the regexp match
88
// nlsRegExp.exec("foo/bar/baz/nls/en-ca/foo") gives:
@@ -16,9 +16,8 @@ define(['./i18n/common', "module"], function (common, module) {
1616
// Build variables
1717
bundlesList = [],
1818
confLocalesList,
19-
mid,
20-
parentRequire,
21-
19+
pluginMid,
20+
2221
// Simple function to mix in properties from source into target,
2322
// but only if target does not already have a property of the same name.
2423
// This is not robust in IE for transferring methods that match
@@ -47,6 +46,16 @@ define(['./i18n/common', "module"], function (common, module) {
4746
}
4847
},
4948

49+
normalizeBundlesMap = function (bundlesMap) {
50+
var result = {};
51+
eachProp(bundlesMap, function (layer, bundleList) {
52+
bundleList.forEach(function (bundle) {
53+
result[bundle] = layer;
54+
});
55+
});
56+
return result;
57+
},
58+
5059
// Use nlsRegExp to parse the resource mid and return a usable object.
5160
parseName = function (name) {
5261
var match = nlsRegExp.exec(name);
@@ -113,9 +122,9 @@ define(['./i18n/common', "module"], function (common, module) {
113122

114123
getLayer = function (name, layer, moduleConfig, getParentLocale, req, onLoad) {
115124
var locale = name.requestedLocale,
116-
localesMap = moduleConfig.localesMap[layer];
125+
localesList = moduleConfig.localesMap[layer];
117126

118-
while (locale && !localesMap[locale]) {
127+
while (locale && localesList.indexOf(locale) < 0) {
119128
locale = getParentLocale(locale);
120129
}
121130

@@ -168,13 +177,17 @@ define(['./i18n/common', "module"], function (common, module) {
168177
};
169178

170179
return {
180+
/* jshint -W074 */
171181
load: function (name, req, onLoad, config) {
172182
config = config || {};
173183

174-
var moduleConfig = module.config() || {},
184+
var moduleConfig = {},
175185
masterMid,
176186
layer;
177187

188+
// Copy the config
189+
mixin(moduleConfig, typeof module.config === "function" ? module.config() || {} : {});
190+
178191
if (config.isBuild) {
179192
confLocalesList = config.localesList;
180193
onLoad();
@@ -197,7 +210,9 @@ define(['./i18n/common', "module"], function (common, module) {
197210
// From now on there is at least one layer available
198211

199212
// Check if requested module is in a layer
213+
moduleConfig.bundlesMap = normalizeBundlesMap(moduleConfig.bundlesMap);
200214
layer = moduleConfig.bundlesMap[masterMid];
215+
201216
if (!layer && moduleConfig.layerOnly) {
202217
console.log("i18n: module " + masterMid + " not found in layer.");
203218
onLoad();
@@ -219,21 +234,49 @@ define(['./i18n/common', "module"], function (common, module) {
219234
return;
220235
}
221236
},
237+
/* jshint +W074 */
222238

223-
writeFile: function (pluginName, moduleName, req, write) {
224-
mid = mid || pluginName;
225-
parentRequire = req;
239+
write: function (pluginName, moduleName, write) {
240+
pluginMid = pluginMid || pluginName;
226241
bundlesList.push(moduleName);
227242
},
228243

229244
onLayerEnd: function (write, data) {
230245
var i18nConf,
231246
layersContent = {},
232247
localesList,
248+
pseudoRoots,
249+
match = data.name.match(/^(.*\/)(.*)$/),
250+
layerMid = match[1] + "nls/" + match[2],
251+
252+
// Get the list of locales to process and calculate the pseudoRoots
253+
init = function () {
254+
localesList = confLocalesList || [];
255+
pseudoRoots = {};
256+
bundlesList.forEach(function (bundle) {
257+
var root = requirejs(bundle);
258+
pseudoRoots[bundle] = pseudoRoots[bundle] || {};
259+
260+
eachProp(root, function (loc) {
261+
// Calculate the pseudoRoots
262+
var parent = common.getParentLocale(loc);
263+
while (parent && parent !== "root") {
264+
pseudoRoots[bundle][parent] = pseudoRoots[parent] || {};
265+
pseudoRoots[bundle][parent][loc] = true;
266+
parent = common.getParentLocale(parent);
267+
}
268+
// List all available locales if no list provided
269+
if (!confLocalesList && localesList.indexOf(loc) < 0) {
270+
localesList.push(loc);
271+
}
272+
});
273+
});
274+
},
275+
233276
resetPlugin = function () {
234277
bundlesList = [];
235278
confLocalesList = undefined;
236-
mid = undefined;
279+
pluginMid = undefined;
237280
parentRequire = undefined;
238281
};
239282

@@ -242,48 +285,19 @@ define(['./i18n/common', "module"], function (common, module) {
242285
return;
243286
}
244287

245-
localesList = confLocalesList;
246-
if (!localesList) {
247-
// list all existing locales
248-
localesList = [];
249-
bundlesList.forEach(function (bundle) {
250-
var root = parentRequire(bundle);
251-
eachProp(root, function (loc) {
252-
if (localesList.indexOf(loc) < 0) {
253-
localesList.push(loc);
254-
}
255-
});
256-
});
257-
}
258-
if (localesList.indexOf("root") < 0) {
259-
localesList.push("root");
260-
}
261-
262-
288+
// Run through every bundles to know which locales are available for each module.
289+
init();
263290

264-
// write a nls layer for each locale
291+
// calculate the nls layer for each locale
265292
bundlesList.forEach(function (bundle) {
266293
var name = parseName(bundle),
267-
root,
268-
parent,
269-
availableLocales = [],
270-
pseudoRoots = {};
271-
272-
273-
// todo if name.requestedLocale
274-
// rassembler les eachProp avec au dessus
275-
root = parentRequire(bundle);
276-
eachProp(root, function (loc) {
277-
if (loc !== "root") {
278-
availableLocales.push(loc);
279-
parent = common.getParentLocale(loc);
280-
while (parent && parent !== "root") {
281-
pseudoRoots[parent] = pseudoRoots[parent] || {};
282-
pseudoRoots[parent][loc] = true;
283-
parent = common.getParentLocale(parent);
284-
}
285-
}
286-
});
294+
normalizeRoot = function (bundle, name) {
295+
bundle.root = (bundle.root === true || bundle.root === 1) ?
296+
requirejs(name.prefix + "root/" + name.suffix) : bundle.root;
297+
return bundle;
298+
},
299+
root = normalizeRoot(requirejs(bundle), name);
300+
// TODO: if name.requestedLocale a different optimization is possible
287301

288302
localesList.forEach(function (loc) {
289303
var result = {},
@@ -294,66 +308,65 @@ define(['./i18n/common', "module"], function (common, module) {
294308

295309
if (loc !== "root") {
296310
while (locale && locale !== "root") {
297-
if (availableLocales.indexOf(locale) >= 0) {
298-
localizedBundle = parentRequire(name.prefix + locale + "/" + name.suffix);
311+
if (root[locale]) {
312+
localizedBundle = requirejs(name.prefix + locale + "/" + name.suffix);
299313
mixin(result, localizedBundle);
300314
}
301315
locale = common.getParentLocale(locale);
302316
}
303-
localizedBundle = root.root === true || root.root === 1 ? parentRequire(name.prefix + "root/" + name.suffix) : root.root;
317+
localizedBundle = root.root;
304318
mixin(result, localizedBundle);
305319

306320
result._flattened = true;
307-
result._pseudoRoot = pseudoRoots[loc] || {};
308-
309-
layersContent[loc] += 'define("' + name.prefix + loc + "/" + name.suffix + '",' + JSON.stringify(result) + ");";
321+
result._pseudoRoot = pseudoRoots[bundle][loc] || {};
310322
} else {
311323
mixin(result, root);
312-
result.root = result.root === true || result.root === 1 ? parentRequire(name.prefix + "root/" + name.suffix) : result.root;
313-
layersContent[loc] += 'define("' + name.prefix + loc + "/" + name.suffix + '",' + JSON.stringify(result) + ");";
314324
}
315325

326+
layersContent[loc] += 'define("' + name.prefix + loc + "/" +
327+
name.suffix + '",' + JSON.stringify(result) + ");";
316328
});
317329
});
318330

319331
// This assumes nodejs
320332
eachProp(layersContent, function (loc, content) {
321333
var fs = require("fs"),
322-
matchP = data.path.match(/^(.*\/)(.*)(\.js)$/),
323-
path = matchP[1] + "nls/" + matchP[2] + "_" + loc + ".js",
324-
parts = path.split("/"),
325-
tmp = [],
326-
matchM = data.name.match(/^(.*\/)(.*)$/),
327-
mid = matchM[1] + "nls/" + matchM[2] + "_" + loc;
334+
createPath = function (path) {
335+
var parts = path.split("/"),
336+
tmp = [];
337+
338+
while (!fs.existsSync(parts.join("/") + "/")) {
339+
tmp.push(parts.pop());
340+
}
341+
while (tmp.length) {
342+
parts.push(tmp.pop());
343+
fs.mkdirSync(parts.join("/"));
344+
}
345+
},
346+
347+
match = data.path.match(/^(.*\/)(.*)(\.js)$/),
348+
dir = match[1] + "nls/",
349+
filename = match[2] + "_" + loc + ".js",
350+
mid = layerMid + "_" + loc;
328351

329352
content += "define('" + mid + "', true);";
330353

331354
// Create path
332-
parts.pop();
333-
while (!fs.existsSync(parts.join("/") + "/")) {
334-
tmp.push(parts.pop());
335-
}
336-
while (tmp.length) {
337-
parts.push(tmp.pop());
338-
fs.mkdirSync(parts.join("/"));
339-
}
340-
341-
fs.writeFileSync(path, content);
355+
createPath(dir);
356+
fs.writeFileSync(dir + filename, content);
342357
});
343358

344-
console.log(layersContent);
345-
console.log(data.name);
346359

360+
// Init config structure
347361
i18nConf = {
348362
config: {}
349363
};
350-
i18nConf.config[mid] = {
364+
i18nConf.config[pluginMid] = {
351365
bundlesMap: {},
352366
localesMap: {}
353367
};
354-
i18nConf.config[mid].bundlesMap[data.name] = bundlesList;
355-
i18nConf.config[mid].localesMap[data.name] = localesList;
356-
//changer format config pour coller au mail addresser à christophe
368+
i18nConf.config[pluginMid].bundlesMap[layerMid] = bundlesList;
369+
i18nConf.config[pluginMid].localesMap[layerMid] = localesList;
357370

358371
// write i18n config on the layer
359372
write("require.config(" + JSON.stringify(i18nConf) + ")");

0 commit comments

Comments
 (0)