@@ -13,6 +13,12 @@ define(['./i18n/common', "module"], function (common, module) {
13
13
var nlsRegExp = / ( ^ .* ( ^ | \/ ) n l s ( \/ | $ ) ) ( [ ^ \/ ] * ) \/ ? ( [ ^ \/ ] * ) / ,
14
14
15
15
16
+ // Build variables
17
+ bundlesList = [ ] ,
18
+ confLocalesList ,
19
+ mid ,
20
+ parentRequire ,
21
+
16
22
// Simple function to mix in properties from source into target,
17
23
// but only if target does not already have a property of the same name.
18
24
// This is not robust in IE for transferring methods that match
@@ -32,6 +38,15 @@ define(['./i18n/common', "module"], function (common, module) {
32
38
}
33
39
} ,
34
40
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
+
35
50
// Use nlsRegExp to parse the resource mid and return a usable object.
36
51
parseName = function ( name ) {
37
52
var match = nlsRegExp . exec ( name ) ;
@@ -160,8 +175,14 @@ define(['./i18n/common', "module"], function (common, module) {
160
175
masterMid ,
161
176
layer ;
162
177
178
+ if ( config . isBuild ) {
179
+ confLocalesList = config . localesList ;
180
+ onLoad ( ) ;
181
+ return ;
182
+ }
183
+
163
184
moduleConfig . enhanceLayer = moduleConfig . enhanceLayer === undefined ? true : moduleConfig . enhanceLayer ;
164
-
185
+
165
186
// Parse name and set the locale if a top level bundle is required
166
187
name = parseName ( name ) ;
167
188
name . requestedLocale = name . requestedLocale || common . getLocale ( moduleConfig . locale || config . locale ) ;
@@ -197,6 +218,147 @@ define(['./i18n/common', "module"], function (common, module) {
197
218
getLayer ( name , layer , moduleConfig , common . getParentLocale , req , onLoad ) ;
198
219
return ;
199
220
}
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 ( / ^ ( .* \/ ) ( .* ) ( \. j s ) $ / ) ,
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 ( ) ;
200
362
}
201
363
} ;
202
364
} ) ;
0 commit comments