/
translator.js
49 lines (40 loc) · 1.09 KB
/
translator.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.translate = translate;
var alt_search = function alt_search(locale, path) {
return locale && path.length ? alt_search(locale[path.shift()], path) : locale;
};
var search = function search(locale, path) {
if (Array.isArray(path)){return alt_search(locale, path)
}
var valor = locale[path];
return locale && path.length ? valor : locale;
};
var replace = function replace(entry, repls) {
if (!repls) return entry;
return entry.replace(/{(\w+)}/g, function (match, index) {
return repls[index] !== undefined ? repls[index] : match;
});
};
function translate(locales, lang, path, repls) {
if (!locales) return;
var entry = search(locales[lang], path.split('.'));
if (typeof entry === 'string') {
return replace(entry, repls);
}
else{
if (Array.isArray(entry)) {
var str= ""
for (var i = 0; i < entry.length; i++){
str+= entry[i]
}
var replaced = replace(str, repls)
if (typeof replaced === 'string') {
return entry
}
return replaced;
}
}
}