ComunicWeb/assets/js/common/langs.js

103 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
* Lang functions
*
* @author Pierre HUBERT
*/
2018-02-04 08:25:04 +00:00
ComunicWeb.common.langs = {
2018-02-04 08:25:04 +00:00
/**
* Local storage lang item name
*/
_lang_storage_field_name: "comunic_v2_lang",
2018-02-04 08:25:04 +00:00
/**
* Currently selected language
*/
__currentLang: "en",
2018-02-04 08:25:04 +00:00
/**
* Default langage (updated on init)
*/
__defaultLang: "en",
2018-02-04 08:25:04 +00:00
/**
* Get current language
*
* @return {String} The id of the current language
*/
current: function(){
2018-02-04 08:25:04 +00:00
//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);
//Get user language
var language = navigator.language || navigator.userLanguage;
if(language){
return language.split("-")[0];
}
2018-02-04 08:25:04 +00:00
//Else return default language
return ComunicWeb.__config.defaultLanguage;
},
2018-02-04 08:25:04 +00:00
/**
* Set language
*
* @param {String} lang The language to set
*/
setLang: function(lang){
//Set new language in local storage
localStorage.setItem(this._lang_storage_field_name, lang);
//Save name
this.__currentLang = lang;
},
/**
* Language initiator
*
* @return {Boolean} False if it fails
*/
initLanguages: function(){
//Debug message
ComunicWeb.debug.logMessage("Get and install languages...");
//Get languages to install
this.__currentLang = this.current();
2018-02-04 08:25:04 +00:00
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]);
}
}
2018-02-04 08:25:04 +00:00
return string;
},
}