mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-24 22:25:52 -05:00
Can now render non-english pages.
This commit is contained in:
parent
08142d91a7
commit
60aa387f70
@ -11,9 +11,9 @@ var path = require('path');
|
|||||||
var performCheck = false;
|
var performCheck = false;
|
||||||
var translationTable = null;
|
var translationTable = null;
|
||||||
var sourceStrings = null;
|
var sourceStrings = null;
|
||||||
const jsdom = require('jsdom');
|
var jsdom = null; //require('jsdom');
|
||||||
const esprima = require('esprima'); // https://www.npmjs.com/package/esprima
|
var esprima = null; //require('esprima'); // https://www.npmjs.com/package/esprima
|
||||||
const { JSDOM } = jsdom;
|
//var { JSDOM } = jsdom;
|
||||||
|
|
||||||
var meshCentralSourceFiles = [
|
var meshCentralSourceFiles = [
|
||||||
"../views/agentinvite.handlebars",
|
"../views/agentinvite.handlebars",
|
||||||
@ -34,12 +34,18 @@ var meshCentralSourceFiles = [
|
|||||||
// node translate.json EXTRACT bob.json ../meshcentral/views/default.handlebars
|
// node translate.json EXTRACT bob.json ../meshcentral/views/default.handlebars
|
||||||
// node translate.js TRANSLATE fr test2.json ../meshcentral/views/default.handlebars
|
// node translate.js TRANSLATE fr test2.json ../meshcentral/views/default.handlebars
|
||||||
|
|
||||||
var command = null;
|
InstallModules(['jsdom', 'esprima'], start);
|
||||||
if (process.argv.length > 2) { command = process.argv[2].toLowerCase(); }
|
|
||||||
if (['check', 'extract', 'extractall', 'translate', 'translateall'].indexOf(command) == -1) { command = null; }
|
|
||||||
|
|
||||||
console.log('MeshCentral web site translator');
|
function start() {
|
||||||
if (command == null) {
|
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; }
|
||||||
|
|
||||||
|
console.log('MeshCentral web site translator');
|
||||||
|
if (command == null) {
|
||||||
console.log('Usage "node translate.js [command] [options]');
|
console.log('Usage "node translate.js [command] [options]');
|
||||||
console.log('Possible commands:');
|
console.log('Possible commands:');
|
||||||
console.log('');
|
console.log('');
|
||||||
@ -59,10 +65,10 @@ if (command == null) {
|
|||||||
console.log(' Translate all MeshCentral strings using the languages.json file.');
|
console.log(' Translate all MeshCentral strings using the languages.json file.');
|
||||||
process.exit();
|
process.exit();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract strings from web pages and display a report
|
// Extract strings from web pages and display a report
|
||||||
if (command == 'check') {
|
if (command == 'check') {
|
||||||
var sources = [];
|
var sources = [];
|
||||||
for (var i = 3; i < process.argv.length; i++) { if (fs.existsSync(process.argv[i]) == false) { console.log('Missing file: ' + process.argv[i]); process.exit(); return; } sources.push(process.argv[i]); }
|
for (var i = 3; i < process.argv.length; i++) { if (fs.existsSync(process.argv[i]) == false) { console.log('Missing file: ' + process.argv[i]); process.exit(); return; } sources.push(process.argv[i]); }
|
||||||
if (sources.length == 0) { console.log('No source files specified.'); process.exit(); return; }
|
if (sources.length == 0) { console.log('No source files specified.'); process.exit(); return; }
|
||||||
@ -74,27 +80,27 @@ if (command == 'check') {
|
|||||||
console.log('Extracted ' + count + ' strings.');
|
console.log('Extracted ' + count + ' strings.');
|
||||||
process.exit();
|
process.exit();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract strings from web pages
|
// Extract strings from web pages
|
||||||
if (command == 'extract') {
|
if (command == 'extract') {
|
||||||
if (process.argv.length < 4) { console.log('No language file specified.'); process.exit(); return; }
|
if (process.argv.length < 4) { console.log('No language file specified.'); process.exit(); return; }
|
||||||
var sources = [];
|
var sources = [];
|
||||||
for (var i = 4; i < process.argv.length; i++) { if (fs.existsSync(process.argv[i]) == false) { console.log('Missing file: ' + process.argv[i]); process.exit(); return; } sources.push(process.argv[i]); }
|
for (var i = 4; i < process.argv.length; i++) { if (fs.existsSync(process.argv[i]) == false) { console.log('Missing file: ' + process.argv[i]); process.exit(); return; } sources.push(process.argv[i]); }
|
||||||
if (sources.length == 0) { console.log('No source files specified.'); process.exit(); return; }
|
if (sources.length == 0) { console.log('No source files specified.'); process.exit(); return; }
|
||||||
extract(process.argv[3], sources);
|
extract(process.argv[3], sources);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract or translate all MeshCentral strings
|
// Extract or translate all MeshCentral strings
|
||||||
if (command == 'extractall') { extract("translate.json", meshCentralSourceFiles); }
|
if (command == 'extractall') { extract("translate.json", meshCentralSourceFiles); }
|
||||||
if (command == 'translateall') {
|
if (command == 'translateall') {
|
||||||
if (fs.existsSync("../views/translations") == false) { fs.mkdirSync("../views/translations"); }
|
if (fs.existsSync("../views/translations") == false) { fs.mkdirSync("../views/translations"); }
|
||||||
if (fs.existsSync("../public/translations") == false) { fs.mkdirSync("../public/translations"); }
|
if (fs.existsSync("../public/translations") == false) { fs.mkdirSync("../public/translations"); }
|
||||||
translate(null, "translate.json", meshCentralSourceFiles, "translations");
|
translate(null, "translate.json", meshCentralSourceFiles, "translations");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate web pages to a given language given a language file
|
// Translate web pages to a given language given a language file
|
||||||
if (command == 'translate') {
|
if (command == 'translate') {
|
||||||
if (process.argv.length < 4) { console.log("No language specified."); process.exit(); return; }
|
if (process.argv.length < 4) { console.log("No language specified."); process.exit(); return; }
|
||||||
if (process.argv.length < 5) { console.log("No language file specified."); process.exit(); return; }
|
if (process.argv.length < 5) { console.log("No language file specified."); process.exit(); return; }
|
||||||
var lang = process.argv[3].toLowerCase();
|
var lang = process.argv[3].toLowerCase();
|
||||||
@ -106,6 +112,7 @@ if (command == 'translate') {
|
|||||||
if (sources.length == 0) { console.log("No source files specified."); process.exit(); return; }
|
if (sources.length == 0) { console.log("No source files specified."); process.exit(); return; }
|
||||||
|
|
||||||
translate(lang, langFile, sources, false);
|
translate(lang, langFile, sources, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function translate(lang, langFile, sources, createSubDir) {
|
function translate(lang, langFile, sources, createSubDir) {
|
||||||
@ -162,6 +169,7 @@ function extract(langFile, sources) {
|
|||||||
|
|
||||||
function extractFromHtml(file) {
|
function extractFromHtml(file) {
|
||||||
var data = fs.readFileSync(file);
|
var data = fs.readFileSync(file);
|
||||||
|
var { JSDOM } = jsdom;
|
||||||
const dom = new JSDOM(data, { includeNodeLocations: true });
|
const dom = new JSDOM(data, { includeNodeLocations: true });
|
||||||
console.log("Processing HTML: " + path.basename(file));
|
console.log("Processing HTML: " + path.basename(file));
|
||||||
getStrings(path.basename(file), dom.window.document.querySelector('body'));
|
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
|
// Check if the "value" attribute exists and needs to be translated
|
||||||
if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
|
if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
|
||||||
var subnodeignore = false;
|
var subnodeignore = false, subnodevalue = null, subnodeplaceholder = null, subnodetitle = null;
|
||||||
var subnodevalue = null;
|
|
||||||
for (var j in subnode.attributes) {
|
for (var j in subnode.attributes) {
|
||||||
if ((subnode.attributes[j].name == 'type') && (subnode.attributes[j].value == 'hidden')) { subnodeignore = true; }
|
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 == '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)) {
|
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 (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
|
// Check the content of the element
|
||||||
@ -228,6 +247,7 @@ function getStringFromJavaScript(name, script) {
|
|||||||
|
|
||||||
function translateFromHtml(lang, file, createSubDir) {
|
function translateFromHtml(lang, file, createSubDir) {
|
||||||
var data = fs.readFileSync(file);
|
var data = fs.readFileSync(file);
|
||||||
|
var { JSDOM } = jsdom;
|
||||||
const dom = new JSDOM(data, { includeNodeLocations: true });
|
const dom = new JSDOM(data, { includeNodeLocations: true });
|
||||||
console.log("Translating HTML: " + path.basename(file));
|
console.log("Translating HTML: " + path.basename(file));
|
||||||
translateStrings(path.basename(file), dom.window.document.querySelector('body'));
|
translateStrings(path.basename(file), dom.window.document.querySelector('body'));
|
||||||
@ -235,10 +255,10 @@ function translateFromHtml(lang, file, createSubDir) {
|
|||||||
|
|
||||||
var outname = file;
|
var outname = file;
|
||||||
if (createSubDir != null) { outname = path.join(path.dirname(file), createSubDir, path.basename(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'); }
|
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('.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 if (outname.endsWith('.htm')) { outname = (outname.substring(0, outname.length - 4) + '_' + lang + '.htm'); }
|
||||||
else { outname = (outname + '-' + lang); }
|
else { outname = (outname + '_' + lang); }
|
||||||
fs.writeFileSync(outname, out, { flag: 'w+' });
|
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
|
// Check if the "value" attribute exists and needs to be translated
|
||||||
if ((subnode.attributes != null) && (subnode.attributes.length > 0)) {
|
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) {
|
for (var j in subnode.attributes) {
|
||||||
if ((subnode.attributes[j].name == 'type') && (subnode.attributes[j].value == 'hidden')) { subnodeignore = true; }
|
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 == '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)) {
|
if ((subnodeignore == false) && (subnodevalue != null)) {
|
||||||
// Perform attribute translation
|
// Perform attribute translation for value
|
||||||
if (translationTable[subnodevalue] != null) { subnode.attributes[subnodeindex].value = translationTable[subnodevalue]; }
|
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;
|
var subname = subnode.id;
|
||||||
@ -267,10 +299,17 @@ function translateStrings(name, node) {
|
|||||||
} else {
|
} else {
|
||||||
if (subnode.nodeValue == null) continue;
|
if (subnode.nodeValue == null) continue;
|
||||||
var nodeValue = subnode.nodeValue.trim().split('\\r').join('').split('\\n').join('').trim();
|
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 ((nodeValue.length > 0) && (subnode.nodeType == 3)) {
|
||||||
if ((node.tagName != 'SCRIPT') && (node.tagName != 'STYLE') && (nodeValue.length < 8000) && (nodeValue.startsWith('{{{') == false) && (nodeValue != ' ')) {
|
if ((node.tagName != 'SCRIPT') && (node.tagName != 'STYLE') && (nodeValue.length < 8000) && (nodeValue.startsWith('{{{') == false) && (nodeValue != ' ')) {
|
||||||
// Check if we have a translation for this string
|
// 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') {
|
} else if (node.tagName == 'SCRIPT') {
|
||||||
// Translate JavaScript
|
// Translate JavaScript
|
||||||
subnode.nodeValue = translateStringsFromJavaScript(name, subnode.nodeValue);
|
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 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; }); };
|
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
Loading…
Reference in New Issue
Block a user