forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalize.js
More file actions
45 lines (39 loc) · 1.3 KB
/
localize.js
File metadata and controls
45 lines (39 loc) · 1.3 KB
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
const texts_json = require('../../autogenerated/texts').texts_json;
const template = require('./utility').template;
const moment = require('moment');
const Localize = (function () {
const texts = {};
let localizedTexts;
const init = function () {
// make texts objects localizable
Object.keys(texts_json).forEach(function(key) {
texts[key] = texts_json[key] ? texts_json[key] : {};
});
};
const localizeForLang = function(lang) {
init();
localizedTexts = texts[lang];
moment.locale(lang.toLowerCase());
};
const do_localize = function(text, params) {
const index = text.replace(/[\s|.]/g, '_');
text = (localizedTexts && localizedTexts[index]) || text;
// only do templating when explicitly required
return params ? template(text, params) : text;
};
const localize = function (text, params) {
if (Array.isArray(text)) {
return text.map(function(t) { return do_localize(t, params); });
}
// else
return do_localize(text, params);
};
return {
localizeForLang: localizeForLang,
localize : localize,
};
})();
module.exports = {
localize : Localize.localize,
localizeForLang: Localize.localizeForLang,
};