-
Notifications
You must be signed in to change notification settings - Fork 7
/
I18N.js
67 lines (55 loc) · 1.87 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
const Defaults = {
collatorOptions: {},
numberFormatterOptions: {
maximumFractionDigits: 10
},
dateFormatterOptions: {
day: 'numeric',
month: 'numeric',
year: 'numeric'
},
dateTimeFormatterOptions: {
day: 'numeric',
month: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZone: "UTC",
timeZoneName: "short"
}
};
const I18N = {
locales: [],
collator: new Intl.Collator(Defaults.collatorOptions),
dateFormatter: new Intl.DateTimeFormat(Defaults.dateFormatterOptions),
dateTimeFormatter: new Intl.DateTimeFormat(Defaults.dateTimeFormatterOptions),
numberFormatter: new Intl.NumberFormat(Defaults.numberFormatterOptions),
translate: null, // function(value: string, vars: array|object = null) : string
getDefaults() {
return Defaults;
},
setLocales(locales, dateFormatterOptions = {}, dateTimeFormatterOptions = {}, numberFormatterOptions = {}, collatorOptions = {}) {
this.locales = locales;
collatorOptions = Object.assign({}, Defaults.collatorOptions, collatorOptions);
this.collator = new Intl.Collator(locales, collatorOptions);
dateFormatterOptions = Object.assign({}, Defaults.dateFormatterOptions, dateFormatterOptions);
this.dateFormatter = new Intl.DateTimeFormat(locales, dateFormatterOptions);
dateTimeFormatterOptions = Object.assign({}, Defaults.dateTimeFormatterOptions, dateTimeFormatterOptions);
this.dateTimeFormatter = new Intl.DateTimeFormat(locales, dateTimeFormatterOptions);
numberFormatterOptions = Object.assign({}, Defaults.numberFormatterOptions, numberFormatterOptions);
this.numberFormatter = new Intl.NumberFormat(locales, numberFormatterOptions);
},
setTranslator(fn) {
this.translate = fn;
},
format(value, vars = null) {
if (vars) {
for(let key in vars) {
value = value.replaceAll(`{${key}}`, vars[key]);
}
}
return value;
}
};
module.exports = I18N;