forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi18n.js
303 lines (271 loc) · 9.63 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
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Gulp tasks for the extraction of translatable messages.
*/
import childProcess from 'child_process';
import fileExists from 'file-exists';
import gulp from 'gulp';
import cheerio from 'gulp-cheerio';
import freplace from 'gulp-findreplace';
import gulpUtil from 'gulp-util';
import xslt from 'gulp-xslt';
import jsesc from 'jsesc';
import path from 'path';
import q from 'q';
import regexpClone from 'regexp-clone';
import conf from './conf';
/**
* Extracts the translatable text messages for the given language key from the pre-compiled
* files under conf.paths.{serve|messagesForExtraction}.
* @param {string} langKey - the locale key
* @return {!Promise} A promise object.
*/
function extractForLanguage(langKey) {
let deferred = q.defer();
let translationBundle = path.join(conf.paths.base, `i18n/messages-${langKey}.xtb`);
let codeSource = path.join(conf.paths.serve, '**.js');
let messagesSource = path.join(conf.paths.messagesForExtraction, '**.js');
let command = `java -jar ${conf.paths.xtbgenerator} --lang ${langKey}` +
` --xtb_output_file ${translationBundle} --js ${codeSource} --js ${messagesSource}`;
if (fileExists.sync(translationBundle)) {
command = `${command} --translations_file ${translationBundle}`;
}
childProcess.exec(command, function(err, stdout, stderr) {
if (err) {
gulpUtil.log(stdout);
gulpUtil.log(stderr);
deferred.reject(new Error(err));
}
deferred.resolve();
});
return deferred.promise;
}
gulp.task('generate-xtbs', [
'extract-translations',
'remove-unused-translations',
'remove-duplicated-translations',
'sort-translations',
'set-prod-node-env',
]);
let prevMsgs = {};
gulp.task('buildExistingI18nCache', function() {
return gulp.src('i18n/messages-en.xtb').pipe(cheerio((doc) => {
doc('translation').each((i, translation) => {
let key = translation.attribs.key;
let index = key.lastIndexOf('_');
if (index !== -1) {
let lastpart = key.substring(index + 1);
if (/^[0-9]+$/.test(lastpart)) {
let indexSuffix = Number(lastpart);
let filePrefix = key.substring(0, index);
if (!prevMsgs[filePrefix]) {
prevMsgs[filePrefix] = [];
}
prevMsgs[filePrefix].push({
index: indexSuffix,
key: key,
text: doc(translation).text(),
desc: translation.attribs.desc,
used: false,
});
}
}
});
}));
});
/**
* Extracts all translation messages into XTB bundles.
*
* Cleans up the data from the previous run to prevent cross-pollination between branches.
*/
gulp.task(
'extract-translations', ['scripts', 'angular-templates', 'clean-messages-for-extraction'],
function() {
let promises = conf.translations.map((translation) => extractForLanguage(translation.key));
return q.all(promises);
});
/**
* Task to sort translations.
*/
gulp.task('sort-translations', ['remove-duplicated-translations'], function() {
return gulp.src('i18n/messages-*.xtb')
.pipe(xslt('build/sort-translations.xslt'))
.pipe(gulp.dest('i18n'));
});
/**
* Task to remove duplicated translations (should remove old translations from XTB when entries are
* updated in source code). It has to be ran before 'sort-translations' as original translation
* order is required.
*/
gulp.task('remove-duplicated-translations', ['extract-translations'], function() {
return gulp.src('i18n/messages-*.xtb')
.pipe(xslt('build/remove-duplicated-translations.xslt'))
.pipe(gulp.dest('i18n'));
});
/**
* Task to used to find translations used in JavaScript files. Do not run manually. It should be
* invoked as a part of 'remove-unused-translations'.
*/
gulp.task('find-translations-used-in-js', function() {
let jsSource = path.join(conf.paths.frontendSrc, '**/*.js');
return gulp.src(jsSource).pipe(freplace(/MSG_\w*/g, function(match) {
// Mark every message found in JavaScript files as used, it will allow deletion of unused
// messages afterwards.
translationsManager.addUsed(match);
}));
});
/**
* Task to remove unused translations. Do not run manually. It should be invoked as a part of
* 'generate-xtbs'.
*/
gulp.task(
'remove-unused-translations', ['angular-templates', 'find-translations-used-in-js'],
function() {
// Get translations used in JavaScript and HTML files. These will not be removed.
let used = translationsManager.getUsed();
return gulp.src('i18n/messages-*.xtb')
.pipe(cheerio((doc) => {
let unused = new Set();
// Find translations to remove.
doc('translation').each((i, translation) => {
let key = translation.attribs.key;
if (!used.has(key)) {
unused.add(key);
}
});
// Remove unused translations.
unused.forEach((r) => {
doc(`translation[key=${r}]`).remove();
});
}))
.pipe(gulp.dest('i18n/'));
});
/**
* Translations manager is a closure function allowing to manage translations.
* Allows removing unused translations after marking certain as used.
*
* @type {{markAsUsed, removeUnused}}
*/
export let translationsManager = (function() {
/**
* Set of translations marked as used.
*
* @type {Set}
*/
let used = new Set();
/**
* Function used to mark translations as used.
*
* @param {string} key
*/
function addUsed(key) {
used.add(key);
}
/**
* Function used to get used translations.
*
* @return {Set}
*/
function getUsed() {
return used;
}
return {
addUsed: addUsed,
getUsed: getUsed,
};
})();
// Regex to match [[Foo | Bar]] or [[Foo]] i18n placeholders.
// Technical details:
// * First capturing group is lazy math for any string not-containing |. This is to make
// both [[ message | description ]] and [[ message ]] work.
// * Second is non-capturing and optional. It has a capturing group inside. This is to
// extract description that is optional.
const I18N_REGEX = /\[\[([^|]*?)(?:\|(.*?))?\]\]/g;
export function processI18nMessages(file, minifiedHtml) {
let content = jsesc(minifiedHtml);
let pureHtmlContent = `${content}`;
let filePath = path.relative(file.base, file.path);
let messageVarPrefix = filePath.toUpperCase().split('/').join('_').replace('.HTML', '');
let used = new Set();
/**
* Finds all i18n messages inside a template and returns its text, description and original
* string.
* @param {string} htmlContent
* @return {!Array<{text: string, desc: string, original: string}>}
*/
function findI18nMessages(htmlContent) {
let matches = htmlContent.match(I18N_REGEX);
if (matches) {
return matches.map((match) => {
let exec = regexpClone(I18N_REGEX).exec(match);
// Default to no description when it is not provided.
let desc = (exec[2] || '(no description provided)').trim();
// replace {{$variableName}} with {{ $variableName}} to avoid {$ getting recognised as
// google.getMsg format
let text = exec[1].replace('{$', '{ $');
let varName = undefined;
if (prevMsgs[`MSG_${messageVarPrefix}`]) {
for (let msg of prevMsgs[`MSG_${messageVarPrefix}`]) {
if (msg.text === text && msg.desc === desc && !msg.used) {
varName = msg.key;
msg.used = true;
used.add(msg.index);
break;
}
}
}
return {text: text, desc: desc, original: match, varName: varName};
});
}
return [];
}
let i18nMessages = findI18nMessages(content);
/**
* @param {number} index
* @return {string}
*/
function createMessageVarName() {
for (let i = 0;; i++) {
if (!used.has(i)) {
used.add(i);
return `MSG_${messageVarPrefix}_${i}`;
}
}
}
i18nMessages.forEach((message) => {
message.varName = message.varName || createMessageVarName();
// Replace i18n messages with english messages for testing and MSG_ vars invocations
// for compiler passses.
content = content.replace(message.original, `' + ${message.varName} + '`);
pureHtmlContent = pureHtmlContent.replace(message.original, message.text);
// Mark every message found in this HTML file as used, it will allow deletion of unused messages
// afterwards.
translationsManager.addUsed(message.varName);
});
let messageVariables = i18nMessages.map((message) => {
return `/** @desc ${message.desc} */\n` +
`var ${message.varName} = goog.getMsg('${message.text}');\n`;
});
file.messages = messageVariables.join('\n');
// Eval pure HTML content, because it has been jsescaped previously. This is safe to eval since
// it was escaped by jsecs previously.
file.pureHtmlContent = eval(`'${pureHtmlContent}'`);
file.moduleContent = `` +
`import module from '/index_module';\n\n${file.messages}\n` +
`module.run(['$templateCache', ($templateCache) => {\n` +
` $templateCache.put('${filePath}', '${content}');\n` +
`}]);\n`;
return minifiedHtml;
}