From cfd671f7e89eca753b97fc5a85e75bea9e64ccc2 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 4 Feb 2018 09:25:04 +0100 Subject: [PATCH] Update lang component. --- assets/js/common/functionsSchema.js | 7 +- assets/js/common/langs.js | 144 ++++++++++++++++------------ system/config/dev.config.php | 3 +- 3 files changed, 85 insertions(+), 69 deletions(-) diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 4ed604b8..dd657ddf 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -76,16 +76,13 @@ var ComunicWeb = { */ getCurrentLanguage: function(){}, - /** - * Include and install specified language - */ - installLanguage: function(languageID){}, - /** * Initializate languages */ initLanguages: function(){}, + //TODO : implement + /** * Return a string in correct language */ diff --git a/assets/js/common/langs.js b/assets/js/common/langs.js index 7e475bde..8d7f67e7 100644 --- a/assets/js/common/langs.js +++ b/assets/js/common/langs.js @@ -4,75 +4,93 @@ * @author Pierre HUBERT */ -/** - * Get current language - * - * @return {String} The id of the current language - */ -ComunicWeb.common.langs.getCurrentLanguage = function(){ - return "fr"; - //return ComunicWeb.__config.defaultLanguage; -}; +ComunicWeb.common.langs = { -/** - * Include and install specified language - * - * @param {String} languageID The languageID to install - */ -ComunicWeb.common.langs.installLanguage = function(languageID){ - //Generate filename to include - var fileToInclude = ComunicWeb.__config.languagesPath + languageID + ".inc.js"; + /** + * Local storage lang item name + */ + _lang_storage_field_name: "comunic_v2_lang", - //Include filename - ComunicWeb.common.jsFiles.includeFile(fileToInclude); -}; + /** + * Currently selected language + */ + __currentLang: "en", -/** - * Language initiator - * - * @return Boolean False if it fails - */ -ComunicWeb.common.langs.initLanguages = function(){ - //Debug message - ComunicWeb.debug.logMessage("Get and install languages..."); + /** + * Default langage (updated on init) + */ + __defaultLang: "en", - //Get languages to install - this.__currentLang = this.getCurrentLanguage(); - this.__defaultLang = ComunicWeb.__config.defaultLanguage; - - //Install default language (made by default) - //this.installLanguage(this.__defaultLang); + /** + * Get current language + * + * @return {String} The id of the current language + */ + 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 - if(this.__currentLang !== this.__defaultLang) - this.installLanguage(this.__currentLang); - - //Everything is OK - return 0; -} + //Else return default language + return ComunicWeb.__config.defaultLanguage; + }, -/** - * 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 - */ -ComunicWeb.common.langs.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]); + /** + * 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.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; + }, } \ No newline at end of file diff --git a/system/config/dev.config.php b/system/config/dev.config.php index ef65ff62..bc1f2af8 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -189,8 +189,9 @@ class Dev { "js/common/date.js", "js/common/system.js", - //Default langage + //Languages "js/langs/en.inc.js", + "js/langs/fr.inc.js", //Components //Mail caching