-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtranslation.js
More file actions
51 lines (46 loc) · 1.42 KB
/
translation.js
File metadata and controls
51 lines (46 loc) · 1.42 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
import Translation from 'src/structures/constants/translation';
import clone from 'src/utils/clone';
import eventManager from 'src/utils/eventManager';
/** @type {Map<string, Translation[]>} */
const arrays = new Map();
// TODO: include base underscript.json in script so we always have something to show?
const translations = (async () => {
const response = await fetch(
'https://raw.githubusercontent.com/UCProjects/UnderScript/refs/heads/master/lang/underscript.json',
{
cache: 'default',
},
);
const data = await response.text();
const text = typeof GM_getResourceText === 'undefined' ?
data :
GM_getResourceText('underscript.json') || data;
return JSON.parse(text, function reviver(key, value) {
if (Array.isArray(value)) {
if (!arrays.has(key)) {
arrays.set(key, value.map(
(_, i) => new Translation(`${key}.${i + 1}`, { prefix: null }),
));
}
value.forEach((val, i) => {
this[`${key}.${i + 1}`] = val;
});
return undefined;
}
return value;
});
})();
eventManager.on('translation:loaded', async () => {
await $.i18n().load(await translations);
eventManager.singleton.emit('translation:underscript');
});
export function getLength(key) {
return arrays.get(key)?.length ?? 0;
}
/**
* @param {string} key
* @returns {Translation[]}
*/
export function getTranslationArray(key) {
return clone(arrays.get(key)) ?? [];
}