Skip to content

Commit 9254268

Browse files
committed
almost good build plugin
1 parent 6f4594c commit 9254268

File tree

1 file changed

+163
-1
lines changed

1 file changed

+163
-1
lines changed

i18n.js

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ define(['./i18n/common', "module"], function (common, module) {
1313
var nlsRegExp = /(^.*(^|\/)nls(\/|$))([^\/]*)\/?([^\/]*)/,
1414

1515

16+
// Build variables
17+
bundlesList = [],
18+
confLocalesList,
19+
mid,
20+
parentRequire,
21+
1622
// Simple function to mix in properties from source into target,
1723
// but only if target does not already have a property of the same name.
1824
// This is not robust in IE for transferring methods that match
@@ -32,6 +38,15 @@ define(['./i18n/common', "module"], function (common, module) {
3238
}
3339
},
3440

41+
eachProp = function (obj, func) {
42+
var prop;
43+
for (prop in obj) {
44+
if (obj.hasOwnProperty(prop)) {
45+
func(prop, obj[prop]);
46+
}
47+
}
48+
},
49+
3550
// Use nlsRegExp to parse the resource mid and return a usable object.
3651
parseName = function (name) {
3752
var match = nlsRegExp.exec(name);
@@ -160,8 +175,14 @@ define(['./i18n/common', "module"], function (common, module) {
160175
masterMid,
161176
layer;
162177

178+
if (config.isBuild) {
179+
confLocalesList = config.localesList;
180+
onLoad();
181+
return;
182+
}
183+
163184
moduleConfig.enhanceLayer = moduleConfig.enhanceLayer === undefined ? true : moduleConfig.enhanceLayer;
164-
185+
165186
// Parse name and set the locale if a top level bundle is required
166187
name = parseName(name);
167188
name.requestedLocale = name.requestedLocale || common.getLocale(moduleConfig.locale || config.locale);
@@ -197,6 +218,147 @@ define(['./i18n/common', "module"], function (common, module) {
197218
getLayer(name, layer, moduleConfig, common.getParentLocale, req, onLoad);
198219
return;
199220
}
221+
},
222+
223+
writeFile: function (pluginName, moduleName, req, write) {
224+
mid = mid || pluginName;
225+
parentRequire = req;
226+
bundlesList.push(moduleName);
227+
},
228+
229+
onLayerEnd: function (write, data) {
230+
var i18nConf,
231+
layersContent = {},
232+
localesList,
233+
resetPlugin = function () {
234+
bundlesList = [];
235+
confLocalesList = undefined;
236+
mid = undefined;
237+
parentRequire = undefined;
238+
};
239+
240+
if (!data.name) {
241+
resetPlugin();
242+
return;
243+
}
244+
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+
263+
264+
// write a nls layer for each locale
265+
bundlesList.forEach(function (bundle) {
266+
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+
});
287+
288+
localesList.forEach(function (loc) {
289+
var result = {},
290+
localizedBundle,
291+
locale = loc;
292+
293+
layersContent[loc] = layersContent[loc] || "";
294+
295+
if (loc !== "root") {
296+
while (locale && locale !== "root") {
297+
if (availableLocales.indexOf(locale) >= 0) {
298+
localizedBundle = parentRequire(name.prefix + locale + "/" + name.suffix);
299+
mixin(result, localizedBundle);
300+
}
301+
locale = common.getParentLocale(locale);
302+
}
303+
localizedBundle = root.root === true || root.root === 1 ? parentRequire(name.prefix + "root/" + name.suffix) : root.root;
304+
mixin(result, localizedBundle);
305+
306+
result._flattened = true;
307+
result._pseudoRoot = pseudoRoots[loc] || {};
308+
309+
layersContent[loc] += 'define("' + name.prefix + loc + "/" + name.suffix + '",' + JSON.stringify(result) + ");";
310+
} else {
311+
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) + ");";
314+
}
315+
316+
});
317+
});
318+
319+
// This assumes nodejs
320+
eachProp(layersContent, function (loc, content) {
321+
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;
328+
329+
content += "define('" + mid + "', true);";
330+
331+
// 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);
342+
});
343+
344+
console.log(layersContent);
345+
console.log(data.name);
346+
347+
i18nConf = {
348+
config: {}
349+
};
350+
i18nConf.config[mid] = {
351+
bundlesMap: {},
352+
localesMap: {}
353+
};
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
357+
358+
// write i18n config on the layer
359+
write("require.config(" + JSON.stringify(i18nConf) + ")");
360+
361+
resetPlugin();
200362
}
201363
};
202364
});

0 commit comments

Comments
 (0)