1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add initial red/i18n implementation

This commit is contained in:
Nick O'Leary 2015-04-15 20:59:39 +01:00
parent 0760facb77
commit 7d41781fb4
7 changed files with 157 additions and 26 deletions

View File

@ -128,7 +128,8 @@ module.exports = function(grunt) {
"editor/vendor/jquery/js/jquery.ui.touch-punch.min.js", "editor/vendor/jquery/js/jquery.ui.touch-punch.min.js",
"editor/vendor/marked/marked.min.js", "editor/vendor/marked/marked.min.js",
"editor/vendor/orion/built-editor.min.js", "editor/vendor/orion/built-editor.min.js",
"editor/vendor/d3/d3.v3.min.js" "editor/vendor/d3/d3.v3.min.js",
"editor/vendor/i18next/i18next.min.js"
], ],
"public/vendor/vendor.css": [ "public/vendor/vendor.css": [
"editor/vendor/orion/built-editor.css" "editor/vendor/orion/built-editor.css"

5
editor/vendor/i18next/i18next.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,37 @@
{
"runtime": {
"welcome": "Welcome to Node-RED",
"version": "__component__ version: __version__",
"paths": {
"settings": "Settings file : __path__"
}
},
"nodes": {
"loading": "Loading palette nodes",
"errors": "Failed to register __count__ node type",
"errors_plural": "Failed to register __count__ node types",
"errors-help": "Run with -v for details",
"missing-modules": "Missing node modules:",
"removing-modules": "Removing modules from config",
"added-types": "Added node types:",
"removed-types": "Removed node types:",
"install": {
"invalid": "Invalid module name",
"installing": "Installing module: __name__",
"installed": "Installed module: __name__",
"install-failed": "Install failed",
"install-failed-long": "Installation of module __name__ failed:",
"install-failed-not-found": "$t(install-failed-long) module not found",
"uninstalling": "Uninstalling module: __name__",
"uninstall-failed": "Uninstall failed",
"uninstall-failed-long": "Uninstall of module __name__ failed:",
"uninstalled": "Uninstalled module: __name__"
}
}
}

View File

@ -49,6 +49,7 @@
"passport-http-bearer":"1.0.1", "passport-http-bearer":"1.0.1",
"passport-oauth2-client-password":"0.1.2", "passport-oauth2-client-password":"0.1.2",
"oauth2orize":"1.0.1", "oauth2orize":"1.0.1",
"i18next":"1.7.10",
"node-red-node-feedparser":"0.0.*", "node-red-node-feedparser":"0.0.*",
"node-red-node-email":"0.0.*", "node-red-node-email":"0.0.*",
"node-red-node-twitter":"0.0.*" "node-red-node-twitter":"0.0.*"

85
red/i18n.js Normal file
View File

@ -0,0 +1,85 @@
/**
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var i18n = require("i18next");
var when = require("when");
var path = require("path");
var fs = require("fs");
var resourceMap = {
"messages": path.resolve(__dirname+"/../locales")
}
function registerMessageCatalog(namespace,dir) {
return when.promise(function(resolve,reject) {
resourceMap[namespace] = dir;
i18n.loadNamespace(namespace,function() {
resolve();
});
});
}
var MessageFileLoader = {
fetchOne: function(lng, ns, callback) {
if (resourceMap[ns]) {
var file = path.join(resourceMap[ns],lng,"messages.json");
fs.readFile(file,"utf8",function(err,content) {
if (err) {
callback(err);
} else {
try {
callback(null, JSON.parse(content.replace(/^\uFEFF/, '')));
} catch(e) {
callback(e);
}
}
});
} else {
callback(new Error("Unrecognised namespace"));
}
}
}
function init() {
return when.promise(function(resolve,reject) {
i18n.backend(MessageFileLoader);
i18n.init({
ns: {
namespaces: ["messages"],
defaultNs: "messages"
},
fallbackLng: ['en-US']
},function() {
resolve();
});
});
}
var obj = module.exports = {
init: init,
registerMessageCatalog: registerMessageCatalog
}
obj['_'] = function() {
//var opts = {};
//if (def) {
// opts.defaultValue = def;
//}
return i18n.t.apply(null,arguments);
}

View File

@ -24,6 +24,7 @@ var redNodes = require("./nodes");
var comms = require("./comms"); var comms = require("./comms");
var storage = require("./storage"); var storage = require("./storage");
var log = require("./log"); var log = require("./log");
var i18n = require("./i18n");
var app = null; var app = null;
var nodeApp = null; var nodeApp = null;
@ -44,7 +45,8 @@ function init(_server,_settings) {
} }
function start() { function start() {
return storage.init(settings) return i18n.init()
.then(function() { return storage.init(settings)})
.then(function() { return settings.load(storage)}) .then(function() { return settings.load(storage)})
.then(function() { .then(function() {
if (settings.httpAdminRoot !== false) { if (settings.httpAdminRoot !== false) {
@ -56,12 +58,12 @@ function start() {
reportMetrics(); reportMetrics();
}, settings.runtimeMetricInterval||15000); }, settings.runtimeMetricInterval||15000);
} }
console.log("\n\nWelcome to Node-RED\n===================\n"); console.log("\n\n"+i18n._("runtime.welcome")+"\n===================\n");
if (settings.version) { if (settings.version) {
log.info("Node-RED version: v"+settings.version); log.info(i18n._("runtime.version",{component:"Node-RED",version:"v"+settings.version}));
} }
log.info("Node.js version: "+process.version); log.info(i18n._("runtime.version",{component:"Node.js ",version:process.version}));
log.info("Loading palette nodes"); log.info(i18n._("nodes.loading"));
redNodes.init(settings,storage,app); redNodes.init(settings,storage,app);
return redNodes.load().then(function() { return redNodes.load().then(function() {
@ -75,13 +77,13 @@ function start() {
log.warn("["+nodeErrors[i].name+"] "+nodeErrors[i].err); log.warn("["+nodeErrors[i].name+"] "+nodeErrors[i].err);
} }
} else { } else {
log.warn("Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s")); log.warn(i18n._("nodes.errors",{count:nodeErrors.length}));
log.warn("Run with -v for details"); log.warn(i18n._("nodes.errors-help"));
} }
log.warn("------------------------------------------"); log.warn("------------------------------------------");
} }
if (nodeMissing.length > 0) { if (nodeMissing.length > 0) {
log.warn("Missing node modules:"); log.warn(i18n._("nodes.missing-modules"));
var missingModules = {}; var missingModules = {};
for (i=0;i<nodeMissing.length;i++) { for (i=0;i<nodeMissing.length;i++) {
var missing = nodeMissing[i]; var missing = nodeMissing[i];
@ -100,11 +102,11 @@ function start() {
} }
} }
if (!settings.autoInstallModules) { if (!settings.autoInstallModules) {
log.info("Removing modules from config"); log.info(i18n._("nodes.removing-modules"));
redNodes.cleanModuleList(); redNodes.cleanModuleList();
} }
} }
log.info("Settings file : "+settings.settingsFile); log.info(i18n._("runtime.paths.settings",{path:settings.settingsFile}));
redNodes.loadFlows(); redNodes.loadFlows();
comms.start(); comms.start();
}).otherwise(function(err) { }).otherwise(function(err) {
@ -117,7 +119,7 @@ function start() {
function reportAddedModules(info) { function reportAddedModules(info) {
comms.publish("node/added",info.nodes,false); comms.publish("node/added",info.nodes,false);
if (info.nodes.length > 0) { if (info.nodes.length > 0) {
log.info("Added node types:"); log.info(i18n._("nodes.added-types"));
for (var i=0;i<info.nodes.length;i++) { for (var i=0;i<info.nodes.length;i++) {
for (var j=0;j<info.nodes[i].types.length;j++) { for (var j=0;j<info.nodes[i].types.length;j++) {
log.info(" - "+ log.info(" - "+
@ -133,7 +135,7 @@ function reportAddedModules(info) {
function reportRemovedModules(removedNodes) { function reportRemovedModules(removedNodes) {
comms.publish("node/removed",removedNodes,false); comms.publish("node/removed",removedNodes,false);
log.info("Removed node types:"); log.info(i18n._("nodes.removed-types"));
for (var j=0;j<removedNodes.length;j++) { for (var j=0;j<removedNodes.length;j++) {
for (var i=0;i<removedNodes[j].types.length;i++) { for (var i=0;i<removedNodes[j].types.length;i++) {
log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]); log.info(" - "+(removedNodes[j].module?removedNodes[j].module+":":"")+removedNodes[j].types[i]);
@ -159,18 +161,18 @@ function installModule(module) {
//TODO: ensure module is 'safe' //TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) { if (/[\s;]/.test(module)) {
reject(new Error("Invalid module name")); reject(new Error(i18n._("nodes.install.invalid")));
return; return;
} }
if (redNodes.getModuleInfo(module)) { if (redNodes.getModuleInfo(module)) {
// TODO: nls
var err = new Error("Module already loaded"); var err = new Error("Module already loaded");
err.code = "module_already_loaded"; err.code = "module_already_loaded";
reject(err); reject(err);
return; return;
} }
log.info(i18n._("nodes.install.installing",{name: module}));
log.info("Installing module: "+module);
var installDir = settings.userDir || process.env.NODE_RED_HOME || "."; var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var child = child_process.exec('npm install --production '+module, var child = child_process.exec('npm install --production '+module,
{ {
@ -180,19 +182,19 @@ function installModule(module) {
if (err) { if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m"); var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
if (lookFor404.test(stdout)) { if (lookFor404.test(stdout)) {
log.warn("Installation of module "+module+" failed: module not found"); log.warn(i18n._("nodes.install.install-failed-not-found",{name:module}));
var e = new Error(); var e = new Error();
e.code = 404; e.code = 404;
reject(e); reject(e);
} else { } else {
log.warn("Installation of module "+module+" failed:"); log.warn(i18n._("nodes.install.install-failed-long",{name:module}));
log.warn("------------------------------------------"); log.warn("------------------------------------------");
log.warn(err.toString()); log.warn(err.toString());
log.warn("------------------------------------------"); log.warn("------------------------------------------");
reject(new Error("Install failed")); reject(new Error(i18n._("nodes.install.install-failed")));
} }
} else { } else {
log.info("Installed module: "+module); log.info(i18n._("nodes.install.installed",{name:module}));
resolve(redNodes.addModule(module).then(reportAddedModules)); resolve(redNodes.addModule(module).then(reportAddedModules));
} }
} }
@ -203,30 +205,30 @@ function installModule(module) {
function uninstallModule(module) { function uninstallModule(module) {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) { if (/[\s;]/.test(module)) {
reject(new Error("Invalid module name")); reject(new Error(i18n._("nodes.install.invalid")));
return; return;
} }
var installDir = settings.userDir || process.env.NODE_RED_HOME || "."; var installDir = settings.userDir || process.env.NODE_RED_HOME || ".";
var moduleDir = path.join(installDir,"node_modules",module); var moduleDir = path.join(installDir,"node_modules",module);
if (!fs.existsSync(moduleDir)) { if (!fs.existsSync(moduleDir)) {
return reject(new Error("Unabled to uninstall "+module+".")); return reject(new Error(i18n._("nodes.install.uninstall-failed",{name:module})));
} }
var list = redNodes.removeModule(module); var list = redNodes.removeModule(module);
log.info("Removing module: "+module); log.info(i18n._("nodes.install.uninstalling",{name:module}));
var child = child_process.exec('npm remove '+module, var child = child_process.exec('npm remove '+module,
{ {
cwd: installDir cwd: installDir
}, },
function(err, stdin, stdout) { function(err, stdin, stdout) {
if (err) { if (err) {
log.warn("Removal of module "+module+" failed:"); log.warn(i18n._("nodes.install.uninstall-failed-long",{name:module}));
log.warn("------------------------------------------"); log.warn("------------------------------------------");
log.warn(err.toString()); log.warn(err.toString());
log.warn("------------------------------------------"); log.warn("------------------------------------------");
reject(new Error("Removal failed")); reject(new Error(i18n._("nodes.install.uninstall-failed",{name:module})));
} else { } else {
log.info("Removed module: "+module); log.info(i18n._("nodes.install.uninstalled",{name:module}));
reportRemovedModules(list); reportRemovedModules(list);
resolve(list); resolve(list);
} }

0
test/red/i18n_spec.js Normal file
View File