forked from webpack-contrib/i18n-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
75 lines (56 loc) · 1.91 KB
/
index.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var ConstDependency = require("webpack/lib/dependencies/ConstDependency");
var NullFactory = require("webpack/lib/NullFactory");
var i18next = require('i18next');
var clc = require('cli-color');
i18next.on('missingKey', function(lngs, namespace, key, res) {
if (lngs !== 'basic') {
console.log(clc.yellow('[' + lngs + '] key `' + namespace + ':' + key + '` not found'));
}
});
function I18nPlugin(i18nOptions, lang, functionName, quotes) {
this.lang = lang;
this.functionName = functionName || '__';
this.quotes = quotes || '\'';
i18next.init(i18nOptions);
}
function extractArgs(arg) {
switch (arg.type) {
case 'Literal':
return arg.value;
case 'Identifier':
return arg.name;
case 'ObjectExpression':
var res = {};
for (i in arg.properties) {
res[extractArgs(arg.properties[i].key)] = extractArgs(arg.properties[i].value);
}
return res;
default:
console.log(clc.red('unable to parse arg ' + arg));
return '';
}
}
I18nPlugin.prototype.apply = function(compiler) {
var q = this.quotes;
var lang = this.lang;
compiler.plugin("compilation", function(compilation) {
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
});
compiler.parser.plugin("call " + this.functionName, function(expr) {
var args = expr.arguments.map(function(arg) {return extractArgs(arg)});
i18next.changeLanguage(lang, function (err, t) {
var value = q + t.apply(i18next, args) + q;
var dep = new ConstDependency(value, expr.range);
dep.loc = expr.loc;
this.state.current.addDependency(dep);
}.bind(this));
return true;
});
};
I18nPlugin.prototype.extractArgs = extractArgs;
module.exports = I18nPlugin;