| 1 | /** |
| 2 | * @description MeshCentral MeshAgent |
| 3 | * @author Ylian Saint-Hilaire |
| 4 | * @copyright Intel Corporation 2019-2021 |
| 5 | * @license Apache-2.0 |
| 6 | * @version v0.0.1 |
| 7 | */ |
| 8 | |
| 9 | const fs = require('fs'); |
| 10 | const path = require('path'); |
| 11 | const os = require('os'); |
| 12 | //const zlib = require('zlib'); |
| 13 | var performCheck = false; |
| 14 | var translationTable = null; |
| 15 | var sourceStrings = null; |
| 16 | var jsdom = null; //require('jsdom'); |
| 17 | var esprima = null; //require('esprima'); // https://www.npmjs.com/package/esprima |
| 18 | var minifyLib = 2; // 0 = None, 1 = minify-js, 2 = HTMLMinifier |
| 19 | var minify = null; |
| 20 | |
| 21 | var 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" |
| 62 | ]; |
| 63 | |
| 64 | var 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" |
| 112 | ]; |
| 113 | |
| 114 | // True is this module is run directly using NodeJS |
| 115 | var directRun = (require.main === module); |
| 116 | |
| 117 | // Check NodeJS version |
| 118 | const NodeJSVer = Number(process.version.match(/^v(\d+\.\d+)/)[1]); |
| 119 | if (directRun && (NodeJSVer < 8)) { log("Translate.js requires Node v8 or above, current version is " + process.version + "."); return; } |
| 120 | |
| 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 |
| 124 | |
| 125 | var worker = null; |
| 126 | function log() { |
| 127 | if (worker == null) { |
| 128 | console.log(...arguments); |
| 129 | } else { |
| 130 | worker.parentPort.postMessage({ msg: arguments[0] }) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (directRun && (NodeJSVer >= 12)) { |
| 135 | const xworker = require('worker_threads'); |
| 136 | try { |
| 137 | if (xworker.isMainThread == false) { |
| 138 | // We are being called to do some work |
| 139 | worker = xworker; |
| 140 | const op = worker.workerData.op; |
| 141 | const args = worker.workerData.args; |
| 142 | |
| 143 | // Get things setup |
| 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 |
| 148 | |
| 149 | switch (op) { |
| 150 | case 'translate': { |
| 151 | translateSingleThreaded(args[0], args[1], args[2], args[3]); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | return; |
| 156 | } |
| 157 | } catch (ex) { log(ex); } |
| 158 | } |
| 159 | |
| 160 | if (directRun) { setup(); } |
| 161 | |
| 162 | function 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); |
| 167 | } |
| 168 | |
| 169 | function start() { startEx(process.argv); } |
| 170 | |
| 171 | async function startEx(argv) { |
| 172 | // Load dependencies |
| 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 |
| 177 | |
| 178 | var command = null; |
| 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; } |
| 181 | |
| 182 | if (directRun) { log('MeshCentral web site translator'); } |
| 183 | if (command == null) { |
| 184 | log('Usage "node translate.js [command] [options]'); |
| 185 | log('Possible commands:'); |
| 186 | log(''); |
| 187 | log(' CHECK [files]'); |
| 188 | log(' Check will pull string out of a web page and display a report.'); |
| 189 | log(''); |
| 190 | log(' EXTRACT [languagefile] [files]'); |
| 191 | log(' Extract strings from web pages and generate a language (.json) file.'); |
| 192 | log(''); |
| 193 | log(' EXTRACTALL (languagefile)'); |
| 194 | log(' Extract all MeshCentral strings from web pages and generate the languages.json file.'); |
| 195 | log(''); |
| 196 | log(' TRANSLATE [language] [languagefile] [files]'); |
| 197 | log(' Use a language (.json) file to translate web pages to a give language.'); |
| 198 | log(''); |
| 199 | log(' TRANSLATEALL (languagefile) (language code)'); |
| 200 | log(' Translate all MeshCentral strings using the languages.json file.'); |
| 201 | log(''); |
| 202 | log(' MINIFY [sourcefile]'); |
| 203 | log(' Minify a single file.'); |
| 204 | log(''); |
| 205 | log(' MINIFYDIR [sourcedir] [destinationdir]'); |
| 206 | log(' Minify all files in a directory.'); |
| 207 | log(''); |
| 208 | log(' MINIFYALL'); |
| 209 | log(' Minify the main MeshCentral english web pages.'); |
| 210 | log(''); |
| 211 | log(' MERGE [sourcefile] [targetfile] [language code]'); |
| 212 | log(' Merge a language from a translation file into another translation file.'); |
| 213 | log(''); |
| 214 | log(' TOTEXT [translationfile] [textfile] [language code]'); |
| 215 | log(' Save a text for with all strings of a given language.'); |
| 216 | log(''); |
| 217 | log(' FROMTEXT [translationfile] [textfile] [language code]'); |
| 218 | log(' Import raw text string as translations for a language code.'); |
| 219 | process.exit(); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | // Extract strings from web pages and display a report |
| 224 | if (command == 'check') { |
| 225 | var sources = []; |
| 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; } |
| 228 | performCheck = true; |
| 229 | sourceStrings = {}; |
| 230 | for (var i = 0; i < sources.length; i++) { extractFromHtml(sources[i]); } |
| 231 | var count = 0; |
| 232 | for (var i in sourceStrings) { count++; } |
| 233 | log('Extracted ' + count + ' strings.'); |
| 234 | process.exit(); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Extract strings from web pages |
| 239 | if (command == 'extract') { |
| 240 | if (argv.length < 4) { log('No language file specified.'); process.exit(); return; } |
| 241 | var sources = []; |
| 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); |
| 245 | } |
| 246 | |
| 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]); |
| 252 | } else { |
| 253 | log('Usage: TOTEXT [translationfile] [textfile] [language code]'); |
| 254 | } |
| 255 | return; |
| 256 | } |
| 257 | |
| 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]); |
| 264 | } else { |
| 265 | log('Usage: FROMTEXT [translationfile] [textfile] [language code]'); |
| 266 | } |
| 267 | return; |
| 268 | } |
| 269 | |
| 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]); |
| 276 | } else { |
| 277 | log('Usage: MERGE [sourcefile] [tartgetfile] [language code]'); |
| 278 | } |
| 279 | return; |
| 280 | } |
| 281 | |
| 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]; } |
| 288 | } |
| 289 | extract(translationFile, meshCentralSourceFiles, translationFile); |
| 290 | } |
| 291 | |
| 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]; } |
| 299 | } |
| 300 | sourceStrings = {}; |
| 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+' }); |
| 307 | log("Done."); |
| 308 | return; |
| 309 | } |
| 310 | |
| 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'); } |
| 314 | var lang = null; |
| 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]); |
| 319 | } else { |
| 320 | translate(lang, argv[3], meshCentralSourceFiles, 'translations'); |
| 321 | } |
| 322 | } else { |
| 323 | if (fs.existsSync('translate.json') == false) { |
| 324 | log('Unable to find translate.json.'); |
| 325 | } else { |
| 326 | translate(lang, 'translate.json', meshCentralSourceFiles, 'translations'); |
| 327 | } |
| 328 | } |
| 329 | return; |
| 330 | } |
| 331 | |
| 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; } |
| 339 | |
| 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); |
| 344 | } else { |
| 345 | if (fs.existsSync(argv[i]) == false) { log("Missing file: " + argv[i]); process.exit(); return; } sources.push(argv[i]); |
| 346 | } |
| 347 | } |
| 348 | if (sources.length == 0) { log("No source files specified."); process.exit(); return; } |
| 349 | translate(lang, langFile, sources, subdir); |
| 350 | } |
| 351 | |
| 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(); |
| 364 | |
| 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>'; } |
| 368 | |
| 369 | var minifiedOut = await minify(inFile, { |
| 370 | collapseBooleanAttributes: true, |
| 371 | collapseInlineTagWhitespace: false, // This is not good. |
| 372 | collapseWhitespace: true, |
| 373 | minifyCSS: true, |
| 374 | minifyJS: 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 |
| 384 | }); |
| 385 | |
| 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+' }); |
| 390 | } |
| 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+' }); |
| 396 | } |
| 397 | |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 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'); |
| 414 | } else { |
| 415 | outnamemin = (outname, outname + '.min'); |
| 416 | } |
| 417 | log('Generating ' + outnamemin + '...'); |
| 418 | |
| 419 | /* |
| 420 | // Minify the file |
| 421 | if (minifyLib == 1) { |
| 422 | minify.file({ |
| 423 | file: outname, |
| 424 | dist: outnamemin |
| 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(); }); |
| 428 | } |
| 429 | ); |
| 430 | } |
| 431 | */ |
| 432 | |
| 433 | // Minify the file |
| 434 | if (minifyLib == 2) { |
| 435 | var inFile = fs.readFileSync(outname).toString(); |
| 436 | |
| 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>'; } |
| 440 | |
| 441 | var minifiedOut = null; |
| 442 | try { |
| 443 | minifiedOut = await minify(inFile, { |
| 444 | collapseBooleanAttributes: true, |
| 445 | collapseInlineTagWhitespace: false, // This is not good. |
| 446 | collapseWhitespace: true, |
| 447 | minifyCSS: true, |
| 448 | minifyJS: 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 |
| 458 | }); |
| 459 | } catch (ex) { |
| 460 | console.log(ex); |
| 461 | } |
| 462 | |
| 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+' }); |
| 467 | |
| 468 | /* |
| 469 | if (outname.endsWith('.js')) { |
| 470 | var compressHandler = function compressHandlerFunc(err, buffer, outnamemin2) { |
| 471 | if (err == null) { |
| 472 | console.log('GZIP', compressHandlerFunc.outname); |
| 473 | fs.writeFileSync(compressHandlerFunc.outname, buffer, { flag: 'w+' }); |
| 474 | } |
| 475 | }; |
| 476 | compressHandler.outname = outnamemin; |
| 477 | zlib.gzip(Buffer.from(minifiedOut), compressHandler); |
| 478 | } else { |
| 479 | fs.writeFileSync(outnamemin, minifiedOut, { flag: 'w+' }); |
| 480 | } |
| 481 | */ |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 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'); |
| 497 | } else { |
| 498 | outnamemin = (outname, outname + '.min'); |
| 499 | } |
| 500 | log('Generating ' + path.basename(outnamemin) + '...'); |
| 501 | |
| 502 | // Minify the file |
| 503 | if (minifyLib == 2) { |
| 504 | var inFile = fs.readFileSync(outname).toString() |
| 505 | |
| 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>'; } |
| 509 | |
| 510 | var minifiedOut = await minify(inFile, { |
| 511 | collapseBooleanAttributes: true, |
| 512 | collapseInlineTagWhitespace: false, // This is not good. |
| 513 | collapseWhitespace: true, |
| 514 | minifyCSS: true, |
| 515 | minifyJS: 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 |
| 525 | }); |
| 526 | |
| 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+' }); |
| 531 | |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | |
| 537 | function 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; } |
| 542 | |
| 543 | log('Writing ' + lang + '...'); |
| 544 | |
| 545 | // Generate raw text |
| 546 | var output = []; |
| 547 | var outputCharCount = 0; // Google has a 5000 character limit |
| 548 | var splitOutput = []; |
| 549 | var splitOutputPtr = 1; |
| 550 | var count = 0; |
| 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]); |
| 558 | } else { |
| 559 | output.push(''); |
| 560 | outputCharCount += 2; |
| 561 | if (outputCharCount > 4500) { outputCharCount = 0; splitOutputPtr++; } |
| 562 | if (splitOutput[splitOutputPtr] == null) { splitOutput[splitOutputPtr] = []; } |
| 563 | splitOutput[splitOutputPtr].push(''); |
| 564 | } |
| 565 | count++; |
| 566 | } |
| 567 | |
| 568 | if (splitOutputPtr == 1) { |
| 569 | // Save the target back |
| 570 | fs.writeFileSync(target + '-' + lang + '.txt', output.join(os.EOL), { flag: 'w+' }); |
| 571 | log('Done.'); |
| 572 | } else { |
| 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+' }); |
| 577 | } |
| 578 | log('Done.'); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | function 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; } |
| 587 | |
| 588 | log('Updating ' + lang + '...'); |
| 589 | |
| 590 | // Read raw text |
| 591 | var rawText = fs.readFileSync(target).toString('utf8'); |
| 592 | var rawTextArray = rawText.split(/\r?\n/); |
| 593 | var rawTextPtr = 0; |
| 594 | |
| 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; } |
| 598 | |
| 599 | var output = []; |
| 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]; } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | fs.writeFileSync(source + '-new', translationsToJson(sourceLangFileData), { flag: 'w+' }); |
| 608 | log('Done.'); |
| 609 | } |
| 610 | |
| 611 | function 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; } |
| 616 | |
| 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; } |
| 621 | |
| 622 | log('Merging ' + lang + '...'); |
| 623 | |
| 624 | // Index the target file |
| 625 | var index = {}; |
| 626 | for (var i in targetLangFileData.strings) { if (targetLangFileData.strings[i].en != null) { index[targetLangFileData.strings[i].en] = targetLangFileData.strings[i]; } } |
| 627 | |
| 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]; |
| 633 | //} |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // Deindex the new target file |
| 638 | var targetData = { strings: [] }; |
| 639 | for (var i in index) { targetData.strings.push(index[i]); } |
| 640 | |
| 641 | // Save the target back |
| 642 | fs.writeFileSync(target, translationsToJson(targetData), { flag: 'w+' }); |
| 643 | log('Done.'); |
| 644 | } |
| 645 | |
| 646 | function translate(lang, langFile, sources, createSubDir) { |
| 647 | if (directRun && (NodeJSVer >= 12) && (lang == null)) { |
| 648 | // Multi threaded translation |
| 649 | log("Multi-threaded translation."); |
| 650 | |
| 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; } |
| 655 | |
| 656 | langs = {}; |
| 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; |
| 661 | var taskQueue = []; |
| 662 | function processNextTask() { |
| 663 | if (activeWorkers < MAX_WORKERS && taskQueue.length > 0) { |
| 664 | var nextTask = taskQueue.shift(); |
| 665 | activeWorkers++; |
| 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(); }); |
| 671 | } |
| 672 | } |
| 673 | for (var lang in langs) { |
| 674 | if (langs.hasOwnProperty(lang)) { |
| 675 | taskQueue.push({ lang: lang, langFile: langFile, sources: sources, createSubDir: createSubDir}); |
| 676 | } |
| 677 | } |
| 678 | for (var i = 0; i < Math.min(MAX_WORKERS, taskQueue.length); i++) { processNextTask(); } |
| 679 | } else { |
| 680 | // Single threaded translation |
| 681 | translateSingleThreaded(lang, langFile, sources, createSubDir); |
| 682 | } |
| 683 | |
| 684 | // Translate any JSON files |
| 685 | for (var i = 0; i < sources.length; i++) { if (sources[i].endsWith('.json')) { translateAllInJson(lang, langFile, sources[i]); } } |
| 686 | } |
| 687 | |
| 688 | function 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; } |
| 693 | |
| 694 | if ((lang != null) && (lang != '*')) { |
| 695 | // Translate a single language |
| 696 | translateEx(lang, langFileData, sources, createSubDir); |
| 697 | } else { |
| 698 | // See that languages are in the translation file |
| 699 | langs = {}; |
| 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); } |
| 702 | } |
| 703 | |
| 704 | //process.exit(); |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | function 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]; } |
| 714 | } |
| 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); } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | function extract(langFile, sources) { |
| 723 | sourceStrings = {}; |
| 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; |
| 731 | } |
| 732 | } |
| 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]); } |
| 737 | } |
| 738 | var count = 0, output = []; |
| 739 | for (var i in sourceStrings) { |
| 740 | count++; |
| 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 |
| 744 | } |
| 745 | fs.writeFileSync(langFile, translationsToJson({ strings: output }), { flag: 'w+' }); |
| 746 | log(format("{0} strings in output file.", count)); |
| 747 | //process.exit(); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | function 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) { |
| 756 | var line = lines[i]; |
| 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); } |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | function 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] }; |
| 773 | } else { |
| 774 | if (sourceStrings[str].xloc == null) { sourceStrings[str].xloc = []; } sourceStrings[str].xloc.push(name); |
| 775 | } |
| 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); } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | function 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')); |
| 793 | } |
| 794 | |
| 795 | function getStringsHtml(name, node) { |
| 796 | for (var i = 0; i < node.childNodes.length; i++) { |
| 797 | var subnode = node.childNodes[i]; |
| 798 | |
| 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; } |
| 811 | } |
| 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); } |
| 818 | } |
| 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); } |
| 822 | } |
| 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); } |
| 826 | } |
| 827 | } |
| 828 | |
| 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); |
| 835 | } else { |
| 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') { |
| 844 | // Parse JavaScript |
| 845 | getStringFromJavaScript(name, subnode.nodeValue); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | function 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)); } |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | function 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) { |
| 871 | var line = lines[i]; |
| 872 | if ((line.length > 1) && (line[0] != '~')) { |
| 873 | if (translationTable[line] != null) { outlines.push(translationTable[line]); } else { outlines.push(line); } |
| 874 | } else { |
| 875 | outlines.push(line); |
| 876 | } |
| 877 | } |
| 878 | |
| 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)); |
| 884 | } |
| 885 | outname = (outname.substring(0, outname.length - 4) + '_' + lang + '.txt'); |
| 886 | fs.writeFileSync(outname, out, { flag: 'w+' }); |
| 887 | } |
| 888 | |
| 889 | function translateAllInJson(xlang, langFile, file) { |
| 890 | log("Translating JSON (" + ((xlang == null)?'All':xlang) + "): " + path.basename(file)); |
| 891 | |
| 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; } |
| 896 | var languages = []; |
| 897 | |
| 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]; } |
| 907 | } |
| 908 | } |
| 909 | |
| 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); } |
| 926 | } |
| 927 | if (translateCount > 0) { json[lang][i] = r; } |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | // Save the results |
| 934 | fs.writeFileSync(file, JSON.stringify(json, null, 2), { flag: 'w+' }); |
| 935 | } |
| 936 | |
| 937 | async 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(); |
| 945 | |
| 946 | // Change the <html lang="en"> tag. |
| 947 | out = out.split('<html lang="en"').join('<html lang="' + lang + '"'); |
| 948 | |
| 949 | var outname = file; |
| 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)); |
| 955 | } |
| 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'); |
| 970 | } else { |
| 971 | outnamemin = (outname + '_' + lang + '.min'); |
| 972 | outname = (outname + '_' + lang); |
| 973 | } |
| 974 | fs.writeFileSync(outname, out, { flag: 'w+' }); |
| 975 | |
| 976 | // Minify the file |
| 977 | if (minifyLib == 1) { |
| 978 | minify.file({ |
| 979 | file: outname, |
| 980 | dist: outnamemin |
| 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(); }); |
| 984 | } |
| 985 | ); |
| 986 | } |
| 987 | |
| 988 | // Minify the file |
| 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, |
| 995 | minifyCSS: true, |
| 996 | minifyJS: 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 |
| 1006 | }); |
| 1007 | if (outnamemin.endsWith('.handlebars') >= 0) { minifiedOut = minifiedOut.split('"{{{pluginHandler}}}"').join('{{{pluginHandler}}}'); } |
| 1008 | fs.writeFileSync(outnamemin, minifiedOut, { flag: 'w+' }); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | function minifyDone() { log('Completed minification.'); } |
| 1013 | |
| 1014 | function translateStrings(name, node) { |
| 1015 | for (var i = 0; i < node.childNodes.length; i++) { |
| 1016 | var subnode = node.childNodes[i]; |
| 1017 | |
| 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; } |
| 1028 | } |
| 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]; } |
| 1035 | } |
| 1036 | if (subnodeplaceholder != null) { |
| 1037 | // Perform attribute translation for placeholder |
| 1038 | if (translationTable[subnodeplaceholder] != null) { subnode.attributes[subnodeplaceholderindex].value = translationTable[subnodeplaceholder]; } |
| 1039 | } |
| 1040 | if (subnodetitle != null) { |
| 1041 | // Perform attribute translation for title |
| 1042 | if (translationTable[subnodetitle] != null) { subnode.attributes[subnodetitleindex].value = translationTable[subnodetitle]; } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 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); |
| 1051 | } else { |
| 1052 | if (subnode.nodeValue == null) continue; |
| 1053 | var nodeValue = subnode.nodeValue.trim().split('\\r').join('').split('\\n').join('').trim(); |
| 1054 | |
| 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); } |
| 1060 | |
| 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); |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | function 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]; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | output.push(script.substring(ptr)); |
| 1091 | return output.join(''); |
| 1092 | } |
| 1093 | |
| 1094 | function isNumber(x) { return (('' + parseInt(x)) === x) || (('' + parseFloat(x)) === x); } |
| 1095 | function 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; }); }; |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | // Check if a list of modules are present and install any missing ones |
| 1100 | var InstallModuleChildProcess = null; |
| 1101 | function InstallModules(modules, func) { |
| 1102 | var missingModules = []; |
| 1103 | if (modules.length > 0) { |
| 1104 | for (var i in modules) { |
| 1105 | var moduleName = modules[i].split('@')[0]; |
| 1106 | try { |
| 1107 | var xxmodule = require(moduleName); |
| 1108 | } catch (e) { |
| 1109 | missingModules.push(modules[i]); |
| 1110 | } |
| 1111 | } |
| 1112 | if (missingModules.length > 0) { |
| 1113 | console.log('Missing modules: ' + missingModules.join(', ') + '.'); |
| 1114 | InstallModuleEx(modules, func); |
| 1115 | } else { func(); } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | // Check if a module is present and install it if missing |
| 1120 | function InstallModuleEx(modulenames, func) { |
| 1121 | log('Installing modules...'); |
| 1122 | var names = modulenames.join(' '); |
| 1123 | var child_process = require('child_process'); |
| 1124 | var parentpath = __dirname; |
| 1125 | |
| 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, '../..'); } |
| 1128 | |
| 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'); |
| 1134 | process.exit(); |
| 1135 | return; |
| 1136 | } |
| 1137 | func(); |
| 1138 | return; |
| 1139 | }); |
| 1140 | } |
| 1141 | |
| 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 |
| 1144 | function 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(); } |
| 1152 | arr2.push(el2); |
| 1153 | } |
| 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, ' '); |
| 1156 | } |
| 1157 | |
| 1158 | // Export table |
| 1159 | module.exports.startEx = startEx; |