-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-potfile.js
293 lines (235 loc) · 9.2 KB
/
generate-potfile.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
const fs = require('fs');
const PO = require('pofile');
const esprima = require('esprima');
const { findTranslationCalls, contract, shave } = require('./localization-util');
const PEBBLE_FILES = [
'resources/public/pebble_templates/index.html',
'resources/public/pebble_templates/faq.html',
'resources/public/pebble_templates/info.html',
'resources/public/pebble_templates/profile.html',
'resources/public/pebble_templates/40x.html'
];
const PEBBLE_COMMENT_MATCH = /{([#])((?!\1}).)*\1}/g;
const PEBBLE_CODE_MATCH = /{([%])((?!\1}).)*\1}|{{((?!}}).)*}}/g;
const I18N_MATCH = /i18n\s*[(]/g;
// a string matching regex, handles escapes and escaped escapes.
const STRING_MATCH = /(["'])((\\{2})*|(.*?[^\\](\\{2})*))\1/g;
const poFiles = new Map();
let stringCount = 0;
const offsetToLine = (text, offset) => Array.from(text.slice(0, offset).matchAll('\n')).length;
const globalOffsetToLocal = (text, offset) => offset - text.slice(0, offset).lastIndexOf('\n');
const itemsByIdByPofile = new Map();
function createPoFile(name) {
const poFile = new PO();
poFile.headers['Project-Id-Version'] = 'Pxls';
poFile.headers['POT-Creation-Date'] = (new Date()).toISOString();
// "Better written as…" no it's not - look at the context.
/* eslint-disable-next-line dot-notation */
poFile.headers['Language'] = '';
poFile.headers['Content-Type'] = 'text/plain; charset=UTF-8';
itemsByIdByPofile.set(name, new Map());
poFiles.set(name, poFile);
}
for (const path of PEBBLE_FILES) {
const file = fs.readFileSync(path).toString();
const commentsByLine = new Map();
const comments = file.matchAll(PEBBLE_COMMENT_MATCH);
for (const comment of comments) {
const line = offsetToLine(file, comment.index);
if (!commentsByLine.has(line)) {
commentsByLine.set(line, []);
}
commentsByLine.get(line).push(shave(comment[0], 2));
}
const sections = file.matchAll(PEBBLE_CODE_MATCH);
for (const section of sections) {
const sectionOffset = section.index;
const sectionLine = offsetToLine(file, section.index);
const gettextCalls = section[0].matchAll(I18N_MATCH);
for (const call of gettextCalls) {
const callOffset = sectionOffset + call.index;
const callLine = sectionLine + offsetToLine(section[0], call.index);
const callLocalOffset = globalOffsetToLocal(file, callOffset);
const params = [];
let depth = 1;
let callLength;
for (let i = call.index + 5; i < section[0].length; i++) {
let char = section[0][i];
if (char === '\'' || char === '"') {
const match = section[0].slice(i - 1).matchAll(STRING_MATCH).next();
if (match.done) {
throw new Error(`Template file is incorrect (unterminated string) at ${callLine}:${callLocalOffset}`);
}
i += match.value[0].length;
params.push(contract(match.value[0], 1).replaceAll('\\\'', '\''));
char = section[0][i];
}
if (char === '(') {
depth++;
} else if (char === ')') {
depth--;
if (depth === 0) {
// Since we can calculate this, it's worth having
// even if we don't currently use it.
/* eslint-disable-next-line no-unused-vars */
callLength = i;
break;
}
}
}
const [poFileName, id] = params;
if (!poFiles.has(poFileName)) {
createPoFile(poFileName);
}
const poFile = poFiles.get(poFileName);
const itemsById = itemsByIdByPofile.get(poFileName);
if (!itemsById.has(id)) {
stringCount++;
const item = new PO.Item();
itemsById.set(id, item);
poFile.items.push(item);
}
const item = itemsById.get(id);
item.msgid = id;
// push reference only if unique
if (item.references.indexOf(path) === -1) {
// NOTE ([ ]): linenumber and position are cool to have, but they cause
// too much noise in localization updates.
// item.references.push(`${path}:${callLine + 1}:${callLocalOffset + 1}`);
item.references.push(path);
}
if (commentsByLine.has(callLine - 1)) {
item.extractedComments.push(...commentsByLine.get(callLine - 1));
}
if (commentsByLine.has(callLine)) {
item.extractedComments.push(...commentsByLine.get(callLine));
}
}
}
}
// TODO: maybe just use ttag instead?
const JS_FILES = [
'resources/public/pxls.js',
'resources/public/include/ban.js',
'resources/public/include/board.js',
'resources/public/include/chat.js',
'resources/public/include/chromeOffsetWorkaround.js',
'resources/public/include/coords.js',
'resources/public/include/grid.js',
'resources/public/include/helpers.js',
'resources/public/include/lookup.js',
'resources/public/include/modal.js',
'resources/public/include/nativeNotifications.js',
'resources/public/include/notifications.js',
'resources/public/include/overlays.js',
'resources/public/include/panels.js',
'resources/public/include/place.js',
'resources/public/include/query.js',
'resources/public/include/serviceworkers.js',
'resources/public/include/settings.js',
'resources/public/include/socket.js',
'resources/public/include/storage.js',
'resources/public/include/template.js',
'resources/public/include/timer.js',
'resources/public/include/typeahead.js',
'resources/public/include/uiHelper.js',
'resources/public/include/user.js',
'resources/public/admin/admin.js'
];
const JS_POFILE = 'Localization';
if (!poFiles.has(JS_POFILE)) {
createPoFile(JS_POFILE);
}
const jsPoFile = poFiles.get(JS_POFILE);
const jsItemsById = itemsByIdByPofile.get(JS_POFILE);
const TRANSLATOR_COMMENT_REGEX = /^\s*translator:\s?(.*)$/i;
for (const path of JS_FILES) {
const file = fs.readFileSync(path).toString();
const script = esprima.parseScript(file, { range: true, comment: true });
const translatableStrings = script.body
.map(findTranslationCalls)
.flat()
.map(e => e.arguments[0].range);
for (const [start, end] of translatableStrings) {
const relevantComments = script.comments.filter(comment => {
const [commentStart, commentEnd] = comment.range;
if (!TRANSLATOR_COMMENT_REGEX.test(comment.value)) {
return false;
}
if (commentEnd < start) {
// before string
const newlineCount = Array.from(file
.substring(commentEnd, start)
.matchAll('\n')).length;
return newlineCount < 2;
} else {
// after string
const hasNewline = file
.substring(end, commentStart)
.indexOf('\n') !== -1;
return !hasNewline;
}
}).map(comment => TRANSLATOR_COMMENT_REGEX.exec(comment.value)[1]);
const id = contract(file.substring(start, end), 1);
const callLine = offsetToLine(file, start);
const callLocalOffset = globalOffsetToLocal(file, start);
if (!jsItemsById.has(id)) {
stringCount++;
const item = new PO.Item();
jsItemsById.set(id, item);
jsPoFile.items.push(item);
}
const item = jsItemsById.get(id);
item.msgid = id;
// push reference only if unique
if (item.references.indexOf(path) === -1) {
// NOTE (Flying): The below logic will "double up" references for
// JavaScript files. For example:
//
// #: resources/public/include/chat.js
// #: resources/public/include/user.js
// #: resources/public/admin/admin.js
//
// will become:
//
// #: resources/public/include/chat.js resources/public/include/user.js
// #: resources/public/admin/admin.js
//
// This is to remain consistent with how .po files are structured.
// Copy/pasting them one-per-line into a .po file and running the
// compile-localizations.js script will cause the output .properties file
// to break like so:
//
// #: resources/public/include/chat.js
// resources/public/include/user.js
// #: resources/public/admin/admin.js
//
// This should make it cleaner to create new localization files by
// copying Localization.pot.
// find last index of references ending in .js
const lastJs = item.references.indexOf(item.references.filter(r => r.endsWith('.js')).reverse()[0]);
// if the path doesn't end in .js, there are no .js files yet, or the
// last .js file has a space in it (already "doubled up"), push like
// normal and continue
if (path.endsWith('.js') && lastJs !== -1 && !item.references[lastJs].includes(' ')) {
item.references[lastJs] += ' ' + path;
} else {
// see note on html reference extraction.
// item.references.push(`${path}:${callLine + 1}:${callLocalOffset + 1}`);
item.references.push(path);
}
}
for (const comment of relevantComments) {
item.extractedComments.push(comment);
}
}
// clear the strings array
// because none of this code is well-structured
translatableStrings.splice(0);
}
for (const [name, poFile] of poFiles.entries()) {
poFile.save(`po/${name}.pot`, e => e ? console.error : null);
}
console.info(`Parsed ${PEBBLE_FILES.length + JS_FILES.length} files.`);
console.info(`${stringCount} strings found.`);
console.info(`Output ${poFiles.size} files.`);