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

Move locale files under api/runtime components

This commit is contained in:
Nick O'Leary 2015-11-21 23:12:39 +00:00
parent 88dc202db2
commit d2be7f8c8f
9 changed files with 63 additions and 66 deletions

View File

@ -174,8 +174,8 @@ module.exports = function(grunt) {
messages: {
src: [
'nodes/core/locales/en-US/messages.json',
'locales/en-US/editor.json',
'locales/en-US/runtime.json'
'red/api/locales/en-US/editor.json',
'red/runtime/locales/en-US/runtime.json'
]
}
},
@ -223,8 +223,8 @@ module.exports = function(grunt) {
json: {
files: [
'nodes/core/locales/en-US/messages.json',
'locales/en-US/editor.json',
'locales/en-US/runtime.json'
'red/api/locales/en-US/editor.json',
'red/runtime/locales/en-US/runtime.json'
],
tasks: ['jsonlint:messages']
}
@ -238,7 +238,7 @@ module.exports = function(grunt) {
args: nodemonArgs,
ext: 'js,html,json',
watch: [
'red','nodes','locales'
'red','nodes'
]
}
}
@ -302,8 +302,7 @@ module.exports = function(grunt) {
'red/**',
'public/**',
'editor/templates/**',
'bin/**',
'locales/**'
'bin/**'
],
dest: path.resolve('<%= paths.dist %>/node-red-<%= pkg.version %>')
}]

View File

@ -34,6 +34,7 @@ var comms = require("./comms");
var auth = require("./auth");
var needsPermission = auth.needsPermission;
var i18n;
var log;
var adminApp;
var nodeApp;
@ -50,6 +51,7 @@ var errorHandler = function(err,req,res,next) {
function init(server,runtime) {
var settings = runtime.settings;
i18n = runtime.i18n;
log = runtime.log;
if (settings.httpNodeRoot !== false) {
nodeApp = express();
@ -127,8 +129,9 @@ function init(server,runtime) {
}
}
function start() {
comms.start();
return when.resolve();
return i18n.registerMessageCatalog("editor",path.resolve(path.join(__dirname,"locales")),"editor.json").then(function(){
comms.start();
});
}
function stop() {
comms.stop();

View File

@ -13,20 +13,52 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var fs = require('fs');
var path = require('path');
var i18n;
var supportedLangs = [];
var apiLocalDir = path.resolve(path.join(__dirname,"locales"));
var initSupportedLangs = function() {
fs.readdir(apiLocalDir, function(err,files) {
if(!err) {
supportedLangs = files;
}
});
}
function determineLangFromHeaders(acceptedLanguages){
var lang = i18n.defaultLang;
acceptedLanguages = acceptedLanguages || [];
for (var i=0;i<acceptedLanguages.length;i++){
if (supportedLangs.indexOf(acceptedLanguages[i]) !== -1){
lang = acceptedLanguages[i];
break;
// check the language without the country code
} else if (supportedLangs.indexOf(acceptedLanguages[i].split("-")[0]) !== -1) {
lang = acceptedLanguages[i].split("-")[0];
break;
}
}
return lang;
}
module.exports = {
init: function(runtime) {
i18n = runtime.i18n;
initSupportedLangs();
},
get: function(req,res) {
var namespace = req.params[0];
namespace = namespace.replace(/\.json$/,"");
var lang = i18n.determineLangFromHeaders(req.acceptsLanguages() || []);
var lang = determineLangFromHeaders(req.acceptsLanguages() || []);
var prevLang = i18n.i.lng();
i18n.i.setLng(lang, function(){
var catalog = i18n.catalog(namespace,lang);
res.json(catalog||{});
});
i18n.i.setLng(prevLang);
}
},
determineLangFromHeaders: determineLangFromHeaders
}

View File

@ -16,6 +16,7 @@
var when = require("when");
var comms = require("./comms");
var locales = require("./locales");
var redNodes;
var log;
var i18n;
@ -33,7 +34,7 @@ module.exports = {
log.audit({event: "nodes.list.get"},req);
res.json(redNodes.getNodeList());
} else {
var lang = i18n.determineLangFromHeaders(req.acceptsLanguages());
var lang = locales.determineLangFromHeaders(req.acceptsLanguages());
log.audit({event: "nodes.configs.get"},req);
res.send(redNodes.getNodeConfigs(lang));
}
@ -126,7 +127,7 @@ module.exports = {
res.status(404).end();
}
} else {
var lang = i18n.determineLangFromHeaders(req.acceptsLanguages());
var lang = locales.determineLangFromHeaders(req.acceptsLanguages());
result = redNodes.getNodeConfig(id,lang);
if (result) {
log.audit({event: "nodes.config.get",id:id},req);

View File

@ -20,19 +20,9 @@ var path = require("path");
var fs = require("fs");
var defaultLang = "en-US";
var supportedLangs = [];
var resourceMap = {
"runtime": {
basedir: path.resolve(__dirname+"/../../locales"),
file:"runtime.json"
},
"editor": {
basedir: path.resolve(__dirname+"/../../locales"),
file: "editor.json"
}
}
var resourceCache = {}
var resourceMap = {};
var resourceCache = {};
function registerMessageCatalog(namespace,dir,file) {
return when.promise(function(resolve,reject) {
@ -43,19 +33,6 @@ function registerMessageCatalog(namespace,dir,file) {
});
}
var initSupportedLangs = function() {
return when.promise(function(resolve,reject) {
fs.readdir(resourceMap.editor.basedir, function(err,files) {
if(err) {
reject(err);
} else {
supportedLangs = files;
resolve();
}
});
});
}
function mergeCatalog(fallback,catalog) {
for (var k in fallback) {
if (fallback.hasOwnProperty(k)) {
@ -102,21 +79,17 @@ function init() {
i18n.backend(MessageFileLoader);
i18n.init({
ns: {
namespaces: ["runtime","editor"],
namespaces: [],
defaultNs: "runtime"
},
fallbackLng: ['en-US']
fallbackLng: [defaultLang]
},function() {
initSupportedLangs().then(function() {
resolve();
});
resolve();
});
});
}
function getCatalog(namespace,lang) {
//console.log("+",namespace,lang);
//console.log(resourceCache[namespace][lang]);
var result = null;
if (resourceCache.hasOwnProperty(namespace)) {
result = resourceCache[namespace][lang];
@ -130,32 +103,15 @@ function getCatalog(namespace,lang) {
}
}
}
//console.log(result);
return result;
}
function determineLangFromHeaders(acceptedLanguages){
var lang = "en-US";
acceptedLanguages = acceptedLanguages || [];
for (var i=0;i<acceptedLanguages.length;i++){
if (supportedLangs.indexOf(acceptedLanguages[i]) !== -1){
lang = acceptedLanguages[i];
break;
// check the language without the country code
} else if (supportedLangs.indexOf(acceptedLanguages[i].split("-")[0]) !== -1) {
lang = acceptedLanguages[i].split("-")[0];
break;
}
}
return lang;
}
var obj = module.exports = {
init: init,
registerMessageCatalog: registerMessageCatalog,
catalog: getCatalog,
i: i18n,
determineLangFromHeaders: determineLangFromHeaders
defaultLang:defaultLang
}
obj['_'] = function() {

View File

@ -51,6 +51,9 @@ function getVersion() {
function start() {
return i18n.init()
.then(function() {
return i18n.registerMessageCatalog("runtime",path.resolve(path.join(__dirname,"locales")),"runtime.json")
})
.then(function() { return storage.init(settings)})
.then(function() { return settings.load(storage)})
.then(function() {

View File

@ -22,9 +22,8 @@ var sinon = require('sinon');
var when = require('when');
var nodes = require("../../../red/api/nodes");
var comms = require("../../../red/api/comms");
var locales = require("../../../red/api/locales");
describe("nodes api", function() {
@ -37,6 +36,7 @@ describe("nodes api", function() {
warn: function(){}
}
nodes.init(runtime);
}
before(function() {
@ -50,8 +50,11 @@ describe("nodes api", function() {
app.put("/nodes/:mod/:set",nodes.putSet);
app.delete("/nodes/:id",nodes.delete);
sinon.stub(comms,"publish");
sinon.stub(locales,"determineLangFromHeaders", function() {
return "en-US";
});
});
after(function() {
comms.publish.restore();
});