From 2252d5d7074a1963be18fa0ddb261298e400a9d6 Mon Sep 17 00:00:00 2001 From: elliotjd1 Date: Mon, 24 Apr 2017 10:48:32 +0200 Subject: [PATCH] Implement option for fallback language If a translation in the current language is not available, a translation from the specified fallback language will be used (if available). --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e2b6cf9..25ab9dd 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,15 @@ var _lang = null; var _filePath = null;//"../../locale.json"; var _locale = null; +var _fallbackLang = null; -module.exports = function(lang, filePath) { +module.exports = function(lang, filePath, fallbackLang) { //set the lang and file relative path _lang = lang; _filePath = filePath; _locale = require(_filePath); + _fallbackLang = fallbackLang; + //Get the rule for pluralization //http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html @@ -248,9 +251,11 @@ module.exports = function(lang, filePath) { __ : function(string, values) { //return translation of the original sting if did not find the translation var translation = string; - //get the corresponding translation from the file + //get the corresponding translation from the file, or use the fallback language if translation is not available if (typeof _locale[string] != "undefined" && typeof _locale[string][_lang] != "undefined") { translation = _locale[string][_lang]; + } else if (typeof _locale[string] != "undefined" && typeof _locale[string][_lang] == "undefined" && typeof _locale[string][_fallbackLang] != "undefined") { + translation = _locale[string][_fallbackLang]; } //If the string have place to render values withen @@ -306,4 +311,4 @@ module.exports = function(lang, filePath) { return translation; }//END OF function __() } -} \ No newline at end of file +}