forked from jamovi/jamovi-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
297 lines (246 loc) · 9.16 KB
/
index.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
#!/usr/bin/env node
'use strict';
const CLA = require('command-line-args');
const path = require('path');
const glob = require('glob');
const fs = require('fs-extra');
const po2json = require('po2json');
const gettextParser = require("gettext-parser");
const { GettextExtractor, JsExtractors } = require('gettext-extractor');
const utils = require('./utils');
const ARGS = [
{ name: 'build', type: String },
{ name: 'update', type: String },
{ name: 'create', type: String },
{ name: 'dest', type: String },
{ name: 'source', type: String },
{ name: 'pysource', type: String },
{ name: 'code', type: String }
];
const args = CLA(ARGS);
let usage = 'Usage:\n';
usage += ' j18n --build path/to/translations [--dest path]\n';
usage += '\n j18n --update path/to/translations --source glob --pysource pyglob\n';
usage += '\n j18n --create path/to/translations --code lang';
let sourceDir = '.';
if (args.build)
sourceDir = args.build;
else if (args.update)
sourceDir = args.update;
else if (args.create)
sourceDir = args.create;
if (args.build) {
let buildDir = '.';
if (args.dest)
buildDir = args.dest;
console.log('\nBuilding translation files.');
if (utils.exists(sourceDir) === false) {
console.log('\nNo translations can be found.');
return;
}
let sourcefiles = fs.readdirSync(sourceDir);
if (utils.exists(buildDir) === false)
fs.mkdirSync(buildDir);
console.log(`Searching ${sourceDir}...`);
let codes = [ ];
let codesInDev = [ ];
for (let file of sourcefiles) {
if (file.endsWith('.po') === false)
continue;
let poPath = path.join(sourceDir, file);
console.log(`found ${file}`);
let content = fs.readFileSync(poPath, { encoding: 'utf-8' });
if (content.includes('X-Status: hidden'))
continue;
let inDev = true;
if (content.includes('X-Status: production'))
inDev = false;
let translation = po2json.parse(content, { format: 'jed1.x' });
let code = translation.locale_data.messages[""].lang;
if (code === 'en')
codes.unshift('en')
else if (inDev)
codesInDev.push(code);
else
codes.push(code);
let buildFile = code + '.json';
fs.writeFileSync(path.join(buildDir, buildFile), JSON.stringify(translation, null, 4));
console.log(`wrote: ${buildFile}`);
}
if (codes.length > 0) {
if (codesInDev.length > 0)
codes = codes.concat(['---']).concat(codesInDev);
let manifest = { current: '', available: codes };
fs.writeFileSync(path.join(buildDir, 'manifest.json'), JSON.stringify(manifest, null, 4));
console.log('wrote: manifest.json');
}
else {
console.log('\nNo translation files found.');
}
}
else if (args.update) {
if ( ! args.source) {
console.log(usage);
return;
}
if ( ! args.pysource) {
console.log(usage);
return;
}
if (utils.exists(sourceDir) === false)
fs.mkdirSync(sourceDir);
let sourcefiles = fs.readdirSync(sourceDir);
let globPattern = args.source;
console.log('\nExtracting strings from js files...');
let extractor = new GettextExtractor();
extractor.createJsParser([
JsExtractors.callExpression('_', {
arguments: {
text: 0
}
}),
JsExtractors.callExpression('s_', {
arguments: {
text: 0
}
}),
JsExtractors.callExpression('n_', {
arguments: {
text: 0,
textPlural: 1
}
}),
JsExtractors.callExpression('_p', {
arguments: {
context: 0,
text: 1
}
})
])
.parseFilesGlob(globPattern);
console.log('Extracting strings from py files...');
let re = /[^a-zA-Z._]\_\('([^'\\]*(\\.[^'\\]*)*)'|[^a-zA-Z._]\_\("([^"\\]*(\\.[^"\\]*)*)"/g;
let pyGlobPattern = args.pysource; //"jamovi/server/jamovi/+(common|server)/**/*.py"
console.log(pyGlobPattern)
for (let pyFileName of glob.sync(pyGlobPattern)) {
let content = fs.readFileSync(pyFileName, 'UTF-8');
for (let match of content.matchAll(re)) {
let pieces = match.slice(1);
// when matching strings containing \\n, this regex has a
// duplicate final piece (not sure why)
// if the final piece begins with '\\n' it should be discarded
let last = pieces.slice(-1)[0];
if (last && last.startsWith('\\n'))
pieces.splice(-1);
// unescape newlines
let key = pieces.join('').replace(/\\n/g, '\n');
extractor.addMessage({
text: key,
references: [pyFileName],
comments: []
});
}
}
extractor.printStats();
let headers = {
"Language": "C",
"MIME-Version": "1.0",
"Content-Type": "text/plain; charset=UTF-8",
"Content-Transfer-Encoding": "8bit",
"Plural-Forms": "nplurals=2; plural=(n != 1);"
};
extractor.savePotFile(path.join(sourceDir, `catalog.pot`), headers);
console.log(`wrote catalog.pot`);
let pot = gettextParser.po.parse(fs.readFileSync(path.join(sourceDir, `catalog.pot`)));
let updatedMsgs = 0;
let addedMsgs = 0;
let removedMsgs = 0;
for (let file of sourcefiles) {
if (file.endsWith('.po') === false)
continue;
let poPath = path.join(sourceDir, file);
console.log(`found ${file}`);
let input = fs.readFileSync(poPath);
let po = gettextParser.po.parse(input);
//merge
for (let cName in pot.translations) {
let context = pot.translations[cName];
if (po.translations[cName] === undefined)
po.translations[cName] = context;
else {
let poContext = po.translations[cName];
for (let key in context) {
if (poContext[key] === undefined) {
addedMsgs += 1;
poContext[key] = context[key];
}
else {
let msg = poContext[key];
if (msg.msgid !== undefined && msg.msgid !== context[key].msgid) {
updatedMsgs += 1;
if (msg.msgstr)
msg.comments.previous = msg.msgid;
msg.msgid = context[key].msgid;
}
if (msg.msgid_plural !== undefined && msg.msgid_plural !== context[key].msgid_plural)
msg.msgid_plural = context[key].msgid_plural;
}
poContext[key]._inUse = true;
}
}
po.translations[cName]._inUse = true;
}
for (let cName in po.translations) {
let context = po.translations[cName];
if (context._inUse === undefined) {
console.log(`remove context ${cName}`);
delete po.translations[cName];
}
else {
delete context._inUse;
for (let key in context) {
let msg = context[key];
if (msg._inUse === undefined) {
removedMsgs += 1;
delete context[key];
}
else
delete msg._inUse;
}
}
}
if (removedMsgs > 0)
console.log(`Messages removed: ${removedMsgs}`);
if (updatedMsgs > 0)
console.log(`Messages updated: ${updatedMsgs}`);
if (addedMsgs > 0)
console.log(`Messages added: ${addedMsgs}`);
// merged
let output = gettextParser.po.compile(po, { foldLength: 77, sort: (a, b) => a.msgid.localeCompare(b.msgid) });
fs.writeFileSync(path.join(sourceDir, file), output);
}
}
else if (args.create) {
if ( ! args.code) {
console.log(usage);
return;
}
if (utils.exists(path.join(sourceDir, `catalog.pot`)) === false) {
console.log(`\nThe catalog.pot file needed to create a new translation file does not exist.\n\n To create a new catalog.pot use:\n\n j18n --update --source glob --pysoure pyglob [--home path]\n`);
return;
}
let code = args.code.toLowerCase();
if (utils.exists(path.join(sourceDir, `${code}.po`))) {
console.log(`\nTranslation file ${code}.po already exists.`);
return;
}
let input = fs.readFileSync(path.join(sourceDir, `catalog.pot`));
let pot = gettextParser.po.parse(input);
pot.headers.language = code;
let output = gettextParser.po.compile(pot, { foldLength: 77, sort: (a, b) => a.msgid.localeCompare(b.msgid) });
fs.writeFileSync(path.join(sourceDir, `${code}.po`), output);
console.log(`wrote ${code}.po`);
}
else {
console.log(usage);
}