2
2
* Plugin based on requirejs i18n
3
3
* see: http://github.com/requirejs/i18n for details
4
4
*/
5
- define ( [ './i18n/common' , "module" ] , function ( common , module ) {
5
+ define ( [ './i18n/common' , "module" , "require" ] , function ( common , module , requirejs ) {
6
6
7
7
// regexp for reconstructing the master bundle name from parts of the regexp match
8
8
// nlsRegExp.exec("foo/bar/baz/nls/en-ca/foo") gives:
@@ -16,9 +16,8 @@ define(['./i18n/common', "module"], function (common, module) {
16
16
// Build variables
17
17
bundlesList = [ ] ,
18
18
confLocalesList ,
19
- mid ,
20
- parentRequire ,
21
-
19
+ pluginMid ,
20
+
22
21
// Simple function to mix in properties from source into target,
23
22
// but only if target does not already have a property of the same name.
24
23
// This is not robust in IE for transferring methods that match
@@ -47,6 +46,16 @@ define(['./i18n/common', "module"], function (common, module) {
47
46
}
48
47
} ,
49
48
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
+
50
59
// Use nlsRegExp to parse the resource mid and return a usable object.
51
60
parseName = function ( name ) {
52
61
var match = nlsRegExp . exec ( name ) ;
@@ -113,9 +122,9 @@ define(['./i18n/common', "module"], function (common, module) {
113
122
114
123
getLayer = function ( name , layer , moduleConfig , getParentLocale , req , onLoad ) {
115
124
var locale = name . requestedLocale ,
116
- localesMap = moduleConfig . localesMap [ layer ] ;
125
+ localesList = moduleConfig . localesMap [ layer ] ;
117
126
118
- while ( locale && ! localesMap [ locale ] ) {
127
+ while ( locale && localesList . indexOf ( locale ) < 0 ) {
119
128
locale = getParentLocale ( locale ) ;
120
129
}
121
130
@@ -168,13 +177,17 @@ define(['./i18n/common', "module"], function (common, module) {
168
177
} ;
169
178
170
179
return {
180
+ /* jshint -W074 */
171
181
load : function ( name , req , onLoad , config ) {
172
182
config = config || { } ;
173
183
174
- var moduleConfig = module . config ( ) || { } ,
184
+ var moduleConfig = { } ,
175
185
masterMid ,
176
186
layer ;
177
187
188
+ // Copy the config
189
+ mixin ( moduleConfig , typeof module . config === "function" ? module . config ( ) || { } : { } ) ;
190
+
178
191
if ( config . isBuild ) {
179
192
confLocalesList = config . localesList ;
180
193
onLoad ( ) ;
@@ -197,7 +210,9 @@ define(['./i18n/common', "module"], function (common, module) {
197
210
// From now on there is at least one layer available
198
211
199
212
// Check if requested module is in a layer
213
+ moduleConfig . bundlesMap = normalizeBundlesMap ( moduleConfig . bundlesMap ) ;
200
214
layer = moduleConfig . bundlesMap [ masterMid ] ;
215
+
201
216
if ( ! layer && moduleConfig . layerOnly ) {
202
217
console . log ( "i18n: module " + masterMid + " not found in layer." ) ;
203
218
onLoad ( ) ;
@@ -219,21 +234,49 @@ define(['./i18n/common', "module"], function (common, module) {
219
234
return ;
220
235
}
221
236
} ,
237
+ /* jshint +W074 */
222
238
223
- writeFile : function ( pluginName , moduleName , req , write ) {
224
- mid = mid || pluginName ;
225
- parentRequire = req ;
239
+ write : function ( pluginName , moduleName , write ) {
240
+ pluginMid = pluginMid || pluginName ;
226
241
bundlesList . push ( moduleName ) ;
227
242
} ,
228
243
229
244
onLayerEnd : function ( write , data ) {
230
245
var i18nConf ,
231
246
layersContent = { } ,
232
247
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
+
233
276
resetPlugin = function ( ) {
234
277
bundlesList = [ ] ;
235
278
confLocalesList = undefined ;
236
- mid = undefined ;
279
+ pluginMid = undefined ;
237
280
parentRequire = undefined ;
238
281
} ;
239
282
@@ -242,48 +285,19 @@ define(['./i18n/common', "module"], function (common, module) {
242
285
return ;
243
286
}
244
287
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 ( ) ;
263
290
264
- // write a nls layer for each locale
291
+ // calculate the nls layer for each locale
265
292
bundlesList . forEach ( function ( bundle ) {
266
293
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
287
301
288
302
localesList . forEach ( function ( loc ) {
289
303
var result = { } ,
@@ -294,66 +308,65 @@ define(['./i18n/common', "module"], function (common, module) {
294
308
295
309
if ( loc !== "root" ) {
296
310
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 ) ;
299
313
mixin ( result , localizedBundle ) ;
300
314
}
301
315
locale = common . getParentLocale ( locale ) ;
302
316
}
303
- localizedBundle = root . root === true || root . root === 1 ? parentRequire ( name . prefix + "root/" + name . suffix ) : root . root ;
317
+ localizedBundle = root . root ;
304
318
mixin ( result , localizedBundle ) ;
305
319
306
320
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 ] || { } ;
310
322
} else {
311
323
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
324
}
315
325
326
+ layersContent [ loc ] += 'define("' + name . prefix + loc + "/" +
327
+ name . suffix + '",' + JSON . stringify ( result ) + ");" ;
316
328
} ) ;
317
329
} ) ;
318
330
319
331
// This assumes nodejs
320
332
eachProp ( layersContent , function ( loc , content ) {
321
333
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 ;
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 ( / ^ ( .* \/ ) ( .* ) ( \. j s ) $ / ) ,
348
+ dir = match [ 1 ] + "nls/" ,
349
+ filename = match [ 2 ] + "_" + loc + ".js" ,
350
+ mid = layerMid + "_" + loc ;
328
351
329
352
content += "define('" + mid + "', true);" ;
330
353
331
354
// 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 ) ;
342
357
} ) ;
343
358
344
- console . log ( layersContent ) ;
345
- console . log ( data . name ) ;
346
359
360
+ // Init config structure
347
361
i18nConf = {
348
362
config : { }
349
363
} ;
350
- i18nConf . config [ mid ] = {
364
+ i18nConf . config [ pluginMid ] = {
351
365
bundlesMap : { } ,
352
366
localesMap : { }
353
367
} ;
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 ;
357
370
358
371
// write i18n config on the layer
359
372
write ( "require.config(" + JSON . stringify ( i18nConf ) + ")" ) ;
0 commit comments