forked from openstreetmap/iD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_locales.js
326 lines (272 loc) · 10.9 KB
/
update_locales.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Downloads the latest translations from Transifex */
const fs = require('fs');
const prettyStringify = require('json-stringify-pretty-compact');
const request = require('request').defaults({ maxSockets: 1 });
const YAML = require('js-yaml');
const resources = ['core', 'presets', 'imagery', 'community'];
const outdir = './dist/locales/';
const api = 'https://www.transifex.com/api/2/';
const projectURL = api + 'project/id-editor/';
/*
* Transifex oddly doesn't allow anonymous downloading
*
* auth is stored in transifex.auth in a json object:
* {
* "user": "username",
* "pass": "password"
* }
* */
const auth = JSON.parse(fs.readFileSync('./transifex.auth', 'utf8'));
const sourceCore = YAML.load(fs.readFileSync('./data/core.yaml', 'utf8'));
const sourcePresets = YAML.load(fs.readFileSync('./data/presets.yaml', 'utf8'));
const sourceImagery = YAML.load(fs.readFileSync('./node_modules/editor-layer-index/i18n/en.yaml', 'utf8'));
const sourceCommunity = YAML.load(fs.readFileSync('./node_modules/osm-community-index/i18n/en.yaml', 'utf8'));
const cldrMainDir = './node_modules/cldr-localenames-full/main/';
var referencedScripts = [];
const languageInfo = {
dataLanguages: getLangNamesInNativeLang()
};
fs.writeFileSync('data/languages.json', JSON.stringify(languageInfo, null, 4));
asyncMap(resources, getResource, function(err, results) {
if (err) return console.log(err);
// merge in strings fetched from transifex
var allStrings = {};
results.forEach(function(resourceStrings) {
Object.keys(resourceStrings).forEach(function(code) {
if (!allStrings[code]) { allStrings[code] = {}; }
var source = resourceStrings[code];
var target = allStrings[code];
Object.keys(source).forEach(function(k) { target[k] = source[k]; });
});
});
// write files and fetch language info for each locale
var dataLocales = {
en: { rtl: false, languageNames: languageNamesInLanguageOf('en'), scriptNames: scriptNamesInLanguageOf('en') }
};
asyncMap(Object.keys(allStrings),
function(code, done) {
if (code === 'en' || !Object.keys(allStrings[code]).length) {
done();
} else {
var obj = {};
obj[code] = allStrings[code];
fs.writeFileSync(outdir + code + '.json', JSON.stringify(obj, null, 4));
getLanguageInfo(code, function(err, info) {
var rtl = info && info.rtl;
// exceptions: see #4783
if (code === 'ckb') {
rtl = true;
} else if (code === 'ku') {
rtl = false;
}
dataLocales[code] = {
rtl: rtl,
languageNames: languageNamesInLanguageOf(code) || {},
scriptNames: scriptNamesInLanguageOf(code) || {}
};
done();
});
}
}, function(err) {
if (!err) {
const keys = Object.keys(dataLocales).sort();
var sorted = {};
keys.forEach(function (k) { sorted[k] = dataLocales[k]; });
fs.writeFileSync('data/locales.json', prettyStringify({ dataLocales: sorted }, { maxLength: 99999 }));
}
}
);
});
function getResource(resource, callback) {
var resourceURL = projectURL + 'resource/' + resource + '/';
getLanguages(resourceURL, function(err, codes) {
if (err) return callback(err);
asyncMap(codes, getLanguage(resourceURL), function(err, results) {
if (err) return callback(err);
var locale = {};
results.forEach(function(result, i) {
if (resource === 'community' && Object.keys(result).length) {
locale[codes[i]] = { community: result }; // add namespace
} else {
if (resource === 'presets') {
// remove terms that were not really translated
var presets = (result.presets && result.presets.presets) || {};
for (const key of Object.keys(presets)) {
var preset = presets[key];
if (!preset.terms) continue;
preset.terms = preset.terms.replace(/<.*>/, '').trim();
if (!preset.terms) {
delete preset.terms;
if (!Object.keys(preset).length) {
delete presets[key];
}
}
}
} else if (resource === 'fields') {
// remove terms that were not really translated
var fields = (result.presets && result.presets.fields) || {};
for (const key of Object.keys(fields)) {
var field = fields[key];
if (!field.terms) continue;
field.terms = field.terms.replace(/\[.*\]/, '').trim();
if (!field.terms) {
delete field.terms;
if (!Object.keys(preset).length) {
delete fields[key];
}
}
}
}
locale[codes[i]] = result;
}
});
callback(null, locale);
});
});
}
function getLanguage(resourceURL) {
return function(code, callback) {
code = code.replace(/-/g, '_');
var url = resourceURL + 'translation/' + code;
if (code === 'vi') { url += '?mode=reviewed'; }
request.get(url, { auth : auth }, function(err, resp, body) {
if (err) return callback(err);
console.log(resp.statusCode + ': ' + url);
var content = JSON.parse(body).content;
callback(null, YAML.safeLoad(content)[code]);
});
};
}
function getLanguageInfo(code, callback) {
code = code.replace(/-/g, '_');
var url = api + 'language/' + code;
request.get(url, { auth : auth }, function(err, resp, body) {
if (err) return callback(err);
console.log(resp.statusCode + ': ' + url);
callback(null, JSON.parse(body));
});
}
function getLanguages(resource, callback) {
var url = resource + '?details';
request.get(url, { auth: auth },
function(err, resp, body) {
if (err) return callback(err);
console.log(resp.statusCode + ': ' + url);
callback(null, JSON.parse(body).available_languages
.map(function(d) { return d.code.replace(/_/g, '-'); })
.filter(function(d) { return d !== 'en'; })
);
});
}
function asyncMap(inputs, func, callback) {
var index = 0;
var remaining = inputs.length;
var results = [];
var error;
next();
function next() {
callFunc(index++);
if (index < inputs.length) {
setTimeout(next, 200);
}
}
function callFunc(i) {
var d = inputs[i];
func(d, function done(err, data) {
if (err) error = err;
results[i] = data;
remaining--;
if (!remaining) callback(error, results);
});
}
}
function getLangNamesInNativeLang() {
// manually add languages we want that aren't in CLDR
var unordered = {
'ja-Hira': {
base: 'ja',
script: 'Hira'
},
'ja-Latn': {
base: 'ja',
script: 'Latn'
},
'ko-Latn': {
base: 'ko',
script: 'Latn'
},
'zh_pinyin': {
base: 'zh',
script: 'Latn'
}
};
var langDirectoryPaths = fs.readdirSync(cldrMainDir);
langDirectoryPaths.forEach(function(code) {
var languagesPath = cldrMainDir + code + '/languages.json';
//if (!fs.existsSync(languagesPath)) return;
var languageObj = JSON.parse(fs.readFileSync(languagesPath, 'utf8')).main[code];
var identity = languageObj.identity;
// skip locale-specific languages
if (identity.variant || identity.territory) return;
var info = {};
var script = identity.script;
if (script) {
referencedScripts.push(script);
info.base = identity.language;
info.script = script;
}
var nativeName = languageObj.localeDisplayNames.languages[code];
if (nativeName) {
info.nativeName = nativeName;
}
unordered[code] = info;
});
var ordered = {};
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
return ordered;
}
var rematchCodes = { 'ar-AA': 'ar', 'zh-CN': 'zh', 'zh-HK': 'zh-Hant-HK', 'zh-TW': 'zh-Hant', 'pt-BR': 'pt', 'pt': 'pt-PT' };
function languageNamesInLanguageOf(code) {
if (rematchCodes[code]) code = rematchCodes[code];
var languageFilePath = cldrMainDir + code + '/languages.json';
if (!fs.existsSync(languageFilePath)) {
return null;
}
var translatedLangsByCode = JSON.parse(fs.readFileSync(languageFilePath, 'utf8')).main[code].localeDisplayNames.languages;
// ignore codes for non-languages
for (var nonLangCode in { mis: true, mul: true, und: true, zxx: true }) {
delete translatedLangsByCode[nonLangCode];
}
for (var langCode in translatedLangsByCode) {
var altLongIndex = langCode.indexOf('-alt-long');
if (altLongIndex !== -1) {
// prefer long names (e.g. Chinese -> Mandarin Chinese)
var base = langCode.substring(0, altLongIndex);
translatedLangsByCode[base] = translatedLangsByCode[langCode];
}
if (langCode.includes('-alt-')) {
// remove alternative names
delete translatedLangsByCode[langCode];
} else if (langCode === translatedLangsByCode[langCode]) {
// no localized value available
delete translatedLangsByCode[langCode];
}
}
return translatedLangsByCode;
}
function scriptNamesInLanguageOf(code) {
if (rematchCodes[code]) code = rematchCodes[code];
var languageFilePath = cldrMainDir + code + '/scripts.json';
if (!fs.existsSync(languageFilePath)) {
return null;
}
var allTranslatedScriptsByCode = JSON.parse(fs.readFileSync(languageFilePath, 'utf8')).main[code].localeDisplayNames.scripts;
var translatedScripts = {};
referencedScripts.forEach(function(script) {
if (!allTranslatedScriptsByCode[script] || script === allTranslatedScriptsByCode[script]) return;
translatedScripts[script] = allTranslatedScriptsByCode[script];
});
return translatedScripts;
}