-
Notifications
You must be signed in to change notification settings - Fork 5
/
i18n.js
504 lines (411 loc) · 15.4 KB
/
i18n.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
'use strict';
const path = require('path');
const fs = require('fs');
const yaml = require('js-yaml');
const utils = require('./utils');
const po2json = require('po2json');
const { GettextExtractor, JsExtractors } = require('gettext-extractor');
const matchAll = require("match-all"); // str.matchAll() not available pre node 12
const translations = { };
const getTranslation = function(msg, context, code, source) {
let key = msg;
if (context)
key = context + '\u0004' + msg;
let value = translations[code].locale_data.messages[key];
if (translations[code]._data === undefined)
translations[code]._data = { };
if (value === undefined)
translations[code].locale_data.messages[key] = [''];
if (translations[code]._data[key] === undefined)
translations[code]._data[key] = { msg: msg, context: context, source: [ source ], _clash: [] };
let data = translations[code]._data[key];
if (data === undefined) {
data = { };
translations[code]._data[key] = data;
}
if (data.source === undefined)
data.source = [];
if (data.source.includes(source) === false)
data.source.push(source);
for (let k in translations[code]._data) {
let d = translations[code]._data[k];
if (k === key) {
if (d.source === undefined)
d.source = [];
if (d.source.includes(source) === false)
d.source.push(source);
return d;
}
else if (k.toLowerCase() === key.toLowerCase()) {
let found = false;
if (d._clash === undefined)
d._clash = [];
else {
for (let clash of d._clash) {
if (clash === key) {
found = true;
break;
}
}
}
if ( ! found)
d._clash.push(key);
}
}
return data;
};
const updateEntry = function(key, context, source) {
for (let code in translations) {
let pair = getTranslation(key, context, code, source);
pair._inUse = true;
}
};
const finalise = function(verbose) {
for (let code in translations) {
for (let k in translations[code].locale_data.messages) {
let data = translations[code]._data[k];
if (data && data._inUse) {
if (verbose && data._clash && data._clash.length > 0)
console.log(` !! TRANSLATION WARNING: '${data._clash.join(', ')}' and '${ data.msg }' have been added as seperate strings.`)
data._inUse = false;
}
else {
if (k !== '')
delete translations[code].locale_data.messages[k];
}
}
}
};
const canBeTranslated = function(value) {
if (value === '')
return false;
if (value === '.')
return false;
if (value === '-')
return false;
if (value === '$key')
return false;
if ( /^\([^\)]*\)$/g.test(value))
return false;
if (/^\$\{[^\}]*\}$/g.test(value))
return false;
return true;
}
const parseContext = function(value) {
let data = { key: value, context: null };
let match = value.match(/(.+) \[([^\[\]]+)\]$/);
if (match !== null) {
data.key = match[1];
data.context = match[2];
}
return data;
}
const extract = function(obj, address, filter) {
for (let property in obj) {
let include = !filter || filter.includes(property);
if (include) {
let value = obj[property];
if (typeof value === 'string') {
value = value.trim();
if (canBeTranslated(value)) {
value = parseContext(value);
if (Array.isArray(obj))
updateEntry(value.key, value.context, `${address}[${property}]`);
else
updateEntry(value.key, value.context, `${address}.${property}`);
}
}
else if (Array.isArray(value) || typeof value === 'object')
extract(value, `${address}.${property}`);
}
}
}
const extractDefaultValueStrings = function(item, itemAddress, defaultValue, basePath) {
if ( ! defaultValue)
return;
switch (item.type) {
case 'String':
let value = parseContext(defaultValue);
updateEntry(value.key, value.context, `${itemAddress}.${basePath}`);
break;
case 'Group':
for (let element of item.elements)
extractDefaultValueStrings(element, itemAddress, defaultValue[element.name], `${basePath}.${element.name}`);
break;
case 'Array':
for (let i = 0; i < defaultValue.length; i++)
extractDefaultValueStrings(item.template, itemAddress, defaultValue[i], `${basePath}[${i}]`);
break;
}
}
const checkItem = function(item, address, customFilter) {
if (customFilter === undefined)
customFilter = [];
if (Array.isArray(item)) {
for (let i = 0; i < item.length; i++) {
let child = item[i];
let childAddress = '';
if (child.name)
childAddress = `${address}/${child.name}`;
else
childAddress = `${address}[${i}]`;
checkItem(child, childAddress, customFilter);
}
}
else if (typeof item === 'object') {
let filter = ['label', 'title', 'description', 'addButton',
'ghostText', 'suffix', 'menuTitle', 'menuGroup',
'menuSubgroup', 'menuSubtitle', 'superTitle', 'content', 'notes', ...customFilter];
extract(item, address, filter);
extractDefaultValueStrings(item, address, item.default, 'default');
if (item.template)
checkItem(item.template, `${address}.template`);
if (item.columns) {
for (let column of item.columns)
checkItem(column, `${address}.columns`);
}
if (item.children)
checkItem(item.children, address);
if (item.items)
checkItem(item.items, address);
if (item.elements)
checkItem(item.elements, address);
if (item.options)
checkItem(item.options, address);
if (item.analyses)
checkItem(item.analyses, `${address}/analyses`);
if (item.datasets)
checkItem(item.datasets, `${address}/datasets`, ['name']);
}
}
const load = function(defDir, code, create) {
let transDir = path.join(defDir, 'i18n');
if (defDir.endsWith('i18n'))
transDir = defDir;
if ( ! utils.exists(transDir)) {
if (create)
fs.mkdirSync(transDir);
else
throw 'No translation files found.';
}
//if (code && create)
// translations[code] = { code: code, name: '', label: '', pluralForms: 'nplurals=2; plural=(n != 1)', _list: [], list: [] };
if (code && create) {
translations[code] = {
"domain": "messages",
"locale_data": {
"messages": {
"": {
"domain": "messages",
"plural_forms": 'nplurals=2; plural=(n != 1)',
"lang": code
}
}
},
_data: { },
code: code
}
}
let transfiles = fs.readdirSync(transDir);
if (create === false && transfiles.length === 0)
throw 'No translation files found.';
let translationLoaded = false;
for (let file of transfiles) {
if (file.endsWith('.po') === false && file.endsWith('.pot') === false)
continue;
if (code) {
if (translationLoaded)
break;
if (file.startsWith(code.toLowerCase()) === false)
continue;
else if (create) {
throw `Translation for language code ${code} already exists.`;
}
}
let langPath = path.join(transDir, file);
let translation = po2json.parseFileSync(langPath, { format: 'jed1.x' });
if (translation.locale_data.messages[""].lang !== '') {
if (! code || code === translation.locale_data.messages[""].lang) {
translation.code = translation.locale_data.messages[""].lang.toLowerCase();
translations[translation.code] = translation;
translationLoaded = true;
}
}
}
if (create === false && code && translationLoaded === false)
throw `No translation for language code ${code} found.
Try using: jmc --i18n path --create ${code}`;
return transDir;
}
const scanAnalyses = function(defDir, srcDir) {
console.log('Extracting strings from js files...');
let extractor = new GettextExtractor();
extractor.createJsParser([
JsExtractors.callExpression('_', {
arguments: {
text: 0
}
}),
JsExtractors.callExpression('n_', {
arguments: {
text: 0,
textPlural: 1
}
}),
JsExtractors.callExpression('_p', {
arguments: {
context: 0,
text: 1
}
})
])
.parseFilesGlob(`${defDir}/js/**/*.@(ts|js|tsx|jsx)`);
let items = extractor.getPofileItems();
for (let item of items) {
if (item.obsolete === false) {
for (let ref of item.references) {
ref = path.relative(srcDir, ref);
updateEntry(item.msgid, item.msgctxt, ref);
}
}
}
extractor.printStats();
console.log('Extracting strings from R files...');
let rDir = path.join(srcDir, 'R')
let rFiles = fs.readdirSync(rDir);
let re = /[^a-zA-Z._]\.\('([^'\\]*(\\.[^'\\]*)*)'|[^a-zA-Z._]\.\("([^"\\]*(\\.[^"\\]*)*)"/g;
for (let fileName of rFiles) {
if ( ! fileName.endsWith('.R') || fileName.endsWith('.h.R'))
continue;
let filePath = path.join(rDir, fileName);
let content = fs.readFileSync(filePath, 'UTF-8').replace(/\\u[0-9A-Fa-f]{4}/g, (x) => JSON.parse(`"${ x }"`));
// to support node < 12, we're using matchAll() rather than
// str.matchAll()
//
// for (let match of content.matchAll(re)) {
// let value = parseContext(match.slice(1).join(''));
// let rel = path.relative(srcDir, filePath);
// updateEntry(value.key, value.context, rel);
// }
for (let match of matchAll(content, re).toArray()) {
let value = parseContext(match);
let rel = path.relative(srcDir, filePath);
updateEntry(value.key, value.context, rel);
}
}
console.log('Extracting strings from yaml files...');
let files = fs.readdirSync(defDir);
for (let file of files) {
if (file === '0000.yaml') {
let packageInfoPath = path.join(defDir, '0000.yaml');
//let refsPath = path.join(defDir, '00refs.yaml');
let content = fs.readFileSync(packageInfoPath);
let packageInfo = yaml.load(content);
checkItem(packageInfo, `package`);
}
else if (file.endsWith('.a.yaml')) {
let analysisPath = path.join(defDir, file);
let basename = path.basename(analysisPath, '.a.yaml');
let resultsPath = path.join(defDir, basename + '.r.yaml');
let uiPath = path.join(defDir, basename + '.u.yaml');
let content = fs.readFileSync(analysisPath, 'utf-8');
let analysis = yaml.load(content);
checkItem(analysis, `${analysis.name}/options`);
if (utils.exists(uiPath)) {
let uiData = yaml.load(fs.readFileSync(uiPath));
checkItem(uiData, `${analysis.name}/ui`);
}
if (utils.exists(resultsPath)) {
let results = yaml.load(fs.readFileSync(resultsPath));
checkItem(results, `${analysis.name}/results`);
}
}
}
}
const escStr = function(str) {
if (typeof str !== 'string')
return str;
return str.replace(/"/g, '\\"').replace(/(\r\n|\n|\r)/gm, '\\n"\n"');
}
const saveAsPO = function(transDir) {
finalise(false);
for (let code in translations) {
let filename = code;
if (filename === 'c')
filename = 'catalog';
let transOutPath = null;
if (filename === 'catalog' )
transOutPath = path.join(transDir, `${filename}.pot`);
else
transOutPath = path.join(transDir, `${filename}.po`);
let poText = `msgid ""
msgstr ""
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"POT-Creation-Date: 2021-07-26 12:08:06+01000\\n"
"PO-Revision-Date: 2021-07-29 16:59:59+01000\\n"
"Language: ${code}\\n"
"Plural-Forms: ${translations[code].locale_data.messages[""].plural_forms}\\n"\n`;
let keys = Object.keys(translations[code].locale_data.messages).sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
let count = 0;
for (let key of keys) {
if (key === '')
continue;
let val = translations[code].locale_data.messages[key];
let data = translations[code]._data[key];
let poEntry = `\n`;
for (let source of data.source)
poEntry = `${poEntry}#: ${source}\n`;
if (data.context)
poEntry += `msgctxt "${escStr(data.context)}"\n`;
poEntry = `${poEntry}msgid "${escStr(data.msg)}"\n`;
poEntry += `msgstr "${val === null ? '' : escStr(val[0]) }"\n`;
poText = poText + poEntry;
count += 1;
}
fs.writeFileSync(transOutPath, poText);
console.log('wrote: ' + `${count} unique strings found`);
console.log('wrote: ' + path.basename(transOutPath));
}
}
const create = function(code, defDir, srcDir, verbose, list) {
let transDir = load(defDir, code, true);
scanAnalyses(defDir, srcDir);
saveAsPO(transDir, verbose);
}
const update = function(code, defDir, srcDir, verbose, list) {
let transDir = load(defDir, code, false);
scanAnalyses(defDir, srcDir);
saveAsPO(transDir, verbose);
};
const list = function(defDir) {
let transDir = path.join(defDir, 'i18n');
if ( ! utils.exists(transDir)) {
console.log('No translation files found.');
return;
}
let transfiles = fs.readdirSync(transDir);
if (create === false && transfiles.length === 0) {
console.log('No translation files found.');
return;
}
if (transfiles.length === 1)
console.log(` ${transfiles.length} language code file was found:`);
else
console.log(` ${transfiles.length} language code files were found:`);
console.log('');
for (let file of transfiles)
console.log(` ${file}`);
console.log('');
console.log('');
console.log(`To update a specific language code file use:
jmc --i18n path --update code
To update all language code files use:
jmc --i18n path --update
To create a new language code file use:
jmc --i18n path --create code`);
}
module.exports = { create, update, load, finalise, list, translations };