Skip to content

Commit

Permalink
Merge pull request #1 from Symphony9/fallback-language
Browse files Browse the repository at this point in the history
Implement option for fallback language
  • Loading branch information
elliotjd1 authored Apr 24, 2017
2 parents 6df7f66 + 2252d5d commit 88045bf
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -306,4 +311,4 @@ module.exports = function(lang, filePath) {
return translation;
}//END OF function __()
}
}
}

0 comments on commit 88045bf

Please sign in to comment.