forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.js
More file actions
59 lines (53 loc) · 1.47 KB
/
Copy pathi18n.js
File metadata and controls
59 lines (53 loc) · 1.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const i18n =
self.browser instanceof Object && self.browser instanceof Element === false
? self.browser.i18n
: self.chrome.i18n;
class i18nHandler {
/**
* @param {string | Date | number} str
*/
static toDate(str = '') {
return new Intl.DateTimeFormat(navigator.language).format(
typeof str === 'string' ? new Date(str) : str
);
}
/**
* @param {number | bigint} number
*/
static toNumber(number) {
return new Intl.NumberFormat(navigator.language).format(number);
}
/**
* @param {...string} args
*/
static i18n$(...args) {
return i18n.getMessage(...args);
}
}
const { i18n$, toDate, toNumber } = i18nHandler;
const language = { i18n$, toDate, toNumber };
const isBackgroundProcess = document && document.title === 'Magic UserJS+ Background Page';
if (isBackgroundProcess !== true) {
// Helper to deal with the i18n'ing of HTML files.
i18n.render = function (context) {
const docu = document;
const root = context || docu;
for (const elem of root.querySelectorAll('[data-i18n]')) {
const text = i18n$(elem.getAttribute('data-i18n'));
if (!text) {
continue;
}
elem.textContent = text;
}
for (const elem of root.querySelectorAll('[placeholder]')) {
const text = i18n$(elem.getAttribute('placeholder'));
if (text === '') {
continue;
}
elem.setAttribute('placeholder', text);
}
};
i18n.render();
}
export { i18n, i18n$, language };