-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathlocale.js
103 lines (92 loc) · 3.01 KB
/
locale.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { global, save } from './vars.js';
let strings;
getString(global.settings.locale);
export function loc(key, variables) {
let string = strings[key];
if (!string) {
if (global.settings.expose){
console.error(`string ${key} not found`);
console.log(strings);
}
return key;
}
if (variables) {
if(variables instanceof Array) {
for (let i = 0; i < variables.length; i++){
let re = new RegExp(`%${i}(?!\\d)`, "g");
if(!re.exec(string)){
if (global.settings.expose){
console.error(`"%${i}" was not found in the string "${key}" to be replace by "${variables[i]}"`);
}
continue;
}
string = string.replace(re, variables[i]);
}
let re = new RegExp("%\\d+(?!\\d)", 'g');
const results = string.match(re);
if(results && global.settings.expose){
console.error(`${results} was found in the string, but there is no variables to make the replacement`);
}
}
else {
if (global.settings.expose){
console.error('"variables" need be a instance of "Array"');
}
}
}
return string;
}
function getString(locale) {
$.ajaxSetup({ async: false });
let defaultString;
$.getJSON("strings/strings.json", (data) => { defaultString = data; });
if (locale != "en-US"){
let localeString;
try {
$.getJSON(`strings/strings.${locale}.json`, (data) => { localeString = data; })
}
catch (e) {
console.error(e,e.stack);
}
const defSize = defaultString.length;
if (localeString) {
Object.assign(defaultString, localeString);
}
if(defaultString.length != defSize && global.settings.expose){
console.error(`string.${locale}.json has extra keys.`);
}
}
let string_pack = save.getItem('string_pack') || false;
if (string_pack && global.settings.sPackOn){
let themeString;
try {
themeString = JSON.parse(LZString.decompressFromUTF16(string_pack));
}
catch (e) {
console.error(e,e.stack);
}
const defSize = defaultString.length;
if (themeString) {
Object.assign(defaultString, themeString);
}
if (defaultString.length != defSize && global.settings.expose){
console.error(`string pack has extra keys.`);
}
}
$.ajaxSetup({ async: true });
strings = defaultString;
}
export const locales = {
'en-US': 'English (US)',
'es-ES': 'Spanish (ESP)',
'pt-BR': 'Português (BR)',
'de-DE': 'Deutsch',
'it-IT': 'Italiano',
'ru-RU': 'Русский',
'cs-CZ': 'Čeština',
'pl-PL': 'Polski',
'zh-CN': '简体中文',
'zh-TW': '繁體中文',
'ko-KR': '한국어',
'im-PL': 'Igpay-Atinlay'
};