-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n-fixer.js
216 lines (206 loc) · 3.43 KB
/
i18n-fixer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const fetch = require('node-fetch');
const path = require('path');
const fs = require('fs');
const DEFAULT_LANGUAGE = 'en';
const SUPPORTED_LANGUAGES = [
'af',
'sq',
'am',
'ar',
'hy',
'as',
'ay',
'az',
'bm',
'eu',
'be',
'bn',
'bho',
'bs',
'bg',
'ca',
'ceb',
'zh-CN',
'zh-TW',
'co',
'hr',
'cs',
'da',
'dv',
'doi',
'nl',
'en',
'eo',
'et',
'ee',
'fil',
'fi',
'fr',
'fy',
'gl',
'ka',
'de',
'el',
'gn',
'gu',
'ht',
'ha',
'haw',
'he',
'hi',
'hmn',
'hu',
'is',
'ig',
'ilo',
'id',
'ga',
'it',
'ja',
'jv',
'kn',
'kk',
'km',
'rw',
'gom',
'ko',
'kri',
'ku',
'ckb',
'ky',
'lo',
'la',
'lv',
'ln',
'lt',
'lg',
'lb',
'mk',
'mai',
'mg',
'ms',
'ml',
'mt',
'mi',
'mr',
'lus',
'mn',
'my',
'ne',
'no',
'ny',
'or',
'om',
'ps',
'fa',
'pl',
'pt',
'pa',
'qu',
'ro',
'ru',
'sm',
'sa',
'gd',
'nso',
'sr',
'st',
'sn',
'sd',
'si',
'sk',
'sl',
'so',
'es',
'su',
'sw',
'sv',
'tl',
'tg',
'ta',
'tt',
'te',
'th',
'ti',
'ts',
'tr',
'tk',
'ak',
'uk',
'ur',
'ug',
'uz',
'vi',
'cy',
'xh',
'yi',
'yo',
'zu',
];
const langsDir = __dirname + `${path.sep}custom${path.sep}/signedLanguages`;
const languages = {};
for (const language of SUPPORTED_LANGUAGES) {
languages[language] = {
filePath: langsDir + path.sep + language.toLowerCase() + '.json',
content: {},
};
}
// Load existing language files
for (const file of fs.readdirSync(langsDir)) {
const [lang] = file.split('.');
const filePath = langsDir + path.sep + file;
languages[lang] = {
filePath,
content: JSON.parse(String(fs.readFileSync(filePath))),
};
}
async function translate(text, sourceLanguage, targetLanguage) {
console.log('translate', { text, sourceLanguage, targetLanguage });
const requestStr = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLanguage}&tl=${targetLanguage}&dt=t&q=${encodeURIComponent(
text
)}`;
const req = await fetch(requestStr);
const json = await req.json();
return json[0][0][0];
}
async function translateMissingEntries(source, target, targetLanguage) {
for (const [key, value] of Object.entries(source)) {
if (!(key in target)) {
if (typeof value === 'string') {
target[key] = await translate(value, DEFAULT_LANGUAGE, targetLanguage);
} else {
target[key] = {};
await translateMissingEntries(value, target[key], targetLanguage);
}
} else {
if (typeof value !== 'string') {
await translateMissingEntries(value, target[key], targetLanguage);
}
}
}
}
function removeExtraEntries(source, target) {
for (const [key, value] of Object.entries(target)) {
if (!(key in source)) {
delete target[key];
} else {
if (typeof value !== 'string') {
removeExtraEntries(source[key], value);
}
}
}
}
async function main() {
// Iterate files and translate missing entries
for (const [lang, { content, filePath }] of Object.entries(languages)) {
if (lang !== DEFAULT_LANGUAGE) {
const source = languages[DEFAULT_LANGUAGE].content;
await translateMissingEntries(source, content, lang);
removeExtraEntries(source, content);
fs.writeFileSync(filePath, JSON.stringify(content, null, 2) + '\n');
}
}
}
main()
.then(() => process.exit(0))
.catch(() => process.exit(1));