Can now render non-english pages.

This commit is contained in:
Ylian Saint-Hilaire 2019-10-21 14:57:27 -07:00
parent 08142d91a7
commit 60aa387f70
2 changed files with 9596 additions and 82 deletions

View File

@ -11,9 +11,9 @@ var path = require('path');
var performCheck = false;
var translationTable = null;
var sourceStrings = null;
const jsdom = require('jsdom');
const esprima = require('esprima'); // https://www.npmjs.com/package/esprima
const { JSDOM } = jsdom;
var jsdom = null; //require('jsdom');
var esprima = null; //require('esprima'); // https://www.npmjs.com/package/esprima
//var { JSDOM } = jsdom;
var meshCentralSourceFiles = [
"../views/agentinvite.handlebars",
@ -34,6 +34,12 @@ var meshCentralSourceFiles = [
// node translate.json EXTRACT bob.json ../meshcentral/views/default.handlebars
// node translate.js TRANSLATE fr test2.json ../meshcentral/views/default.handlebars
InstallModules(['jsdom', 'esprima'], start);
function start() {
jsdom = require('jsdom');
esprima = require('esprima'); // https://www.npmjs.com/package/esprima
var command = null;
if (process.argv.length > 2) { command = process.argv[2].toLowerCase(); }
if (['check', 'extract', 'extractall', 'translate', 'translateall'].indexOf(command) == -1) { command = null; }
@ -107,6 +113,7 @@ if (command == 'translate') {
translate(lang, langFile, sources, false);
}
}
function translate(lang, langFile, sources, createSubDir) {
// Load the language file
@ -162,6 +169,7 @@ function extract(langFile, sources) {
function extractFromHtml(file) {
var data = fs.readFileSync(file);
var { JSDOM } = jsdom;
const dom = new JSDOM(data, { includeNodeLocations: true });
console.log("Processing HTML: " + path.basename(file));
getStrings(path.basename(file), dom.window.document.querySelector('body'));
@ -173,17 +181,28 @@ function getStrings(name, node) {
// Check if the "value" attribute exists and needs to be translated
if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
var subnodeignore = false;
var subnodevalue = null;
var subnodeignore = false, subnodevalue = null, subnodeplaceholder = null, subnodetitle = null;
for (var j in subnode.attributes) {
if ((subnode.attributes[j].name == 'type') && (subnode.attributes[j].value == 'hidden')) { subnodeignore = true; }
if (subnode.attributes[j].name == 'value') { subnodevalue = subnode.attributes[j].value; }
if (subnode.attributes[j].name == 'placeholder') { subnodeplaceholder = subnode.attributes[j].value; }
if (subnode.attributes[j].name == 'title') { subnodetitle = subnode.attributes[j].value; }
}
if ((subnodevalue != null) && isNumber(subnodevalue) == true) { subnodeignore = true; }
if ((subnodevalue != null) && isNumber(subnodevalue) == true) { subnodevalue = null; }
if ((subnodeplaceholder != null) && isNumber(subnodeplaceholder) == true) { subnodeplaceholder = null; }
if ((subnodetitle != null) && isNumber(subnodetitle) == true) { subnodetitle = null; }
if ((subnodeignore == false) && (subnodevalue != null)) {
// Add a new string to the list
// Add a new string to the list (value)
if (sourceStrings[subnodevalue] == null) { sourceStrings[subnodevalue] = { en: subnodevalue, xloc: [name] }; } else { if (sourceStrings[subnodevalue].xloc == null) { sourceStrings[subnodevalue].xloc = []; } sourceStrings[subnodevalue].xloc.push(name); }
}
if (subnodeplaceholder != null) {
// Add a new string to the list (placeholder)
if (sourceStrings[subnodeplaceholder] == null) { sourceStrings[subnodeplaceholder] = { en: subnodeplaceholder, xloc: [name] }; } else { if (sourceStrings[subnodeplaceholder].xloc == null) { sourceStrings[subnodeplaceholder].xloc = []; } sourceStrings[subnodeplaceholder].xloc.push(name); }
}
if (subnodetitle != null) {
// Add a new string to the list (title)
if (sourceStrings[subnodetitle] == null) { sourceStrings[subnodetitle] = { en: subnodetitle, xloc: [name] }; } else { if (sourceStrings[subnodetitle].xloc == null) { sourceStrings[subnodetitle].xloc = []; } sourceStrings[subnodetitle].xloc.push(name); }
}
}
// Check the content of the element
@ -228,6 +247,7 @@ function getStringFromJavaScript(name, script) {
function translateFromHtml(lang, file, createSubDir) {
var data = fs.readFileSync(file);
var { JSDOM } = jsdom;
const dom = new JSDOM(data, { includeNodeLocations: true });
console.log("Translating HTML: " + path.basename(file));
translateStrings(path.basename(file), dom.window.document.querySelector('body'));
@ -235,10 +255,10 @@ function translateFromHtml(lang, file, createSubDir) {
var outname = file;
if (createSubDir != null) { outname = path.join(path.dirname(file), createSubDir, path.basename(file)); }
if (outname.endsWith('.handlebars')) { outname = (outname.substring(0, outname.length - 11) + '-' + lang + '.handlebars'); }
else if (outname.endsWith('.html')) { outname = (outname.substring(0, outname.length - 5) + '-' + lang + '.html'); }
else if (outname.endsWith('.htm')) { outname = (outname.substring(0, outname.length - 4) + '-' + lang + '.htm'); }
else { outname = (outname + '-' + lang); }
if (outname.endsWith('.handlebars')) { outname = (outname.substring(0, outname.length - 11) + '_' + lang + '.handlebars'); }
else if (outname.endsWith('.html')) { outname = (outname.substring(0, outname.length - 5) + '_' + lang + '.html'); }
else if (outname.endsWith('.htm')) { outname = (outname.substring(0, outname.length - 4) + '_' + lang + '.htm'); }
else { outname = (outname + '_' + lang); }
fs.writeFileSync(outname, out, { flag: 'w+' });
}
@ -248,16 +268,28 @@ function translateStrings(name, node) {
// Check if the "value" attribute exists and needs to be translated
if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
var subnodeignore = false, subnodevalue = null, subnodeindex = null;
var subnodeignore = false, subnodevalue = null, subnodeindex = null, subnodeplaceholder = null, subnodeplaceholderindex = null, subnodetitle = null, subnodetitleindex = null;
for (var j in subnode.attributes) {
if ((subnode.attributes[j].name == 'type') && (subnode.attributes[j].value == 'hidden')) { subnodeignore = true; }
if (subnode.attributes[j].name == 'value') { subnodevalue = subnode.attributes[j].value; subnodeindex = j; }
if (subnode.attributes[j].name == 'placeholder') { subnodeplaceholder = subnode.attributes[j].value; subnodeplaceholderindex = j; }
if (subnode.attributes[j].name == 'title') { subnodetitle = subnode.attributes[j].value; subnodetitleindex = j; }
}
if ((subnodevalue != null) && isNumber(subnodevalue) == true) { subnodeignore = true; }
if ((subnodevalue != null) && isNumber(subnodevalue) == true) { subnodevalue = null; }
if ((subnodeplaceholder != null) && isNumber(subnodeplaceholder) == true) { subnodeplaceholder = null; }
if ((subnodetitle != null) && isNumber(subnodetitle) == true) { subnodetitle = null; }
if ((subnodeignore == false) && (subnodevalue != null)) {
// Perform attribute translation
// Perform attribute translation for value
if (translationTable[subnodevalue] != null) { subnode.attributes[subnodeindex].value = translationTable[subnodevalue]; }
}
if (subnodeplaceholder != null) {
// Perform attribute translation for placeholder
if (translationTable[subnodeplaceholder] != null) { subnode.attributes[subnodeplaceholderindex].value = translationTable[subnodeplaceholder]; }
}
if (subnodetitle != null) {
// Perform attribute translation for title
if (translationTable[subnodetitle] != null) { subnode.attributes[subnodetitleindex].value = translationTable[subnodetitle]; }
}
}
var subname = subnode.id;
@ -267,10 +299,17 @@ function translateStrings(name, node) {
} else {
if (subnode.nodeValue == null) continue;
var nodeValue = subnode.nodeValue.trim().split('\\r').join('').split('\\n').join('').trim();
// Look for the front trim
var frontTrim = '', backTrim = '';;
var x1 = subnode.nodeValue.indexOf(nodeValue);
if (x1 > 0) { frontTrim = subnode.nodeValue.substring(0, x1); }
if (x1 != -1) { backTrim = subnode.nodeValue.substring(x1 + nodeValue.length); }
if ((nodeValue.length > 0) && (subnode.nodeType == 3)) {
if ((node.tagName != 'SCRIPT') && (node.tagName != 'STYLE') && (nodeValue.length < 8000) && (nodeValue.startsWith('{{{') == false) && (nodeValue != ' ')) {
// Check if we have a translation for this string
if (translationTable[nodeValue]) { subnode.nodeValue = translationTable[nodeValue]; }
if (translationTable[nodeValue]) { subnode.nodeValue = (frontTrim + translationTable[nodeValue] + backTrim); }
} else if (node.tagName == 'SCRIPT') {
// Translate JavaScript
subnode.nodeValue = translateStringsFromJavaScript(name, subnode.nodeValue);
@ -301,3 +340,46 @@ function translateStringsFromJavaScript(name, script) {
function isNumber(x) { return (('' + parseInt(x)) === x) || (('' + parseFloat(x)) === x); }
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; }); };
// Check if a list of modules are present and install any missing ones
var InstallModuleChildProcess = null;
var previouslyInstalledModules = {};
function InstallModules(modules, func) {
var missingModules = [];
if (previouslyInstalledModules == null) { previouslyInstalledModules = {}; }
if (modules.length > 0) {
for (var i in modules) {
try {
var xxmodule = require(modules[i]);
} catch (e) {
if (previouslyInstalledModules[modules[i]] !== true) { missingModules.push(modules[i]); }
}
}
if (missingModules.length > 0) { InstallModule(missingModules.shift(), InstallModules, modules, func); } else { func(); }
}
}
// Check if a module is present and install it if missing
function InstallModule(modulename, func, tag1, tag2) {
console.log('Installing ' + modulename + '...');
var child_process = require('child_process');
var parentpath = __dirname;
// Get the working directory
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, '../..'); }
// Looks like we need to keep a global reference to the child process object for this to work correctly.
InstallModuleChildProcess = child_process.exec('npm install --no-optional --save ' + modulename, { maxBuffer: 512000, timeout: 10000, cwd: parentpath }, function (error, stdout, stderr) {
InstallModuleChildProcess = null;
if ((error != null) && (error != '')) {
console.log('ERROR: Unable to install required module "' + modulename + '". May not have access to npm, or npm may not have suffisent rights to load the new module. Try "npm install ' + modulename + '" to manualy install this module.\r\n');
process.exit();
return;
}
previouslyInstalledModules[modulename] = true;
func(tag1, tag2);
return;
});
}

File diff suppressed because one or more lines are too long