Update lang component.

This commit is contained in:
Pierre 2018-02-04 09:25:04 +01:00
parent eb4723d9fc
commit cfd671f7e8
3 changed files with 85 additions and 69 deletions

View File

@ -76,16 +76,13 @@ var ComunicWeb = {
*/ */
getCurrentLanguage: function(){}, getCurrentLanguage: function(){},
/**
* Include and install specified language
*/
installLanguage: function(languageID){},
/** /**
* Initializate languages * Initializate languages
*/ */
initLanguages: function(){}, initLanguages: function(){},
//TODO : implement
/** /**
* Return a string in correct language * Return a string in correct language
*/ */

View File

@ -4,75 +4,93 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
/** ComunicWeb.common.langs = {
* Get current language
*
* @return {String} The id of the current language
*/
ComunicWeb.common.langs.getCurrentLanguage = function(){
return "fr";
//return ComunicWeb.__config.defaultLanguage;
};
/** /**
* Include and install specified language * Local storage lang item name
* */
* @param {String} languageID The languageID to install _lang_storage_field_name: "comunic_v2_lang",
*/
ComunicWeb.common.langs.installLanguage = function(languageID){
//Generate filename to include
var fileToInclude = ComunicWeb.__config.languagesPath + languageID + ".inc.js";
//Include filename /**
ComunicWeb.common.jsFiles.includeFile(fileToInclude); * Currently selected language
}; */
__currentLang: "en",
/** /**
* Language initiator * Default langage (updated on init)
* */
* @return Boolean False if it fails __defaultLang: "en",
*/
ComunicWeb.common.langs.initLanguages = function(){
//Debug message
ComunicWeb.debug.logMessage("Get and install languages...");
//Get languages to install /**
this.__currentLang = this.getCurrentLanguage(); * Get current language
this.__defaultLang = ComunicWeb.__config.defaultLanguage; *
* @return {String} The id of the current language
//Install default language (made by default) */
//this.installLanguage(this.__defaultLang); getCurrentLanguage: function(){
//Check if a language has been set in local storage
if(localStorage.getItem(this._lang_storage_field_name) != null)
return localStorage.getItem(this._lang_storage_field_name);
//If selected language is different than default one, install it too //Else return default language
if(this.__currentLang !== this.__defaultLang) return ComunicWeb.__config.defaultLanguage;
this.installLanguage(this.__currentLang); },
//Everything is OK
return 0;
}
/** /**
* Return a string in correct language * Set language
* *
* @param {String} stringName The name of the string to show * @param {String} lang The language to set
* @param {Array} stringParams The optionnal parametres to include with the string */
* @return {String} The string ready to show setLang: function(lang){
*/
ComunicWeb.common.langs.getTranslatedText = function(stringName, stringParams){ //Set new language in local storage
//Try to get string localStorage.setItem(this._lang_storage_field_name, lang);
if(this[this.__currentLang][stringName])
var string = this[this.__currentLang][stringName]; //Save name
else if(this[this.__defaultLang][stringName]) this.__currentLang = lang;
var string = this[this.__defaultLang][stringName]; },
else
var string = "No Translated String"; /**
* Language initiator
//Change string with parametres if required *
if(stringParams){ * @return {Boolean} False if it fails
for(i in stringParams){ */
string = string.replace("%p", stringParams[i]); initLanguages: function(){
//Debug message
ComunicWeb.debug.logMessage("Get and install languages...");
//Get languages to install
this.__currentLang = this.getCurrentLanguage();
this.__defaultLang = ComunicWeb.__config.defaultLanguage;
//Everything is OK
return 0;
},
/**
* Return a string in correct language
*
* @param {String} stringName The name of the string to show
* @param {Array} stringParams The optionnal parametres to include with the string
* @return {String} The string ready to show
*/
getTranslatedText: function(stringName, stringParams){
//Try to get string
if(this[this.__currentLang][stringName])
var string = this[this.__currentLang][stringName];
else if(this[this.__defaultLang][stringName])
var string = this[this.__defaultLang][stringName];
else
var string = "No Translated String";
//Change string with parametres if required
if(stringParams){
for(i in stringParams){
string = string.replace("%p", stringParams[i]);
}
} }
}
return string; return string;
},
} }

View File

@ -189,8 +189,9 @@ class Dev {
"js/common/date.js", "js/common/date.js",
"js/common/system.js", "js/common/system.js",
//Default langage //Languages
"js/langs/en.inc.js", "js/langs/en.inc.js",
"js/langs/fr.inc.js",
//Components //Components
//Mail caching //Mail caching