forked from avast/vuei18n-po
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (201 loc) · 7.63 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
'use strict';
const glob = require('glob').sync;
const mkdir = require('make-dir').sync;
const path = require('path');
const fs = require('fs');
const po = require('pofile');
/**
* transform a set of gettext translation files into a JavaScript and JSON consumable by VueI18n.prototype.getChoiceIndex and messages
* @param options {
* po: input filename, glob, array of filenames or array of globs
* localeNameHeader: (optional) .po header that contains the name of a locale; if missing, locale name is taken from the file name
* pluralRules: (optional) output javascript file exporting an object { langCode1: (choice, choicesLength) => choiceIndex, langCode2: ... }
* messagesFile: (optional) single output JSON file containing all translations for all languages
* messagesDir: (optional) output directory containing a JSON file per language
* whiteList: (optional) glob of files that are tested for presence of the message keys; the unnused are filtered out
* }
* @return promise {
* langCode1: {
* messages: {
* key1: {
* subkey1: value
* }
* }
* }
* }
*/
async function main(options) {
if (!options || !options.po) {
throw new Error('missing input filename');
}
const inputPoFiles = glob2names(options.po);
if (inputPoFiles.length == 0) {
console.error('Warning: no files found at', Array.isArray(options.po) ? options.po.join(', ') : options.po);
}
const pos = await parseFiles(inputPoFiles, options.localeNameHeader);
if (options.whiteList) {
filterByWhitelist(options.whiteList, pos);
}
let json = {};
for (var lang in pos) {
json[lang] = {
messages: {},
plural: pos[lang].plural
};
let messages = json[lang].messages;
pos[lang].messages.forEach(item => {
let value;
if (!item.msgid_plural) {
value = item.msgstr[0] || item.msgid;
}
else {
if (item.msgstr.some(str => str.indexOf('|') != -1)) {
throw new Error('plural messages cannot contain | (see https://kazupon.github.io/vue-i18n/guide/pluralization.html#accessing-the-number-via-the-pre-defined-argument)');
}
value = (item.msgstr.some(str => str !== '') ?
item.msgstr.join(' | ') :
item.msgid + ' | ' + item.msgid_plural)
.replace(/%s/g, '{n}');
}
messages[item.msgctxt || item.msgid] = value;
});
}
if (options.messagesFile) {
if (path.basename(options.messagesFile) != options.messagesFile) {
mkdir(path.dirname(options.messagesFile));
}
let messages = {};
for (let lang in json) {
messages[lang] = json[lang].messages;
}
fs.writeFileSync(options.messagesFile, JSON.stringify(messages, null, 2), 'utf8');
}
if (options.pluralRules) {
if (path.basename(options.pluralRules) != options.pluralRules) {
mkdir(path.dirname(options.pluralRules));
}
let rules = `/* eslint-disable no-extra-semi */\n${options.es6 ? "export default" : "module.exports ="} {\n` +
Object.keys(json).map(lang => ' "' + lang + '": ' + json[lang].plural).join(',\n') +
'\n};\n';
fs.writeFileSync(options.pluralRules, rules, 'utf8');
}
if (options.messagesDir) {
mkdir(options.messagesDir);
for (let lang in json) {
fs.writeFileSync(path.join(options.messagesDir, lang + '.json'), JSON.stringify(json[lang].messages, null, 2), 'utf8');
}
}
return json;
}
function glob2names(patterns) {
return [].concat(
...((Array.isArray(patterns) ? patterns : [ patterns ])
.map(pattern => glob(pattern)))
);
}
function parseFiles(fileNames, localeNameHeader) {
return Promise.all(fileNames.map(fname => new Promise((resolve, reject) => {
po.load(fname, (err, data) => {
if (err) {
reject(err);
}
else {
try {
const name = (localeNameHeader && data.headers[localeNameHeader]) || path.basename(fname, path.extname(fname));
resolve({
[name]: {
messages: data.items,
...parsePlurals(data.headers['Plural-Forms'])
}
});
}
catch (ex) {
reject(ex);
}
}
});
})))
.then(values => values.reduce((obj, entry) => ({ ...obj, ...entry }), {}));
}
function parsePlurals(header) {
const match = header.match(/nplurals=([0-9]+).*plural=(.+)/); // the trailing \n is already removed by pofile
if (!match) {
throw new Error('Cannot parse plural definition: ' + header);
}
let pluralFunc = match[2];
return { plural: 'function (n) { const rv = ' + pluralFunc + '; return Number(rv); }' };
}
function filterByWhitelist(whitelistGlob, pos) {
let removalCandidates = new Set([]); // for messages filtering at the end
let removalRes = new Set([]); // for whitelist files processing
// preprocess the messages from *.po so that we can traverse the disk with whitelist files in one go
for (let lang in pos) {
pos[lang].messages.forEach(m => {
const msgctxt = m.msgctxt || m.msgid;
if (removalCandidates.has(msgctxt)) {
return;
}
if (!msgctxt) {
let meaningfulCopy = {};
for (let k in m) {
if (!!m[k] && (typeof m[k] !== 'object' || Object.keys(m[k]).length > 0))
meaningfulCopy[k] = m[k];
}
console.error('invalid entry in language', lang, ':', JSON.stringify(meaningfulCopy));
return;
}
removalCandidates.add(msgctxt);
let reEntry = { msgctxt: msgctxt };
if (msgctxt.indexOf('.') == -1) {
reEntry.re = new RegExp('[\'"`]' + msgctxt + '[\'"`]');
}
else {
// either one of the components ends with .', .", .` or .$ or the full key matches
// `namespace + '.rest.of.the.path'` will thus fail
// also, `foo.${something}.bar` will incorrectly let foo.xxx.yyy in
const components = msgctxt.split('.');
const reStr = '([\'"`]' + components.slice(1)
.reduce((prefixes, val, idx) => { // reduce returns an array of strings [ c0, c0[.]c1, c0[.]c1[.]c2, ... ]
prefixes.push(prefixes[idx] + '[.]' + val); // take the last entry in the array and create a new one, one component longer
return prefixes;
}, [ components[0] ])
.join('[.][\'"`$])|([\'"`]') + '[\'"`])'; // join the prefixes returned from reduce
// as alternatives <some quotation>c0.<some quotation> || <some quotation>c0.c1.<some quotation> || ... || <full key>
// example: "foo.bar.baz" turns into
// /(['"`]foo[.]['"`$])|(['"`]foo[.]bar[.]['"`$])|(['"`]foo[.]bar[.]baz['"`])/
// or, nicer
// ( ['"`]foo[.]['"`$] ) | ( ['"`]foo[.]bar[.]['"`$] ) | ( ['"`]foo[.]bar[.]baz['"`] )
reEntry.re = new RegExp(reStr);
}
removalRes.add(reEntry);
});
}
if (removalCandidates.size == 0) {
// unexpected, but possible
return;
}
// whitelist files traversal
const whitelist = glob(whitelistGlob);
whitelist.every(fname => {
try {
const text = fs.readFileSync(fname, 'utf8');
removalRes.forEach(reEntry => {
if (reEntry.re.test(text)) { // as soon as a key is found at least once, we don't need to test any more
removalCandidates.delete(reEntry.msgctxt);
removalRes.delete(reEntry);
}
});
return removalCandidates.size != 0; // all keys found, no need to process further
}
catch (ex) {
console.error(ex);
}
});
if (removalCandidates.size == 0) {
return;
}
for (let lang in pos) {
pos[lang].messages = pos[lang].messages.filter(m => !removalCandidates.has(m.msgctxt || m.msgid));
}
}
module.exports = main;