2
2
* Plugin based on requirejs i18n
3
3
* see: http://github.com/requirejs/i18n for details
4
4
*/
5
- define ( [ './i18n/common' , "module" , "require" ] , function ( common , module , requirejs ) {
6
-
7
- // regexp for reconstructing the master bundle name from parts of the regexp match
8
- // nlsRegExp.exec("foo/bar/baz/nls/en-ca/foo") gives:
9
- // ["foo/bar/baz/nls/en-ca/foo", "foo/bar/baz/nls/", "/", "/", "en-ca", "foo"]
10
- // nlsRegExp.exec("foo/bar/baz/nls/foo") gives:
11
- // ["foo/bar/baz/nls/foo", "foo/bar/baz/nls/", "/", "/", "foo", ""]
12
- // so, if match[5] is blank, it means this is the top bundle definition.
13
- var nlsRegExp = / ( ^ .* ( ^ | \/ ) n l s ( \/ | $ ) ) ( [ ^ \/ ] * ) \/ ? ( [ ^ \/ ] * ) / ,
14
-
15
-
16
- // Build variables
17
- bundlesList = [ ] ,
18
- confLocalesList ,
19
- pluginMid ,
20
-
21
- // Simple function to mix in properties from source into target,
22
- // but only if target does not already have a property of the same name.
23
- // This is not robust in IE for transferring methods that match
24
- // Object.prototype names, but the uses of mixin here seem unlikely to
25
- // trigger a problem related to that.
26
- mixin = function ( target , source , force ) {
27
- var prop ;
28
- for ( prop in source ) {
29
- if ( source . hasOwnProperty ( prop ) && ( ! target . hasOwnProperty ( prop ) || force ) ) {
30
- target [ prop ] = source [ prop ] ;
31
- } else if ( typeof source [ prop ] === 'object' ) {
32
- if ( ! target [ prop ] && source [ prop ] ) {
33
- target [ prop ] = { } ;
34
- }
35
- mixin ( target [ prop ] , source [ prop ] , force ) ;
36
- }
37
- }
38
- } ,
5
+ define ( [ "./i18n/common" , "./i18n/build" , "module" ] , function ( common , build , module ) {
39
6
40
- eachProp = function ( obj , func ) {
41
- var prop ;
42
- for ( prop in obj ) {
43
- if ( obj . hasOwnProperty ( prop ) ) {
44
- func ( prop , obj [ prop ] ) ;
45
- }
46
- }
47
- } ,
7
+ var localesList ,
8
+ mixin = common . mixin ,
9
+ eachProp = common . eachProp ,
10
+ parseName = common . parseName ,
11
+ getMasterMid = common . getMasterMid ,
48
12
49
13
normalizeBundlesMap = function ( bundlesMap ) {
50
14
var result = { } ;
@@ -56,26 +20,6 @@ define(['./i18n/common', "module", "require"], function (common, module, require
56
20
return result ;
57
21
} ,
58
22
59
- // Use nlsRegExp to parse the resource mid and return a usable object.
60
- parseName = function ( name ) {
61
- var match = nlsRegExp . exec ( name ) ;
62
-
63
- // If match[5] is blank, it means this is the top bundle definition,
64
- // hence suffix is match[4] and locale is null.
65
- return {
66
- prefix : match [ 1 ] ,
67
- masterLocale : "root" ,
68
- requestedLocale : match [ 5 ] ? match [ 4 ] : null ,
69
- suffix : match [ 5 ] || match [ 4 ]
70
- } ;
71
- } ,
72
-
73
- getMasterMid = function ( name ) {
74
- return name . masterLocale === "root" ? name . prefix + name . suffix :
75
- name . prefix + name . masterLocale + "/" + name . suffix ;
76
- } ,
77
-
78
-
79
23
// Transform a bundle from a language layer to a root bundle.
80
24
rootify = function ( bundle , locale ) {
81
25
var result = { } ;
@@ -189,7 +133,7 @@ define(['./i18n/common', "module", "require"], function (common, module, require
189
133
mixin ( moduleConfig , typeof module . config === "function" ? module . config ( ) || { } : { } ) ;
190
134
191
135
if ( config . isBuild ) {
192
- confLocalesList = config . localesList ;
136
+ localesList = config . localesList ;
193
137
onLoad ( ) ;
194
138
return ;
195
139
}
@@ -236,141 +180,34 @@ define(['./i18n/common', "module", "require"], function (common, module, require
236
180
} ,
237
181
/* jshint +W074 */
238
182
239
- write : function ( pluginName , moduleName ) {
240
- pluginMid = pluginMid || pluginName ;
241
- bundlesList . push ( moduleName ) ;
242
- } ,
243
-
244
- onLayerEnd : function ( write , data ) {
245
- var i18nConf ,
246
- layersContent = { } ,
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
- } ,
183
+ write : function ( pluginName , moduleName , write ) {
184
+ var name = parseName ( moduleName ) ,
185
+ bundle ;
275
186
276
- resetPlugin = function ( ) {
277
- bundlesList = [ ] ;
278
- confLocalesList = undefined ;
279
- pluginMid = undefined ;
280
- } ;
281
-
282
- if ( ! data . name ) {
283
- resetPlugin ( ) ;
284
- return ;
187
+ if ( name . requestedLocale ) {
188
+ bundle = build . resolveSync ( name . requestedLocale , name ) ;
189
+ if ( name . requestedLocale !== "root" ) {
190
+ bundle . _pseudoRoot = { } ;
191
+ bundle . _flattened = true ;
192
+ }
193
+ write . asModule ( moduleName , "define(" + JSON . stringify ( bundle ) + ")" ) ;
194
+ } else {
195
+ build . addBundleToNlsLayer ( name ) ;
285
196
}
197
+ } ,
286
198
287
- // Run through every bundles to know which locales are available for each module.
288
- init ( ) ;
289
-
290
- // calculate the nls layer for each locale
291
- bundlesList . forEach ( function ( bundle ) {
292
- var name = parseName ( bundle ) ,
293
- normalizeRoot = function ( bundle , name ) {
294
- bundle . root = ( bundle . root === true || bundle . root === 1 ) ?
295
- requirejs ( name . prefix + "root/" + name . suffix ) : bundle . root ;
296
- return bundle ;
297
- } ,
298
- root = normalizeRoot ( requirejs ( bundle ) , name ) ;
299
- // TODO: if name.requestedLocale a different optimization is possible
300
-
301
- localesList . forEach ( function ( loc ) {
302
- var result = { } ,
303
- localizedBundle ,
304
- locale = loc ;
305
-
306
- layersContent [ loc ] = layersContent [ loc ] || "" ;
307
-
308
- if ( loc !== "root" ) {
309
- while ( locale && locale !== "root" ) {
310
- if ( root [ locale ] ) {
311
- localizedBundle = requirejs ( name . prefix + locale + "/" + name . suffix ) ;
312
- mixin ( result , localizedBundle ) ;
313
- }
314
- locale = common . getParentLocale ( locale ) ;
315
- }
316
- localizedBundle = root . root ;
317
- mixin ( result , localizedBundle ) ;
318
-
319
- result . _flattened = true ;
320
- result . _pseudoRoot = pseudoRoots [ bundle ] [ loc ] || { } ;
321
- } else {
322
- mixin ( result , root ) ;
323
- }
324
-
325
- layersContent [ loc ] += 'define("' + name . prefix + loc + "/" +
326
- name . suffix + '",' + JSON . stringify ( result ) + ");" ;
327
- } ) ;
328
- } ) ;
329
-
330
- // This assumes nodejs
331
- eachProp ( layersContent , function ( loc , content ) {
332
- var fs = require ( "fs" ) ,
333
- createPath = function ( path ) {
334
- var parts = path . split ( "/" ) ,
335
- tmp = [ ] ;
336
-
337
- while ( ! fs . existsSync ( parts . join ( "/" ) + "/" ) ) {
338
- tmp . push ( parts . pop ( ) ) ;
339
- }
340
- while ( tmp . length ) {
341
- parts . push ( tmp . pop ( ) ) ;
342
- fs . mkdirSync ( parts . join ( "/" ) ) ;
343
- }
344
- } ,
345
-
346
- match = data . path . match ( / ^ ( .* \/ ) ( .* ) ( \. j s ) $ / ) ,
347
- dir = match [ 1 ] + "nls/" ,
348
- filename = match [ 2 ] + "_" + loc + ".js" ,
349
- mid = layerMid + "_" + loc ;
350
-
351
- content += "define('" + mid + "', true);" ;
352
-
353
- // Create path
354
- createPath ( dir ) ;
355
- fs . writeFileSync ( dir + filename , content ) ;
356
- } ) ;
357
-
199
+ onLayerEnd : function ( write , data ) {
200
+ if ( data . name && data . path ) {
201
+ var layersContent ;
358
202
359
- // Init config structure
360
- i18nConf = {
361
- config : { }
362
- } ;
363
- i18nConf . config [ pluginMid ] = {
364
- bundlesMap : { } ,
365
- localesMap : { }
366
- } ;
367
- i18nConf . config [ pluginMid ] . bundlesMap [ layerMid ] = bundlesList ;
368
- i18nConf . config [ pluginMid ] . localesMap [ layerMid ] = localesList ;
203
+ build . setLocalesList ( localesList ) ;
369
204
370
- // write i18n config on the layer
371
- write ( "require.config(" + JSON . stringify ( i18nConf ) + ")" ) ;
205
+ layersContent = build . getLayersContent ( ) ;
372
206
373
- resetPlugin ( ) ;
207
+ build . writeLayers ( layersContent , data ) ;
208
+ build . writeConfig ( module . id , data , write ) ;
209
+ }
210
+ build . reset ( ) ;
374
211
}
375
212
} ;
376
213
} ) ;
0 commit comments