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

Allow a language catalog to be a partial catalog

This commit is contained in:
Nick O'Leary 2015-09-24 21:56:45 +01:00
parent 9560dc9408
commit 3963fa9738
2 changed files with 23 additions and 8 deletions

View File

@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ **/
RED.i18n = (function() { RED.i18n = (function() {
return { return {
init: function(done) { init: function(done) {
i18n.init({ i18n.init({
@ -26,7 +26,8 @@ RED.i18n = (function() {
namespaces: ["editor","node-red"], namespaces: ["editor","node-red"],
defaultNs: "editor" defaultNs: "editor"
}, },
fallbackLng: ['en-US'] fallbackLng: ['en-US'],
useCookie: false
},function() { },function() {
done(); done();
}); });
@ -40,6 +41,3 @@ RED.i18n = (function() {
} }
} }
})(); })();

View File

@ -56,6 +56,18 @@ var initSupportedLangs = function() {
}); });
} }
function mergeCatalog(fallback,catalog) {
for (var k in fallback) {
if (fallback.hasOwnProperty(k)) {
if (!catalog[k]) {
catalog[k] = fallback[k];
} else if (typeof fallback[k] === 'object') {
mergeCatalog(fallback[k],catalog[k]);
}
}
}
}
var MessageFileLoader = { var MessageFileLoader = {
fetchOne: function(lng, ns, callback) { fetchOne: function(lng, ns, callback) {
if (resourceMap[ns]) { if (resourceMap[ns]) {
@ -66,10 +78,12 @@ var MessageFileLoader = {
callback(err); callback(err);
} else { } else {
try { try {
//console.log(">>",ns,file,lng);
resourceCache[ns] = resourceCache[ns]||{}; resourceCache[ns] = resourceCache[ns]||{};
resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, '')); resourceCache[ns][lng] = JSON.parse(content.replace(/^\uFEFF/, ''));
//console.log(resourceCache[ns][lng]); //console.log(resourceCache[ns][lng]);
if (lng !== defaultLang) {
mergeCatalog(resourceCache[ns][defaultLang],resourceCache[ns][lng]);
}
callback(null, resourceCache[ns][lng]); callback(null, resourceCache[ns][lng]);
} catch(e) { } catch(e) {
callback(e); callback(e);
@ -109,7 +123,10 @@ function getCatalog(namespace,lang) {
if (!result) { if (!result) {
var langParts = lang.split("-"); var langParts = lang.split("-");
if (langParts.length == 2) { if (langParts.length == 2) {
result = getCatalog(namespace,langParts[0]); result = resourceCache[namespace][langParts[0]];
}
if (!result) {
return resourceCache[namespace][defaultLang];
} }
} }
} }