-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
54 lines (42 loc) · 1.35 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Kamil Głód @kglod
*/
var loaderUtils = require('loader-utils');
var i18next = require('i18next');
var clc = require('cli-color');
// build RegExp for string like {'asd': "qwe"}
var ESCAPE = '[\'"`]?';
var WORD = '(\\w*)';
var jsonRegex = new RegExp(
[ESCAPE, WORD, ESCAPE, ':\\s', ESCAPE, WORD, ESCAPE].join(''),
['g']);
i18next.on('missingKey', function(lngs, namespace, key) {
console.log(clc.yellow(
'[' + lngs + '] key `' + namespace + ':' + key + '` missing'
));
});
module.exports = function(source) {
if (this.cacheable) {
this.cacheable();
}
var params = loaderUtils.parseQuery(this.query);
var funcName = params.funcName || '__';
var defaultQuotes = params.quotes || '\'';
var quotes = params.quotes || '\'';
var regex = new RegExp(funcName + '\\(([\'"`])(.*?)[\'"`](?:,\\s(.*?))?\\)', ['g']);
params.lng && i18next.changeLanguage(params.lng)
source = source.replace(regex, function (res, quotes, key, jsonString) {
if (jsonString) {
var options = {};
jsonString.replace(jsonRegex, function (res, key, value) {
options[key] = value;
});
} else {
var options = undefined;
}
quotes = (quotes == '`') ? '`' : defaultQuotes;
return quotes + i18next.t(key, options) + quotes;
});
return source;
}