2015-04-15 21:59:39 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
2015-07-19 23:10:46 +02:00
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
var i18n = require("i18next");
|
|
|
|
var when = require("when");
|
|
|
|
var path = require("path");
|
|
|
|
var fs = require("fs");
|
|
|
|
|
2015-04-26 00:29:53 +02:00
|
|
|
var defaultLang = "en-US";
|
2015-06-01 19:59:25 +02:00
|
|
|
var supportedLangs = null;
|
2015-04-26 00:29:53 +02:00
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
var resourceMap = {
|
2015-04-27 00:02:14 +02:00
|
|
|
"runtime": {
|
2015-04-26 00:29:53 +02:00
|
|
|
basedir: path.resolve(__dirname+"/../locales"),
|
2015-04-27 00:02:14 +02:00
|
|
|
file:"runtime.json"
|
|
|
|
},
|
|
|
|
"editor": {
|
|
|
|
basedir: path.resolve(__dirname+"/../locales"),
|
|
|
|
file: "editor.json"
|
2015-04-26 00:29:53 +02:00
|
|
|
}
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
2015-04-26 00:29:53 +02:00
|
|
|
var resourceCache = {}
|
2015-04-15 21:59:39 +02:00
|
|
|
|
2015-04-26 00:29:53 +02:00
|
|
|
function registerMessageCatalog(namespace,dir,file) {
|
2015-04-15 21:59:39 +02:00
|
|
|
return when.promise(function(resolve,reject) {
|
2015-04-26 00:29:53 +02:00
|
|
|
resourceMap[namespace] = { basedir:dir, file:file};
|
2015-04-15 21:59:39 +02:00
|
|
|
i18n.loadNamespace(namespace,function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-01 19:59:25 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
var MessageFileLoader = {
|
|
|
|
fetchOne: function(lng, ns, callback) {
|
|
|
|
if (resourceMap[ns]) {
|
2015-04-26 00:29:53 +02:00
|
|
|
var file = path.join(resourceMap[ns].basedir,lng,resourceMap[ns].file);
|
2015-05-06 23:14:00 +02:00
|
|
|
//console.log(file);
|
2015-04-15 21:59:39 +02:00
|
|
|
fs.readFile(file,"utf8",function(err,content) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
try {
|
2015-05-06 23:14:00 +02:00
|
|
|
//console.log(">>",ns,file,lng);
|
2015-04-26 00:29:53 +02:00
|
|
|
resourceCache[ns] = resourceCache[ns]||{};
|
|
|
|
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
|
2015-05-06 23:14:00 +02:00
|
|
|
//console.log(resourceCache[ns][lng]);
|
2015-04-26 00:29:53 +02:00
|
|
|
callback(null, resourceCache[ns][lng]);
|
2015-04-15 21:59:39 +02:00
|
|
|
} catch(e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(new Error("Unrecognised namespace"));
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 23:10:46 +02:00
|
|
|
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
i18n.backend(MessageFileLoader);
|
|
|
|
i18n.init({
|
|
|
|
ns: {
|
2015-04-27 00:02:14 +02:00
|
|
|
namespaces: ["runtime","editor"],
|
|
|
|
defaultNs: "runtime"
|
2015-04-15 21:59:39 +02:00
|
|
|
},
|
|
|
|
fallbackLng: ['en-US']
|
|
|
|
},function() {
|
2015-06-01 19:59:25 +02:00
|
|
|
initSupportedLangs().then(function() {
|
|
|
|
resolve();
|
|
|
|
});
|
2015-04-15 21:59:39 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-26 00:29:53 +02:00
|
|
|
function getCatalog(namespace,lang) {
|
2015-05-06 23:14:00 +02:00
|
|
|
//console.log("+",namespace,lang);
|
|
|
|
//console.log(resourceCache[namespace][lang]);
|
2015-04-26 00:29:53 +02:00
|
|
|
var result = null;
|
|
|
|
if (resourceCache.hasOwnProperty(namespace)) {
|
|
|
|
result = resourceCache[namespace][lang];
|
|
|
|
if (!result) {
|
|
|
|
var langParts = lang.split("-");
|
|
|
|
if (langParts.length == 2) {
|
|
|
|
result = getCatalog(namespace,langParts[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 23:14:00 +02:00
|
|
|
//console.log(result);
|
2015-04-26 00:29:53 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-28 21:17:31 +02:00
|
|
|
function determineLangFromHeaders(acceptedLanguages){
|
|
|
|
var lang = "en-US";
|
2015-07-19 23:10:46 +02:00
|
|
|
acceptedLanguages = acceptedLanguages || [];
|
2015-05-28 21:17:31 +02:00
|
|
|
for (var i=0;i<acceptedLanguages.length;i++){
|
2015-06-01 19:59:25 +02:00
|
|
|
if (supportedLangs.indexOf(acceptedLanguages[i]) !== -1){
|
2015-05-28 21:17:31 +02:00
|
|
|
lang = acceptedLanguages[i];
|
|
|
|
break;
|
|
|
|
// check the language without the country code
|
2015-06-05 22:18:01 +02:00
|
|
|
} else if (supportedLangs.indexOf(acceptedLanguages[i].split("-")[0]) !== -1) {
|
2015-05-28 21:17:31 +02:00
|
|
|
lang = acceptedLanguages[i].split("-")[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lang;
|
|
|
|
}
|
2015-04-15 21:59:39 +02:00
|
|
|
|
|
|
|
var obj = module.exports = {
|
|
|
|
init: init,
|
2015-04-26 00:29:53 +02:00
|
|
|
registerMessageCatalog: registerMessageCatalog,
|
|
|
|
catalog: getCatalog,
|
2015-05-28 21:17:31 +02:00
|
|
|
i: i18n,
|
|
|
|
determineLangFromHeaders: determineLangFromHeaders
|
2015-04-15 21:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
obj['_'] = function() {
|
|
|
|
//var opts = {};
|
|
|
|
//if (def) {
|
|
|
|
// opts.defaultValue = def;
|
|
|
|
//}
|
2015-04-26 00:29:53 +02:00
|
|
|
//console.log(arguments);
|
2015-04-15 21:59:39 +02:00
|
|
|
return i18n.t.apply(null,arguments);
|
|
|
|
}
|