Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement option for fallback language #1

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 __()
}
}
}