diff --git a/index.js b/index.js index e2b6cf9..e804338 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,11 @@ -var _lang = null; -var _filePath = null;//"../../locale.json"; -var _locale = null; - -module.exports = function(lang, filePath) { - //set the lang and file relative path - _lang = lang; - _filePath = filePath; - _locale = require(_filePath); - +class I18n { + constructor(lang, filePath) { + this._lang = lang; + this._locale = require(filePath); + } //Get the rule for pluralization //http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html - function get_rule(count, language) { + get_rule(count, language) { switch(language){ //nplurals=2; plural=(n > 1); case 'ach': @@ -243,67 +238,65 @@ module.exports = function(lang, filePath) { } } - return { - //translation function - __ : 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 - if (typeof _locale[string] != "undefined" && typeof _locale[string][_lang] != "undefined") { - translation = _locale[string][_lang]; - } + __(string, values) { + //return translation of the original sting if did not find the translation + var translation = string; + //get the corresponding translation from the file + if (typeof this._locale[string] != "undefined" && typeof this._locale[string][this._lang] != "undefined") { + translation = this._locale[string][this._lang]; + } - //If the string have place to render values withen - if ((/{{.+?}}/g).test(translation)) { - //get all the parts needed to be replaced - var matches = translation.match(/{{.+?}}/g); - //loop on each match - for (var index in matches) { - //get the match {{example}} - var match = matches[index]; - //get the word in the match example - var match_word = (match.replace('}}', '')).replace('{{', ''); + //If the string have place to render values withen + if ((/{{.+?}}/g).test(translation)) { + //get all the parts needed to be replaced + var matches = translation.match(/{{.+?}}/g); + //loop on each match + for (var index in matches) { + //get the match {{example}} + var match = matches[index]; + //get the word in the match example + var match_word = (match.replace('}}', '')).replace('{{', ''); - //translate the word if was passed in the values var - if (values && typeof values[match_word] != "undefined" ) { - translation = translation.replace(match, values[match_word]); - continue;//move to the next word in the loop - } else if (typeof _locale[match_word] != "undefined" && typeof _locale[match_word][_lang] != "undefined") { - //If the translation is there in the file then translate it directly - translation = translation.replace(match, _locale[match_word][_lang]); - continue;//move to the next word in the loop - } + //translate the word if was passed in the values var + if (values && typeof values[match_word] != "undefined" ) { + translation = translation.replace(match, values[match_word]); + continue;//move to the next word in the loop + } else if (typeof this._locale[match_word] != "undefined" && typeof this._locale[match_word][this._lang] != "undefined") { + //If the translation is there in the file then translate it directly + translation = translation.replace(match, this._locale[match_word][this._lang]); + continue;//move to the next word in the loop + } - //if the matched word have a count - if ((/\|\|.+/g).test(match_word)) { - var temp_array = match_word.split("||"); - //update the matched word - match_word = temp_array[0]; - //get the variable of the count for the word - var item_count_variable = temp_array[1]; + //if the matched word have a count + if ((/\|\|.+/g).test(match_word)) { + var temp_array = match_word.split("||"); + //update the matched word + match_word = temp_array[0]; + //get the variable of the count for the word + var item_count_variable = temp_array[1]; - //get the value form values passed to this function - //TODO through error if not found in values - var item_count = values[item_count_variable]; + //get the value form values passed to this function + //TODO through error if not found in values + var item_count = values[item_count_variable]; - //will get the rule or for pluralization based on the lang - var rule = get_rule(item_count, _lang); + //will get the rule or for pluralization based on the lang + var rule = this.get_rule(item_count, this._lang); - if (typeof _locale[match_word][_lang] == "object") { - translation = translation.replace(match, _locale[match_word][_lang][rule]); - } else { - translation = translation.replace(match, _locale[match_word][_lang]); - } + if (typeof this._locale[match_word][this._lang] == "object") { + translation = translation.replace(match, this._locale[match_word][this._lang][rule]); } else { - if (typeof values == "object") { - translation = translation.replace(match, values[match_word]); - } else { - translation = translation.replace(match, _locale[match_word][_lang]); - } + translation = translation.replace(match, this._locale[match_word][this._lang]); } - }//end of for - }//END OF IF - return translation; - }//END OF function __() - } -} \ No newline at end of file + } else { + if (typeof values == "object") { + translation = translation.replace(match, values[match_word]); + } else { + translation = translation.replace(match, this._locale[match_word][this._lang]); + } + } + }//end of for + }//END OF IF + return translation; + }//END OF function __() +} +module.exports = I18n;