This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
babelbox.js
74 lines (66 loc) · 1.94 KB
/
babelbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
( function() {
/**
* Config Options:
folders: Allows languages to be added from a directory
For example:
req('babelbox!./i18n')
./i18n/en.json
./i18n/en_GB.json
./i18n/de.json
fileseperator: Which seperator to use to build paths to json files
For example:
req('babelbox!./i18n')
./i18n-en.json
./i18n-en_GB.json
./i18n-de.json
*/
//Not using requirejs normalise api since it isn't async
function normalise( name, locale, config ) {
var normalisedName = name;
if( config.folders ) {
normalisedName = name + '/' + locale;
} else if( config.fileseperator ) {
normalisedName = name + config.fileseperator + locale;
} else {
normalisedName = name + '_' + locale;
}
return 'text!' + normalisedName + '.json';
}
var loadedPaths = [];
//Main module definition.
define( {
load: function( name, req, onload, config ) {
// Do not bother with the work if a build
if( config && config.isBuild ) {
onload();
return;
}
/**
* Only load an i18n once
*/
if( loadedPaths.indexOf( name ) > -1 ) {
onload();
return;
}
loadedPaths.push( name );
var babelboxconfig = config.babelbox || {};
var babelboxpath = babelboxconfig.babelboxpath || 'i18n';
var localeseperator = babelboxconfig.localeseperator || '-';
var localedepth = typeof babelboxconfig.localedepth !== 'undefined' ? babelboxconfig.localedepth : 1;
req( [ babelboxpath ], function( babelbox ) {
var locale = babelbox.getLocale();
var requires = [];
for( var i = 1; i <= locale.length && i < localedepth + 1; i++ ) {
requires.push( normalise( name, locale.slice( 0, i ).join( localeseperator ), babelboxconfig ) );
}
req( requires, function() {
babelbox.addTokens( JSON.parse( arguments[ 0 ] ) );
for( var i = 1; i < arguments.length; ++i ) {
babelbox.addExtendedTokens( JSON.parse( arguments[ i ] ) );
}
onload();
} );
} );
}
} );
}() );