-
Notifications
You must be signed in to change notification settings - Fork 7
/
i18n.js
129 lines (126 loc) · 4.78 KB
/
i18n.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var EOL = require('os').EOL,
enb = require('enb'),
buildFlow = enb.buildFlow || require('enb/lib/build-flow'),
keysets = require('../lib/keysets'),
compile = require('../lib/compile');
/**
* @class I18nTech
* @augments {BaseTech}
* @classdesc
*
* Builds `?.lang.{lang}.js` from `?.keysets.{lang}.js` bundle files.<br/><br/>
*
* @param {Object} options Options.
* @param {String} [options.target='?.lang.{lang}.js'] Path to a target with compiled file.
* @param {String} options.lang Language identifier.
* @param {String} [options.exports={
* globals: true, commonJS: true, ym: true}] Export settings.
* @param {String} [options.keysetsFile='?.keysets.{lang}.js'] Path to a source keysets file.
*
* @example
* var BemhtmlTech = require('enb-bemhtml/techs/bemhtml'),
* FileProvideTech = require('enb/techs/file-provider'),
* Keysets = require('enb-bem-i18n/techs/keysets'),
* I18n = require('enb-bem-i18n/techs/i18n-js'),
* bemTechs = require('enb-bem-techs');
*
* module.exports = function(config) {
* config.node('bundle', function(node) {
* // get FileList
* node.addTechs([
* [FileProvideTech, { target: '?.bemdecl.js' }],
* [bemTechs.levels, { levels: ['blocks'] }],
* [bemTechs.deps],
* [bemTechs.files]
* ]);
*
* // collect and merge keysets files into bundle
* node.addTechs([
* [ Keysets, { lang: 'all' } ],
* [ Keysets, { lang: '{lang}' } ]
* ]);
*
* // make lang.{lang}.js bundle files from keysets bundle files
* node.addTechs([
* [ I18n, { lang: 'all' } ],
* [ I18n, { lang: '{lang}' } ]
* ]);
* });
* };
*/
module.exports = buildFlow.create()
.name('i18n')
.target('target', '?.lang.{lang}.js')
.defineRequiredOption('lang')
.defineOption('exports', {
globals: true,
commonJS: true,
ym: true
})
.useSourceFilename('keysetsFile', '?.keysets.{lang}.js')
.builder(function (keysetsFilename) {
return this._readKeysetsFile(keysetsFilename)
.then(function (sources) {
var parsed = keysets.parse(sources),
opts = {
version: parsed.version,
language: this._lang,
exports: this._exports
},
globals = opts.exports.globals,
forceGlobals = globals === 'force';
return [].concat(
[
forceGlobals ? 'var BEM;' : '',
'(function (global) {',
' var __i18n__ = ' + compile(parsed.core, parsed.keysets, opts) + ',',
' defineAsGlobal = true;',
''
],
opts.exports.commonJS ? [
' // CommonJS',
' if (typeof exports === "object") {',
' module.exports = __i18n__;',
forceGlobals ?
' defineAsGlobal = false;' : '',
' }',
''
] : '',
opts.exports.ym ? [
' // YModules',
' if (typeof modules === "object") {',
' modules.define("i18n", function (provide) {',
' provide(__i18n__);',
' });',
forceGlobals ?
' defineAsGlobal = false;' : '',
' }',
'',
] : '',
globals ? [
' if (defineAsGlobal) {',
forceGlobals ?
' (BEM || (BEM = {})).I18N = __i18n__;' : '',
' (global.BEM || (global.BEM = {})).I18N = __i18n__;',
' }'
] : '',
'})(typeof window !== "undefined" ? window : global);'
).join(EOL);
}, this);
})
.methods({
/**
* Reads file with keysets.
*
* @param {String} filename — path to file with keysets.
* @returns {Promise}
* @private
*/
_readKeysetsFile: function (filename) {
var node = this.node,
root = node.getRootDir(),
cache = node.getNodeCache(this._target);
return keysets.read(filename, cache, root);
}
})
.createTech();