2* @description MeshCentral MeshAgent
3* @author Ylian Saint-Hilaire
4* @copyright Intel Corporation 2019-2021
9const fs = require('fs');
10const path = require('path');
11const os = require('os');
12//const zlib = require('zlib');
13var performCheck = false;
14var translationTable = null;
15var sourceStrings = null;
16var jsdom = null; //require('jsdom');
17var esprima = null; //require('esprima'); // https://www.npmjs.com/package/esprima
18var minifyLib = 2; // 0 = None, 1 = minify-js, 2 = HTMLMinifier
21var meshCentralSourceFiles = [
22 "../views/agentinvite.handlebars",
23 "../views/invite.handlebars",
24 "../views/default.handlebars",
25 "../views/default3.handlebars",
26 "../views/default-mobile.handlebars",
27 "../views/download.handlebars",
28 "../views/download2.handlebars",
29 "../views/error404.handlebars",
30 "../views/error404-mobile.handlebars",
31 "../views/login.handlebars",
32 "../views/login2.handlebars",
33 "../views/login-mobile.handlebars",
34 "../views/terms.handlebars",
35 "../views/terms-mobile.handlebars",
36 "../views/xterm.handlebars",
37 "../views/message.handlebars",
38 "../views/message2.handlebars",
39 "../views/messenger.handlebars",
40 "../views/player.handlebars",
41 "../views/sharing.handlebars",
42 "../views/sharing-mobile.handlebars",
43 "../views/mstsc.handlebars",
44 "../views/ssh.handlebars",
45 "../emails/account-check.html",
46 "../emails/account-invite.html",
47 "../emails/account-login.html",
48 "../emails/account-reset.html",
49 "../emails/mesh-invite.html",
50 "../emails/device-notify.html",
51 "../emails/device-help.html",
52 "../emails/account-check.txt",
53 "../emails/account-invite.txt",
54 "../emails/account-login.txt",
55 "../emails/account-reset.txt",
56 "../emails/mesh-invite.txt",
57 "../emails/device-notify.txt",
58 "../emails/device-help.txt",
59 "../emails/sms-messages.txt",
60 "../agents/agent-translations.json",
61 "../agents/modules_meshcore/coretranslations.json"
64var minifyMeshCentralSourceFiles = [
65 "../views/agentinvite.handlebars",
66 "../views/invite.handlebars",
67 "../views/default.handlebars",
68 "../views/default3.handlebars",
69 "../views/default-mobile.handlebars",
70 "../views/download.handlebars",
71 "../views/download2.handlebars",
72 "../views/error404.handlebars",
73 "../views/error4042.handlebars",
74 "../views/error404-mobile.handlebars",
75 "../views/login.handlebars",
76 "../views/login2.handlebars",
77 "../views/login-mobile.handlebars",
78 "../views/terms.handlebars",
79 "../views/terms-mobile.handlebars",
80 "../views/xterm.handlebars",
81 "../views/message.handlebars",
82 "../views/message2.handlebars",
83 "../views/messenger.handlebars",
84 "../views/player.handlebars",
85 "../views/sharing.handlebars",
86 "../views/sharing-mobile.handlebars",
87 "../views/mstsc.handlebars",
88 "../views/ssh.handlebars",
89 "../public/scripts/agent-desktop-0.0.2.js",
90 "../public/scripts/agent-rdp-0.0.1.js",
91 "../public/scripts/agent-redir-rtc-0.1.0.js",
92 "../public/scripts/agent-redir-ws-0.1.1.js",
93 "../public/scripts/amt-0.2.0.js",
94 "../public/scripts/amt-desktop-0.0.2.js",
95 "../public/scripts/amt-ider-ws-0.0.1.js",
96 "../public/scripts/amt-redir-ws-0.1.0.js",
97 "../public/scripts/amt-script-0.2.0.js",
98 "../public/scripts/amt-setupbin-0.1.0.js",
99 "../public/scripts/amt-terminal-0.0.2.js",
100 "../public/scripts/amt-wsman-0.2.0.js",
101 "../public/scripts/amt-wsman-ws-0.2.0.js",
102 "../public/scripts/common-0.0.1.js",
103 "../public/scripts/meshcentral.js",
104 "../public/scripts/u2f-api.js",
105 "../public/scripts/xterm-addon-fit.js",
106 "../public/scripts/xterm-addon-image.js",
107 "../public/scripts/xterm.js",
108 "../public/scripts/zlib-adler32.js",
109 "../public/scripts/zlib-crc32.js",
110 "../public/scripts/zlib-inflate.js",
111 "../public/scripts/zlib.js"
114// True is this module is run directly using NodeJS
115var directRun = (require.main === module);
117// Check NodeJS version
118const NodeJSVer = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
119if (directRun && (NodeJSVer < 8)) { log("Translate.js requires Node v8 or above, current version is " + process.version + "."); return; }
121// node translate.json CHECK ../meshcentral/views/default.handlebars
122// node translate.json EXTRACT bob.json ../meshcentral/views/default.handlebars
123// node translate.js TRANSLATE fr test2.json ../meshcentral/views/default.handlebars
127 if (worker == null) {
128 console.log(...arguments);
130 worker.parentPort.postMessage({ msg: arguments[0] })
134if (directRun && (NodeJSVer >= 12)) {
135 const xworker = require('worker_threads');
137 if (xworker.isMainThread == false) {
138 // We are being called to do some work
140 const op = worker.workerData.op;
141 const args = worker.workerData.args;
144 jsdom = require('jsdom');
145 esprima = require('esprima'); // https://www.npmjs.com/package/esprima
146 if (minifyLib == 1) { log("minify-js is no longer used, please switch to \"html-minifier-terser\""); process.exit(); return; }
147 if (minifyLib == 2) { minify = require('html-minifier-terser').minify; } // https://www.npmjs.com/package/html-minifier
151 translateSingleThreaded(args[0], args[1], args[2], args[3]);
157 } catch (ex) { log(ex); }
160if (directRun) { setup(); }
163 var libs = ['jsdom@22.1.0', 'esprima@4.0.1'];
164 if (minifyLib == 1) { log("minify-js is no longer used, please switch to \"html-minifier-terser\""); process.exit(); return; }
165 if (minifyLib == 2) { libs.push('html-minifier-terser@7.2.0'); }
166 InstallModules(libs, start);
169function start() { startEx(process.argv); }
171async function startEx(argv) {
173 jsdom = require('jsdom');
174 esprima = require('esprima'); // https://www.npmjs.com/package/esprima
175 if (minifyLib == 1) { log("minify-js is no longer used, please switch to \"html-minifier-terser\""); process.exit(); return; }
176 if (minifyLib == 2) { minify = require('html-minifier-terser').minify; } // https://www.npmjs.com/package/html-minifier-terser
179 if (argv.length > 2) { command = argv[2].toLowerCase(); }
180 if (['minify', 'check', 'extract', 'extractall', 'translate', 'translateall', 'minifyall', 'minifydir', 'merge', 'totext', 'fromtext', 'remove'].indexOf(command) == -1) { command = null; }
182 if (directRun) { log('MeshCentral web site translator'); }
183 if (command == null) {
184 log('Usage "node translate.js [command] [options]');
185 log('Possible commands:');
187 log(' CHECK [files]');
188 log(' Check will pull string out of a web page and display a report.');
190 log(' EXTRACT [languagefile] [files]');
191 log(' Extract strings from web pages and generate a language (.json) file.');
193 log(' EXTRACTALL (languagefile)');
194 log(' Extract all MeshCentral strings from web pages and generate the languages.json file.');
196 log(' TRANSLATE [language] [languagefile] [files]');
197 log(' Use a language (.json) file to translate web pages to a give language.');
199 log(' TRANSLATEALL (languagefile) (language code)');
200 log(' Translate all MeshCentral strings using the languages.json file.');
202 log(' MINIFY [sourcefile]');
203 log(' Minify a single file.');
205 log(' MINIFYDIR [sourcedir] [destinationdir]');
206 log(' Minify all files in a directory.');
209 log(' Minify the main MeshCentral english web pages.');
211 log(' MERGE [sourcefile] [targetfile] [language code]');
212 log(' Merge a language from a translation file into another translation file.');
214 log(' TOTEXT [translationfile] [textfile] [language code]');
215 log(' Save a text for with all strings of a given language.');
217 log(' FROMTEXT [translationfile] [textfile] [language code]');
218 log(' Import raw text string as translations for a language code.');
223 // Extract strings from web pages and display a report
224 if (command == 'check') {
226 for (var i = 3; i < argv.length; i++) { if (fs.existsSync(argv[i]) == false) { log('Missing file: ' + argv[i]); process.exit(); return; } sources.push(argv[i]); }
227 if (sources.length == 0) { log('No source files specified.'); process.exit(); return; }
230 for (var i = 0; i < sources.length; i++) { extractFromHtml(sources[i]); }
232 for (var i in sourceStrings) { count++; }
233 log('Extracted ' + count + ' strings.');
238 // Extract strings from web pages
239 if (command == 'extract') {
240 if (argv.length < 4) { log('No language file specified.'); process.exit(); return; }
242 for (var i = 4; i < argv.length; i++) { if (fs.existsSync(argv[i]) == false) { log('Missing file: ' + argv[i]); process.exit(); return; } sources.push(argv[i]); }
243 if (sources.length == 0) { log('No source files specified.'); process.exit(); return; }
244 extract(argv[3], sources);
247 // Save a text file with all the strings for a given language
248 if (command == 'totext') {
249 if ((argv.length == 6)) {
250 if (fs.existsSync(argv[3]) == false) { log('Unable to find: ' + argv[3]); return; }
251 totext(argv[3], argv[4], argv[5]);
253 log('Usage: TOTEXT [translationfile] [textfile] [language code]');
258 // Read a text file and use it as translation for a given language
259 if (command == 'fromtext') {
260 if ((argv.length == 6)) {
261 if (fs.existsSync(argv[3]) == false) { log('Unable to find: ' + argv[3]); return; }
262 if (fs.existsSync(argv[4]) == false) { log('Unable to find: ' + argv[4]); return; }
263 fromtext(argv[3], argv[4], argv[5]);
265 log('Usage: FROMTEXT [translationfile] [textfile] [language code]');
270 // Merge one language from a language file into another language file.
271 if (command == 'merge') {
272 if ((argv.length == 6)) {
273 if (fs.existsSync(argv[3]) == false) { log('Unable to find: ' + argv[3]); return; }
274 if (fs.existsSync(argv[4]) == false) { log('Unable to find: ' + argv[4]); return; }
275 merge(argv[3], argv[4], argv[5]);
277 log('Usage: MERGE [sourcefile] [tartgetfile] [language code]');
282 // Extract or translate all MeshCentral strings
283 if (command == 'extractall') {
284 if (argv.length > 4) { lang = argv[4].toLowerCase(); }
285 var translationFile = 'translate.json';
286 if (argv.length > 3) {
287 if (fs.existsSync(argv[3]) == false) { log('Unable to find: ' + argv[3]); return; } else { translationFile = argv[3]; }
289 extract(translationFile, meshCentralSourceFiles, translationFile);
292 // Remove a language from a translation file
293 if (command == 'remove') {
294 if (argv.length <= 3) { log('Usage: remove [language] (file)'); return; }
295 lang = argv[3].toLowerCase();
296 var translationFile = 'translate.json';
297 if (argv.length > 4) {
298 if (fs.existsSync(argv[4]) == false) { log('Unable to find: ' + argv[4]); return; } else { translationFile = argv[4]; }
301 if (fs.existsSync(translationFile) == false) { log('Unable to find: ' + translationFile); return; }
302 var langFileData = null;
303 try { langFileData = JSON.parse(fs.readFileSync(translationFile)); } catch (ex) { }
304 if ((langFileData == null) || (langFileData.strings == null)) { log("Invalid language file."); process.exit(); return; }
305 for (var i in langFileData.strings) { delete langFileData.strings[i][lang]; }
306 fs.writeFileSync(translationFile, translationsToJson({ strings: langFileData.strings }), { flag: 'w+' });
311 if (command == 'translateall') {
312 if (fs.existsSync('../views/translations') == false) { fs.mkdirSync('../views/translations'); }
313 //if (fs.existsSync('../public/translations') == false) { fs.mkdirSync('../public/translations'); }
315 if (argv.length > 4) { lang = argv[4].toLowerCase(); }
316 if (argv.length > 3) {
317 if (fs.existsSync(argv[3]) == false) {
318 log('Unable to find: ' + argv[3]);
320 translate(lang, argv[3], meshCentralSourceFiles, 'translations');
323 if (fs.existsSync('translate.json') == false) {
324 log('Unable to find translate.json.');
326 translate(lang, 'translate.json', meshCentralSourceFiles, 'translations');
332 // Translate web pages to a given language given a language file
333 if (command == 'translate') {
334 if (argv.length < 4) { log("No language specified."); process.exit(); return; }
335 if (argv.length < 5) { log("No language file specified."); process.exit(); return; }
336 var lang = argv[3].toLowerCase();
337 var langFile = argv[4];
338 if (fs.existsSync(langFile) == false) { log("Missing language file: " + langFile); process.exit(); return; }
340 var sources = [], subdir = null;
341 for (var i = 5; i < argv.length; i++) {
342 if (argv[i].startsWith('--subdir:')) {
343 subdir = argv[i].substring(9);
345 if (fs.existsSync(argv[i]) == false) { log("Missing file: " + argv[i]); process.exit(); return; } sources.push(argv[i]);
348 if (sources.length == 0) { log("No source files specified."); process.exit(); return; }
349 translate(lang, langFile, sources, subdir);
352 if (command == 'minifydir') {
353 if (argv.length < 4) { log("Command source and/or destination folders missing."); process.exit(); return; }
354 const sourceFiles = fs.readdirSync(argv[3]);
355 for (var i in sourceFiles) {
356 if (sourceFiles[i].endsWith('.js') || sourceFiles[i].endsWith('.json')) {
357 console.log("Processing " + sourceFiles[i] + "...");
358 const sourceFile = path.join(argv[3], sourceFiles[i]);
359 if (sourceFiles[i].endsWith('.js')) {
360 // Minify the file .js file
361 const destinationFile = path.join(argv[4], sourceFiles[i].substring(0, sourceFiles[i].length - 3) + '.min.js');
362 if (minifyLib == 2) {
363 var inFile = fs.readFileSync(sourceFile).toString();
365 // Perform minification pre-processing
366 if (sourceFile.endsWith('.handlebars') >= 0) { inFile = inFile.split('{{{pluginHandler}}}').join('"{{{pluginHandler}}}"'); }
367 if (sourceFile.endsWith('.js')) { inFile = '<script>' + inFile + '</script>'; }
369 var minifiedOut = await minify(inFile, {
370 collapseBooleanAttributes: true,
371 collapseInlineTagWhitespace: false, // This is not good.
372 collapseWhitespace: true,
375 removeComments: true,
376 removeOptionalTags: true,
377 removeEmptyAttributes: true,
378 removeAttributeQuotes: true,
379 removeRedundantAttributes: true,
380 removeScriptTypeAttributes: true,
381 removeTagWhitespace: true,
382 preserveLineBreaks: false,
383 useShortDoctype: true
386 // Perform minification post-processing
387 if (sourceFile.endsWith('.js')) { minifiedOut = minifiedOut.substring(8, minifiedOut.length - 9); }
388 if (sourceFile.endsWith('.handlebars') >= 0) { minifiedOut = minifiedOut.split('"{{{pluginHandler}}}"').join('{{{pluginHandler}}}'); }
389 fs.writeFileSync(destinationFile, minifiedOut, { flag: 'w+' });
391 } else if (sourceFiles[i].endsWith('.json')) {
392 // Minify the file .json file
393 const destinationFile = path.join(argv[4], sourceFiles[i]);
394 var inFile = JSON.parse(fs.readFileSync(sourceFile).toString());
395 fs.writeFileSync(destinationFile, JSON.stringify(inFile), { flag: 'w+' });
402 if (command == 'minifyall') {
403 for (var i in minifyMeshCentralSourceFiles) {
404 var outname = minifyMeshCentralSourceFiles[i];
405 var outnamemin = null;
406 if (outname.endsWith('.handlebars')) {
407 outnamemin = (outname.substring(0, outname.length - 11) + '-min.handlebars');
408 } else if (outname.endsWith('.html')) {
409 outnamemin = (outname.substring(0, outname.length - 5) + '-min.html');
410 } else if (outname.endsWith('.htm')) {
411 outnamemin = (outname.substring(0, outname.length - 4) + '-min.htm');
412 } else if (outname.endsWith('.js')) {
413 outnamemin = (outname.substring(0, outname.length - 3) + '-min.js');
415 outnamemin = (outname, outname + '.min');
417 log('Generating ' + outnamemin + '...');
421 if (minifyLib == 1) {
425 }, function (e, compress) {
426 if (e) { log('ERROR ', e); return done(); }
427 compress.run((e) => { e ? log('Minification fail', e) : log('Minification success'); minifyDone(); });
434 if (minifyLib == 2) {
435 var inFile = fs.readFileSync(outname).toString();
437 // Perform minification pre-processing
438 if (outname.endsWith('.handlebars') >= 0) { inFile = inFile.split('{{{pluginHandler}}}').join('"{{{pluginHandler}}}"'); }
439 if (outname.endsWith('.js')) { inFile = '<script>' + inFile + '</script>'; }
441 var minifiedOut = null;
443 minifiedOut = await minify(inFile, {
444 collapseBooleanAttributes: true,
445 collapseInlineTagWhitespace: false, // This is not good.
446 collapseWhitespace: true,
449 removeComments: true,
450 removeOptionalTags: true,
451 removeEmptyAttributes: true,
452 removeAttributeQuotes: true,
453 removeRedundantAttributes: true,
454 removeScriptTypeAttributes: true,
455 removeTagWhitespace: true,
456 preserveLineBreaks: false,
457 log: function(a) { if (typeof a !== 'string') { console.log(a); } } // Log errors from UglifyJS to console output
463 // Perform minification post-processing
464 if (outname.endsWith('.js')) { minifiedOut = minifiedOut.substring(8, minifiedOut.length - 9); }
465 if (outname.endsWith('.handlebars') >= 0) { minifiedOut = minifiedOut.split('"{{{pluginHandler}}}"').join('{{{pluginHandler}}}'); }
466 fs.writeFileSync(outnamemin, minifiedOut, { flag: 'w+' });
469 if (outname.endsWith('.js')) {
470 var compressHandler = function compressHandlerFunc(err, buffer, outnamemin2) {
472 console.log('GZIP', compressHandlerFunc.outname);
473 fs.writeFileSync(compressHandlerFunc.outname, buffer, { flag: 'w+' });
476 compressHandler.outname = outnamemin;
477 zlib.gzip(Buffer.from(minifiedOut), compressHandler);
479 fs.writeFileSync(outnamemin, minifiedOut, { flag: 'w+' });
486 if (command == 'minify') {
487 var outname = argv[3];
488 var outnamemin = null;
489 if (outname.endsWith('.handlebars')) {
490 outnamemin = (outname.substring(0, outname.length - 11) + '-min.handlebars');
491 } else if (outname.endsWith('.html')) {
492 outnamemin = (outname.substring(0, outname.length - 5) + '-min.html');
493 } else if (outname.endsWith('.htm')) {
494 outnamemin = (outname.substring(0, outname.length - 4) + '-min.htm');
495 } else if (outname.endsWith('.js')) {
496 outnamemin = (outname.substring(0, outname.length - 3) + '-min.js');
498 outnamemin = (outname, outname + '.min');
500 log('Generating ' + path.basename(outnamemin) + '...');
503 if (minifyLib == 2) {
504 var inFile = fs.readFileSync(outname).toString()
506 // Perform minification pre-processing
507 if (outname.endsWith('.handlebars') >= 0) { inFile = inFile.split('{{{pluginHandler}}}').join('"{{{pluginHandler}}}"'); }
508 if (outname.endsWith('.js')) { inFile = '<script>' + inFile + '</script>'; }
510 var minifiedOut = await minify(inFile, {
511 collapseBooleanAttributes: true,
512 collapseInlineTagWhitespace: false, // This is not good.
513 collapseWhitespace: true,
516 removeComments: true,
517 removeOptionalTags: true,
518 removeEmptyAttributes: true,
519 removeAttributeQuotes: true,
520 removeRedundantAttributes: true,
521 removeScriptTypeAttributes: true,
522 removeTagWhitespace: true,
523 preserveLineBreaks: false,
524 useShortDoctype: true
527 // Perform minification post-processing
528 if (outname.endsWith('.js')) { minifiedOut = minifiedOut.substring(8, minifiedOut.length - 9); }
529 if (outname.endsWith('.handlebars') >= 0) { minifiedOut = minifiedOut.split('"{{{pluginHandler}}}"').join('{{{pluginHandler}}}'); }
530 fs.writeFileSync(outnamemin, minifiedOut, { flag: 'w+' });
537function totext(source, target, lang) {
538 // Load the source language file
539 var sourceLangFileData = null;
540 try { sourceLangFileData = JSON.parse(fs.readFileSync(source)); } catch (ex) { console.log(ex); }
541 if ((sourceLangFileData == null) || (sourceLangFileData.strings == null)) { log("Invalid source language file."); process.exit(); return; }
543 log('Writing ' + lang + '...');
547 var outputCharCount = 0; // Google has a 5000 character limit
548 var splitOutput = [];
549 var splitOutputPtr = 1;
551 for (var i in sourceLangFileData.strings) {
552 if ((sourceLangFileData.strings[i][lang] != null) && (sourceLangFileData.strings[i][lang].indexOf('\r') == -1) && (sourceLangFileData.strings[i][lang].indexOf('\n') == -1)) {
553 output.push(sourceLangFileData.strings[i][lang]);
554 outputCharCount += (sourceLangFileData.strings[i][lang].length + 2);
555 if (outputCharCount > 4500) { outputCharCount = 0; splitOutputPtr++; }
556 if (splitOutput[splitOutputPtr] == null) { splitOutput[splitOutputPtr] = []; }
557 splitOutput[splitOutputPtr].push(sourceLangFileData.strings[i][lang]);
560 outputCharCount += 2;
561 if (outputCharCount > 4500) { outputCharCount = 0; splitOutputPtr++; }
562 if (splitOutput[splitOutputPtr] == null) { splitOutput[splitOutputPtr] = []; }
563 splitOutput[splitOutputPtr].push('');
568 if (splitOutputPtr == 1) {
569 // Save the target back
570 fs.writeFileSync(target + '-' + lang + '.txt', output.join(os.EOL), { flag: 'w+' });
573 // Save the text in 1000 string bunches
574 for (var i in splitOutput) {
575 log('Writing ' + target + '-' + lang + '-' + i + '.txt...');
576 fs.writeFileSync(target + '-' + lang + '-' + i + '.txt', splitOutput[i].join(os.EOL), { flag: 'w+' });
582function fromtext(source, target, lang) {
583 // Load the source language file
584 var sourceLangFileData = null;
585 try { sourceLangFileData = JSON.parse(fs.readFileSync(source)); } catch (ex) { console.log(ex); }
586 if ((sourceLangFileData == null) || (sourceLangFileData.strings == null)) { log("Invalid source language file."); process.exit(); return; }
588 log('Updating ' + lang + '...');
591 var rawText = fs.readFileSync(target).toString('utf8');
592 var rawTextArray = rawText.split(/\r?\n/);
595 log('Translation file: ' + sourceLangFileData.strings.length + ' string(s)');
596 log('Text file: ' + rawTextArray.length + ' string(s)');
597 if (sourceLangFileData.strings.length != rawTextArray.length) { log('String count mismatch, unable to import.'); process.exit(1); return; }
600 var splitOutput = [];
601 for (var i in sourceLangFileData.strings) {
602 if ((sourceLangFileData.strings[i]['en'] != null) && (sourceLangFileData.strings[i]['en'].indexOf('\r') == -1) && (sourceLangFileData.strings[i]['en'].indexOf('\n') == -1)) {
603 if (sourceLangFileData.strings[i][lang] == null) { sourceLangFileData.strings[i][lang] = rawTextArray[i]; }
607 fs.writeFileSync(source + '-new', translationsToJson(sourceLangFileData), { flag: 'w+' });
611function merge(source, target, lang) {
612 // Load the source language file
613 var sourceLangFileData = null;
614 try { sourceLangFileData = JSON.parse(fs.readFileSync(source)); } catch (ex) { console.log(ex); }
615 if ((sourceLangFileData == null) || (sourceLangFileData.strings == null)) { log("Invalid source language file."); process.exit(); return; }
617 // Load the target language file
618 var targetLangFileData = null;
619 try { targetLangFileData = JSON.parse(fs.readFileSync(target)); } catch (ex) { console.log(ex); }
620 if ((targetLangFileData == null) || (targetLangFileData.strings == null)) { log("Invalid target language file."); process.exit(); return; }
622 log('Merging ' + lang + '...');
624 // Index the target file
626 for (var i in targetLangFileData.strings) { if (targetLangFileData.strings[i].en != null) { index[targetLangFileData.strings[i].en] = targetLangFileData.strings[i]; } }
628 // Merge the translation
629 for (var i in sourceLangFileData.strings) {
630 if ((sourceLangFileData.strings[i].en != null) && (sourceLangFileData.strings[i][lang] != null) && (index[sourceLangFileData.strings[i].en] != null)) {
631 //if (sourceLangFileData.strings[i][lang] == null) {
632 index[sourceLangFileData.strings[i].en][lang] = sourceLangFileData.strings[i][lang];
637 // Deindex the new target file
638 var targetData = { strings: [] };
639 for (var i in index) { targetData.strings.push(index[i]); }
641 // Save the target back
642 fs.writeFileSync(target, translationsToJson(targetData), { flag: 'w+' });
646function translate(lang, langFile, sources, createSubDir) {
647 if (directRun && (NodeJSVer >= 12) && (lang == null)) {
648 // Multi threaded translation
649 log("Multi-threaded translation.");
651 // Load the language file
652 var langFileData = null;
653 try { langFileData = JSON.parse(fs.readFileSync(langFile)); } catch (ex) { console.log(ex); }
654 if ((langFileData == null) || (langFileData.strings == null)) { log("Invalid language file."); process.exit(); return; }
657 for (var i in langFileData.strings) { var entry = langFileData.strings[i]; for (var j in entry) { if ((j != 'en') && (j != 'xloc') && (j != '*')) { langs[j.toLowerCase()] = true; } } }
658 var Worker = require('worker_threads').Worker;
659 var MAX_WORKERS = os.cpus().length; // limit to the number of CPU cores for now
660 var activeWorkers = 0;
662 function processNextTask() {
663 if (activeWorkers < MAX_WORKERS && taskQueue.length > 0) {
664 var nextTask = taskQueue.shift();
666 var worker = new Worker('./translate.js', { stdout: true, workerData: { op: 'translate', args: [nextTask.lang, nextTask.langFile, nextTask.sources, nextTask.createSubDir] } });
667 worker.stdout.on('data', function (msg) { console.log('wstdio:', msg.toString()); });
668 worker.on('message', function (message) { console.log(message.msg); });
669 worker.on('error', function (error) { console.log('error', error); activeWorkers--; processNextTask(); });
670 worker.on('exit', function (code) { /*console.log('exit', code);*/ activeWorkers--; processNextTask(); });
673 for (var lang in langs) {
674 if (langs.hasOwnProperty(lang)) {
675 taskQueue.push({ lang: lang, langFile: langFile, sources: sources, createSubDir: createSubDir});
678 for (var i = 0; i < Math.min(MAX_WORKERS, taskQueue.length); i++) { processNextTask(); }
680 // Single threaded translation
681 translateSingleThreaded(lang, langFile, sources, createSubDir);
684 // Translate any JSON files
685 for (var i = 0; i < sources.length; i++) { if (sources[i].endsWith('.json')) { translateAllInJson(lang, langFile, sources[i]); } }
688function translateSingleThreaded(lang, langFile, sources, createSubDir) {
689 // Load the language file
690 var langFileData = null;
691 try { langFileData = JSON.parse(fs.readFileSync(langFile)); } catch (ex) { }
692 if ((langFileData == null) || (langFileData.strings == null)) { log("Invalid language file."); process.exit(); return; }
694 if ((lang != null) && (lang != '*')) {
695 // Translate a single language
696 translateEx(lang, langFileData, sources, createSubDir);
698 // See that languages are in the translation file
700 for (var i in langFileData.strings) { var entry = langFileData.strings[i]; for (var j in entry) { if ((j != 'en') && (j != 'xloc') && (j != '*')) { langs[j.toLowerCase()] = true; } } }
701 for (var i in langs) { translateEx(i, langFileData, sources, createSubDir); }
708function translateEx(lang, langFileData, sources, createSubDir) {
709 // Build translation table, simple source->target for the given language.
710 translationTable = {};
711 for (var i in langFileData.strings) {
712 var entry = langFileData.strings[i];
713 if ((entry['en'] != null) && (entry[lang] != null)) { translationTable[entry['en']] = entry[lang]; }
715 // Translate the files
716 for (var i = 0; i < sources.length; i++) {
717 if (sources[i].endsWith('.html') || sources[i].endsWith('.htm') || sources[i].endsWith('.handlebars')) { translateFromHtml(lang, sources[i], createSubDir); }
718 else if (sources[i].endsWith('.txt')) { translateFromTxt(lang, sources[i], createSubDir); }
722function extract(langFile, sources) {
724 if (fs.existsSync(langFile) == true) {
725 var langFileData = null;
726 try { langFileData = JSON.parse(fs.readFileSync(langFile)); } catch (ex) { }
727 if ((langFileData == null) || (langFileData.strings == null)) { log("Invalid language file."); process.exit(); return; }
728 for (var i in langFileData.strings) {
729 sourceStrings[langFileData.strings[i]['en']] = langFileData.strings[i];
730 delete sourceStrings[langFileData.strings[i]['en']].xloc;
733 for (var i = 0; i < sources.length; i++) {
734 if (sources[i].endsWith('.html') || sources[i].endsWith('.htm') || sources[i].endsWith('.handlebars')) { extractFromHtml(sources[i]); }
735 else if (sources[i].endsWith('.txt')) { extractFromTxt(sources[i]); }
736 else if (sources[i].endsWith('.json')) { extractFromJson(sources[i]); }
738 var count = 0, output = [];
739 for (var i in sourceStrings) {
741 sourceStrings[i]['en'] = i;
742 //if ((sourceStrings[i].xloc != null) && (sourceStrings[i].xloc.length > 0)) { output.push(sourceStrings[i]); } // Only save results that have a source location.
743 output.push(sourceStrings[i]); // Save all results
745 fs.writeFileSync(langFile, translationsToJson({ strings: output }), { flag: 'w+' });
746 log(format("{0} strings in output file.", count));
751function extractFromTxt(file) {
752 log("Processing TXT: " + path.basename(file));
753 var lines = fs.readFileSync(file).toString().split(/\r?\n/);
754 var name = path.basename(file);
755 for (var i in lines) {
757 if ((line.length > 1) && (line[0] != '~')) {
758 if (sourceStrings[line] == null) { sourceStrings[line] = { en: line, xloc: [name] }; } else { if (sourceStrings[line].xloc == null) { sourceStrings[line].xloc = []; } sourceStrings[line].xloc.push(name); }
763function extractFromJson(file) {
764 log("Processing JSON: " + path.basename(file));
765 var json = JSON.parse(fs.readFileSync(file).toString());
766 var name = path.basename(file);
767 if (json.en == null) return;
768 for (var i in json.en) {
769 if (typeof json.en[i] == 'string') {
770 const str = json.en[i]
771 if (sourceStrings[str] == null) {
772 sourceStrings[str] = { en: str, xloc: [name] };
774 if (sourceStrings[str].xloc == null) { sourceStrings[str].xloc = []; } sourceStrings[str].xloc.push(name);
776 } else if (Array.isArray(json.en[i])) {
777 for (var k in json.en[i]) {
778 if (typeof json.en[i][k] == 'string') {
779 const str = json.en[i][k];
780 if (sourceStrings[str] == null) { sourceStrings[str] = { en: str, xloc: [name] }; } else { if (sourceStrings[str].xloc == null) { sourceStrings[str].xloc = []; } sourceStrings[str].xloc.push(name); }
787function extractFromHtml(file) {
788 var data = fs.readFileSync(file);
789 var { JSDOM } = jsdom;
790 const dom = new JSDOM(data, { includeNodeLocations: true });
791 log("Processing HTML: " + path.basename(file));
792 getStringsHtml(path.basename(file), dom.window.document.querySelector('body'));
795function getStringsHtml(name, node) {
796 for (var i = 0; i < node.childNodes.length; i++) {
797 var subnode = node.childNodes[i];
799 // Check if the "value" attribute exists and needs to be translated
800 var subnodeignore = false;
801 var subnodevalueignore = false;
802 if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
803 var subnodevalue = null, subnodeplaceholder = null, subnodetitle = null;
804 for (var j in subnode.attributes) {
805 if ((subnode.attributes[j].name == 'notrans') && (subnode.attributes[j].value == '1')) { subnodeignore = true; }
806 if ((subnode.attributes[j].name == 'notransval') && (subnode.attributes[j].value == '1')) { subnodevalueignore = true; }
807 if ((subnode.attributes[j].name == 'type') && (subnode.attributes[j].value == 'hidden')) { subnodeignore = true; }
808 if (subnode.attributes[j].name == 'value') { subnodevalue = subnode.attributes[j].value; }
809 if (subnode.attributes[j].name == 'placeholder') { subnodeplaceholder = subnode.attributes[j].value; }
810 if (subnode.attributes[j].name == 'title') { subnodetitle = subnode.attributes[j].value; }
812 if ((subnodevalue != null) && isNumber(subnodevalue) == true) { subnodevalue = null; }
813 if ((subnodeplaceholder != null) && isNumber(subnodeplaceholder) == true) { subnodeplaceholder = null; }
814 if ((subnodetitle != null) && isNumber(subnodetitle) == true) { subnodetitle = null; }
815 if ((subnodeignore == false) && (subnodevalueignore == false) && (subnodevalue != null)) {
816 // Add a new string to the list (value)
817 if (sourceStrings[subnodevalue] == null) { sourceStrings[subnodevalue] = { en: subnodevalue, xloc: [name] }; } else { if (sourceStrings[subnodevalue].xloc == null) { sourceStrings[subnodevalue].xloc = []; } sourceStrings[subnodevalue].xloc.push(name); }
819 if (subnodeplaceholder != null) {
820 // Add a new string to the list (placeholder)
821 if (sourceStrings[subnodeplaceholder] == null) { sourceStrings[subnodeplaceholder] = { en: subnodeplaceholder, xloc: [name] }; } else { if (sourceStrings[subnodeplaceholder].xloc == null) { sourceStrings[subnodeplaceholder].xloc = []; } sourceStrings[subnodeplaceholder].xloc.push(name); }
823 if (subnodetitle != null) {
824 // Add a new string to the list (title)
825 if (sourceStrings[subnodetitle] == null) { sourceStrings[subnodetitle] = { en: subnodetitle, xloc: [name] }; } else { if (sourceStrings[subnodetitle].xloc == null) { sourceStrings[subnodetitle].xloc = []; } sourceStrings[subnodetitle].xloc.push(name); }
829 if (subnodeignore == false) {
830 // Check the content of the element
831 var subname = subnode.id;
832 if (subname == null || subname == '') { subname = i; }
833 if (subnode.hasChildNodes()) {
834 getStringsHtml(name + '->' + subname, subnode);
836 if (subnode.nodeValue == null) continue;
837 var nodeValue = subnode.nodeValue.trim().split('\\r').join('').split('\\n').join('').trim();
838 if ((nodeValue.length > 0) && (subnode.nodeType == 3)) {
839 if ((node.tagName != 'SCRIPT') && (node.tagName != 'STYLE') && (nodeValue.length < 8000) && (nodeValue.startsWith('{{{') == false) && (nodeValue != ' ')) {
840 if (performCheck) { log(' "' + nodeValue + '"'); }
841 // Add a new string to the list
842 if (sourceStrings[nodeValue] == null) { sourceStrings[nodeValue] = { en: nodeValue, xloc: [name] }; } else { if (sourceStrings[nodeValue].xloc == null) { sourceStrings[nodeValue].xloc = []; } sourceStrings[nodeValue].xloc.push(name); }
843 } else if (node.tagName == 'SCRIPT') {
845 getStringFromJavaScript(name, subnode.nodeValue);
853function getStringFromJavaScript(name, script) {
854 if (performCheck) { log(format('Processing JavaScript of {0} bytes: {1}', script.length, name)); }
855 var tokenScript = esprima.tokenize(script), count = 0;
856 for (var i in tokenScript) {
857 var token = tokenScript[i];
858 if ((token.type == 'String') && (token.value.length > 2) && (token.value[0] == '"')) {
859 var str = token.value.substring(1, token.value.length - 1);
860 //if (performCheck) { log(' ' + name + '->' + (++count), token.value); }
861 if (performCheck) { log(' ' + token.value); }
862 if (sourceStrings[str] == null) { sourceStrings[str] = { en: str, xloc: [name + '->' + (++count)] }; } else { if (sourceStrings[str].xloc == null) { sourceStrings[str].xloc = []; } sourceStrings[str].xloc.push(name + '->' + (++count)); }
867function translateFromTxt(lang, file, createSubDir) {
868 log("Translating TXT (" + lang + "): " + path.basename(file));
869 var lines = fs.readFileSync(file).toString().split(/\r?\n/), outlines = [];
870 for (var i in lines) {
872 if ((line.length > 1) && (line[0] != '~')) {
873 if (translationTable[line] != null) { outlines.push(translationTable[line]); } else { outlines.push(line); }
879 var outname = file, out = outlines.join(os.EOL);
880 if (createSubDir != null) {
881 var outfolder = path.join(path.dirname(file), createSubDir);
882 if (fs.existsSync(outfolder) == false) { fs.mkdirSync(outfolder); }
883 outname = path.join(path.dirname(file), createSubDir, path.basename(file));
885 outname = (outname.substring(0, outname.length - 4) + '_' + lang + '.txt');
886 fs.writeFileSync(outname, out, { flag: 'w+' });
889function translateAllInJson(xlang, langFile, file) {
890 log("Translating JSON (" + ((xlang == null)?'All':xlang) + "): " + path.basename(file));
892 // Load the language file
893 var langFileData = null;
894 try { langFileData = JSON.parse(fs.readFileSync(langFile)); } catch (ex) { console.log(ex); }
895 if ((langFileData == null) || (langFileData.strings == null)) { log("Invalid language file."); process.exit(); return; }
898 // Build translation table, simple source->target for the given language.
899 var xtranslationTable = {};
900 for (var i in langFileData.strings) {
901 var entry = langFileData.strings[i];
902 for (var lang in entry) {
903 if ((lang == 'en') || (lang == 'xloc')) continue;
904 if ((xlang != null) && (lang != xlang)) continue;
905 if (languages.indexOf(lang) == -1) { languages.push(lang); xtranslationTable[lang] = {}; }
906 if ((entry['en'] != null) && (entry[lang] != null)) { xtranslationTable[lang][entry['en']] = entry[lang]; }
910 // Load and translate
911 var json = JSON.parse(fs.readFileSync(file).toString());
912 if (json.en != null) {
913 for (var j in languages) {
914 var lang = languages[j];
915 for (var i in json.en) {
916 if ((typeof json.en[i] == 'string') && (xtranslationTable[lang][json.en[i]] != null)) {
917 // Translate a string
918 if (json[lang] == null) { json[lang] = {}; }
919 json[lang][i] = xtranslationTable[lang][json.en[i]];
920 } else if (Array.isArray(json.en[i])) {
921 // Translate an array of strings
922 var r = [], translateCount = 0;
923 for (var k in json.en[i]) {
924 var str = json.en[i][k];
925 if (xtranslationTable[lang][str] != null) { r.push(xtranslationTable[lang][str]); translateCount++; } else { r.push(str); }
927 if (translateCount > 0) { json[lang][i] = r; }
934 fs.writeFileSync(file, JSON.stringify(json, null, 2), { flag: 'w+' });
937async function translateFromHtml(lang, file, createSubDir) {
938 var data = fs.readFileSync(file);
939 if (file.endsWith('.js')) { data = '<html><head></head><body><script>' + data + '</script></body></html>'; }
940 var { JSDOM } = jsdom;
941 const dom = new JSDOM(data, { includeNodeLocations: true });
942 log("Translating HTML (" + lang + "): " + path.basename(file));
943 translateStrings(path.basename(file), dom.window.document.querySelector('body'));
944 var out = dom.serialize();
946 // Change the <html lang="en"> tag.
947 out = out.split('<html lang="en"').join('<html lang="' + lang + '"');
950 var outnamemin = null;
951 if (createSubDir != null) {
952 var outfolder = path.join(path.dirname(file), createSubDir);
953 if (fs.existsSync(outfolder) == false) { fs.mkdirSync(outfolder); }
954 outname = path.join(path.dirname(file), createSubDir, path.basename(file));
956 if (outname.endsWith('.handlebars')) {
957 outnamemin = (outname.substring(0, outname.length - 11) + '-min_' + lang + '.handlebars');
958 outname = (outname.substring(0, outname.length - 11) + '_' + lang + '.handlebars');
959 } else if (outname.endsWith('.html')) {
960 outnamemin = (outname.substring(0, outname.length - 5) + '-min_' + lang + '.html');
961 outname = (outname.substring(0, outname.length - 5) + '_' + lang + '.html');
962 } else if (outname.endsWith('.htm')) {
963 outnamemin = (outname.substring(0, outname.length - 4) + '-min_' + lang + '.htm');
964 outname = (outname.substring(0, outname.length - 4) + '_' + lang + '.htm');
965 } else if (outname.endsWith('.js')) {
966 if (out.startsWith('<html><head></head><body><script>')) { out = out.substring(33); }
967 if (out.endsWith('</script></body></html>')) { out = out.substring(0, out.length - 23); }
968 outnamemin = (outname.substring(0, outname.length - 3) + '-min_' + lang + '.js');
969 outname = (outname.substring(0, outname.length - 3) + '_' + lang + '.js');
971 outnamemin = (outname + '_' + lang + '.min');
972 outname = (outname + '_' + lang);
974 fs.writeFileSync(outname, out, { flag: 'w+' });
977 if (minifyLib == 1) {
981 }, function(e, compress) {
982 if (e) { log('ERROR ', e); return done(); }
983 compress.run((e) => { e ? log('Minification fail', e) : log('Minification success'); minifyDone(); });
989 if (minifyLib == 2) {
990 if (outnamemin.endsWith('.handlebars') >= 0) { out = out.split('{{{pluginHandler}}}').join('"{{{pluginHandler}}}"'); }
991 var minifiedOut = await minify(out, {
992 collapseBooleanAttributes: true,
993 collapseInlineTagWhitespace: false, // This is not good.
994 collapseWhitespace: true,
997 removeComments: true,
998 removeOptionalTags: true,
999 removeEmptyAttributes: true,
1000 removeAttributeQuotes: true,
1001 removeRedundantAttributes: true,
1002 removeScriptTypeAttributes: true,
1003 removeTagWhitespace: true,
1004 preserveLineBreaks: false,
1005 useShortDoctype: true
1007 if (outnamemin.endsWith('.handlebars') >= 0) { minifiedOut = minifiedOut.split('"{{{pluginHandler}}}"').join('{{{pluginHandler}}}'); }
1008 fs.writeFileSync(outnamemin, minifiedOut, { flag: 'w+' });
1012function minifyDone() { log('Completed minification.'); }
1014function translateStrings(name, node) {
1015 for (var i = 0; i < node.childNodes.length; i++) {
1016 var subnode = node.childNodes[i];
1018 // Check if the "value" attribute exists and needs to be translated
1019 var subnodeignore = false;
1020 if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
1021 var subnodevalue = null, subnodeindex = null, subnodeplaceholder = null, subnodeplaceholderindex = null, subnodetitle = null, subnodetitleindex = null;
1022 for (var j in subnode.attributes) {
1023 if ((subnode.attributes[j].name == 'notrans') && (subnode.attributes[j].value == '1')) { subnodeignore = true; }
1024 if ((subnode.attributes[j].name == 'type') && (subnode.attributes[j].value == 'hidden')) { subnodeignore = true; }
1025 if (subnode.attributes[j].name == 'value') { subnodevalue = subnode.attributes[j].value; subnodeindex = j; }
1026 if (subnode.attributes[j].name == 'placeholder') { subnodeplaceholder = subnode.attributes[j].value; subnodeplaceholderindex = j; }
1027 if (subnode.attributes[j].name == 'title') { subnodetitle = subnode.attributes[j].value; subnodetitleindex = j; }
1029 if ((subnodevalue != null) && isNumber(subnodevalue) == true) { subnodevalue = null; }
1030 if ((subnodeplaceholder != null) && isNumber(subnodeplaceholder) == true) { subnodeplaceholder = null; }
1031 if ((subnodetitle != null) && isNumber(subnodetitle) == true) { subnodetitle = null; }
1032 if ((subnodeignore == false) && (subnodevalue != null)) {
1033 // Perform attribute translation for value
1034 if (translationTable[subnodevalue] != null) { subnode.attributes[subnodeindex].value = translationTable[subnodevalue]; }
1036 if (subnodeplaceholder != null) {
1037 // Perform attribute translation for placeholder
1038 if (translationTable[subnodeplaceholder] != null) { subnode.attributes[subnodeplaceholderindex].value = translationTable[subnodeplaceholder]; }
1040 if (subnodetitle != null) {
1041 // Perform attribute translation for title
1042 if (translationTable[subnodetitle] != null) { subnode.attributes[subnodetitleindex].value = translationTable[subnodetitle]; }
1046 if (subnodeignore == false) {
1047 var subname = subnode.id;
1048 if (subname == null || subname == '') { subname = i; }
1049 if (subnode.hasChildNodes()) {
1050 translateStrings(name + '->' + subname, subnode);
1052 if (subnode.nodeValue == null) continue;
1053 var nodeValue = subnode.nodeValue.trim().split('\\r').join('').split('\\n').join('').trim();
1055 // Look for the front trim
1056 var frontTrim = '', backTrim = '';;
1057 var x1 = subnode.nodeValue.indexOf(nodeValue);
1058 if (x1 > 0) { frontTrim = subnode.nodeValue.substring(0, x1); }
1059 if (x1 != -1) { backTrim = subnode.nodeValue.substring(x1 + nodeValue.length); }
1061 if ((nodeValue.length > 0) && (subnode.nodeType == 3)) {
1062 if ((node.tagName != 'SCRIPT') && (node.tagName != 'STYLE') && (nodeValue.length < 8000) && (nodeValue.startsWith('{{{') == false) && (nodeValue != ' ')) {
1063 // Check if we have a translation for this string
1064 if (translationTable[nodeValue]) { subnode.nodeValue = (frontTrim + translationTable[nodeValue] + backTrim); }
1065 } else if (node.tagName == 'SCRIPT') {
1066 // Translate JavaScript
1067 subnode.nodeValue = translateStringsFromJavaScript(name, subnode.nodeValue);
1075function translateStringsFromJavaScript(name, script) {
1076 if (performCheck) { log(format('Translating JavaScript of {0} bytes: {1}', script.length, name)); }
1077 var tokenScript = esprima.tokenize(script, { range: true }), count = 0;
1078 var output = [], ptr = 0;
1079 for (var i in tokenScript) {
1080 var token = tokenScript[i];
1081 if ((token.type == 'String') && (token.value.length > 2) && (token.value[0] == '"')) {
1082 var str = token.value.substring(1, token.value.length - 1);
1083 if (translationTable[str]) {
1084 output.push(script.substring(ptr, token.range[0]));
1085 output.push('"' + translationTable[str] + '"');
1086 ptr = token.range[1];
1090 output.push(script.substring(ptr));
1091 return output.join('');
1094function isNumber(x) { return (('' + parseInt(x)) === x) || (('' + parseFloat(x)) === x); }
1095function format(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); };
1099// Check if a list of modules are present and install any missing ones
1100var InstallModuleChildProcess = null;
1101function InstallModules(modules, func) {
1102 var missingModules = [];
1103 if (modules.length > 0) {
1104 for (var i in modules) {
1105 var moduleName = modules[i].split('@')[0];
1107 var xxmodule = require(moduleName);
1109 missingModules.push(modules[i]);
1112 if (missingModules.length > 0) {
1113 console.log('Missing modules: ' + missingModules.join(', ') + '.');
1114 InstallModuleEx(modules, func);
1119// Check if a module is present and install it if missing
1120function InstallModuleEx(modulenames, func) {
1121 log('Installing modules...');
1122 var names = modulenames.join(' ');
1123 var child_process = require('child_process');
1124 var parentpath = __dirname;
1126 // Get the working directory
1127 if ((__dirname.endsWith('/node_modules/meshcentral')) || (__dirname.endsWith('\\node_modules\\meshcentral')) || (__dirname.endsWith('/node_modules/meshcentral/')) || (__dirname.endsWith('\\node_modules\\meshcentral\\'))) { parentpath = require('path').join(__dirname, '../..'); }
1129 // Looks like we need to keep a global reference to the child process object for this to work correctly.
1130 InstallModuleChildProcess = child_process.exec(`npm install --no-audit --no-optional --omit=optional ${names}`, { maxBuffer: 512000, timeout: 300000, cwd: parentpath }, function (error, stdout, stderr) {
1131 InstallModuleChildProcess = null;
1132 if ((error != null) && (error != '')) {
1133 log('ERROR: Unable to install required modules. May not have access to npm, or npm may not have suffisent rights to load the new modules. Try "npm install ' + names + '" to manually install the modules.\r\n');
1142// Convert the translations to a standardized JSON we can use in GitHub
1143// Strings are sorder by english source and object keys are sorted
1144function translationsToJson(t) {
1145 var arr2 = [], arr = t.strings;
1146 for (var i in arr) {
1147 var names = [], el = arr[i], el2 = {};
1148 for (var j in el) { names.push(j); }
1149 names.sort(function (a, b) { if (a == b) { return 0; } if (a == 'xloc') { return 1; } if (b == 'xloc') { return -1; } return a - b });
1150 for (var j in names) { el2[names[j]] = el[names[j]]; }
1151 if (el2.xloc != null) { el2.xloc.sort(); }
1154 arr2.sort(function (a, b) { if (a.en > b.en) return 1; if (a.en < b.en) return -1; return 0; });
1155 return JSON.stringify({ strings: arr2 }, null, ' ');
1159module.exports.startEx = startEx;