From 5b9b372ff917dca1c0ae74a8f370078d5e40592d Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 8 Jan 2017 15:54:25 +0100 Subject: [PATCH] Created and implemented language module --- assets/js/common/api.js | 34 +++++----- assets/js/common/debug.js | 20 ++++++ assets/js/common/errors.js | 6 +- assets/js/common/functionsSchema.js | 96 +++++++++++++++++++++++++++++ assets/js/common/jsFiles.js | 26 ++++++++ assets/js/common/langs.js | 78 +++++++++++++++++++++++ assets/js/common/messages.js | 2 +- assets/js/init.js | 30 +++++++++ assets/js/langs/en.inc.js | 11 ++++ assets/js/langs/fr.inc.js | 10 +++ corePage/config/dev.config.php | 18 +++++- index.php | 20 +++--- 12 files changed, 321 insertions(+), 30 deletions(-) create mode 100644 assets/js/common/debug.js create mode 100644 assets/js/common/functionsSchema.js create mode 100644 assets/js/common/jsFiles.js create mode 100644 assets/js/common/langs.js create mode 100644 assets/js/init.js create mode 100644 assets/js/langs/en.inc.js create mode 100644 assets/js/langs/fr.inc.js diff --git a/assets/js/common/api.js b/assets/js/common/api.js index 0cb47579..afba36db 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -8,29 +8,25 @@ * Make an API request * * @param {String} apiURI The URI to call in the API - * @param {Array} params The params to include in request + * @param {Object} params The params to include in request * @param {Function} nextAction What to do next */ -ComunicWeb.makeAPIrequest = function(apiURI, params, nextAction){ +ComunicWeb.common.network.makeAPIrequest = function(apiURI, params, nextAction){ //Prepare the request URL - var requestURL = config['API_URL'] + apiURI; + var requestURL = ComunicWeb.__config.apiURL + apiURI; //Prepare data to send in request var count = 0; var datas = ""; - for(i in values){ - //We check we are still in the initial array values - if(count < values.length){ - //We add a "&" if it isn't the first param - if(count != 0) - data += "&"; - - //We check the field's existence - if(values[i][0]) - datas += encodeURIComponent(values[i][0]) + "=" + encodeURIComponent(values[i][1]); + for(paramName in params){ + //We add a "&" if it isn't the first param + if(count != 0) + datas += "&"; - count++; - } + //We add field value + datas += encodeURIComponent(paramName) + "=" + encodeURIComponent(params[paramName]); + + count++; //Increment counter } //Create request @@ -45,7 +41,13 @@ ComunicWeb.makeAPIrequest = function(apiURI, params, nextAction){ var result = {}; //We can do the next step - //nextAction(apiXHR.responseText); + nextAction(apiXHR.responseText); } } + + //Set request headers + apiXHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + + //Submit request + apiXHR.send(datas); }; \ No newline at end of file diff --git a/assets/js/common/debug.js b/assets/js/common/debug.js new file mode 100644 index 00000000..bf6a35eb --- /dev/null +++ b/assets/js/common/debug.js @@ -0,0 +1,20 @@ +/** + * Debug functions + * + * @author Pierre HUBERT + */ + +/** + * Display message on browser console + * + * @param {String} message The message to show on browser console + */ +ComunicWeb.debug.logMessage = function(message){ + //We check we are not in production mode + if(ComunicWeb.__config.productionMode != 1){ + console.log("ComunicWebApp debug message", message); + } + + //Everything seems ok + return 0; +}; \ No newline at end of file diff --git a/assets/js/common/errors.js b/assets/js/common/errors.js index 083ca0dd..b1f151e3 100644 --- a/assets/js/common/errors.js +++ b/assets/js/common/errors.js @@ -5,11 +5,11 @@ */ /** - * Show a fatal error + * Handle and show a fatal error * * @param {String} errorMessage Error message */ -ComunicWeb.fatalError = function(errorMessage){ +ComunicWeb.common.error.fatalError = function(errorMessage){ //Make a black splash screen var splashScreen = document.createElement("div"); splashScreen.style.position = "fixed"; @@ -20,7 +20,7 @@ ComunicWeb.fatalError = function(errorMessage){ splashScreen.style.backgroundColor = "#000000"; //Show a message on screen to inform user - var messageElem = this.__createCalloutElem("Fatal error", "A fatal error occured : " + errorMessage + ". Please try to refresh the page...", "danger"); + var messageElem = ComunicWeb.common.messages.createCalloutElem("Fatal error", "A fatal error occured : " + errorMessage + ". Please try to refresh the page...", "danger"); messageElem.style.position = "relative"; messageElem.style.margin = "auto"; messageElem.style.width = "50%"; diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js new file mode 100644 index 00000000..c1baab4d --- /dev/null +++ b/assets/js/common/functionsSchema.js @@ -0,0 +1,96 @@ +/** + * Comunic WebApp schema + * + * @author Pierre HUBERT + */ +var ComunicWeb = { + + /** + * Configuration inclusion + */ + __config: ComunicConfig, + + /** + * Common functions + */ + common:{ + /** + * Network functions + */ + network: { + + /** + * Make an API request + */ + makeAPIrequest: function(apiURI, params, nextAction){}, + }, + + /** + * Langs functions + */ + langs: { + /** + * Return current language + */ + getCurrentLanguage: function(){}, + + /** + * Include and install specified language + */ + installLanguage: function(languageID){}, + + /** + * Initializate languages + */ + initLanguages: function(){}, + + /** + * Return a string in correct language + */ + getTranslatedText: function(stringName, stringParams){}, + }, + + /** + * Messages functions + */ + messages: { + + /** + * Create and return a callout element + */ + createCalloutElem: function(calloutTitle, calloutMessage, calloutType){}, + }, + + /** + * Error functions + */ + error:{ + + /** + * Handle and show a fatal error + */ + fatalError: function(errorMessage){}, + }, + + /** + * Operations on JS files + */ + jsFiles:{ + + /** + * Include a Javascript file + */ + includeFile: function(fileURL){}, + }, + }, + + /** + * Debug functions + */ + debug:{ + /** + * Display message on browser console + */ + logMessage: function(message){}, + } +} \ No newline at end of file diff --git a/assets/js/common/jsFiles.js b/assets/js/common/jsFiles.js new file mode 100644 index 00000000..ea356645 --- /dev/null +++ b/assets/js/common/jsFiles.js @@ -0,0 +1,26 @@ +/** + * Operations of Javascript files + * + * @author Pierre HUBERT + */ + +/** + * Include a Javascript file + * + * @param {String} fileURL The file URL + * @return {Boolean} False if it fails + */ +ComunicWeb.common.jsFiles.includeFile = function(fileURL){ + var fileElem = document.createElement("script"); + fileElem.type = "text/javascript"; + fileElem.src = fileURL; + + //Append the new element + document.body.appendChild(fileElem); + + //Debug message + ComunicWeb.debug.logMessage("Added JS file " + fileURL); + + //Everything is OK + return true; +} \ No newline at end of file diff --git a/assets/js/common/langs.js b/assets/js/common/langs.js new file mode 100644 index 00000000..7e475bde --- /dev/null +++ b/assets/js/common/langs.js @@ -0,0 +1,78 @@ +/** + * Lang functions + * + * @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; +}; + +/** + * 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"; + + //Include filename + ComunicWeb.common.jsFiles.includeFile(fileToInclude); +}; + +/** + * Language initiator + * + * @return Boolean False if it fails + */ +ComunicWeb.common.langs.initLanguages = function(){ + //Debug message + ComunicWeb.debug.logMessage("Get and install languages..."); + + //Get languages to install + this.__currentLang = this.getCurrentLanguage(); + this.__defaultLang = ComunicWeb.__config.defaultLanguage; + + //Install default language (made by default) + //this.installLanguage(this.__defaultLang); + + //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; +} + +/** + * 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]); + } + } + + return string; +} \ No newline at end of file diff --git a/assets/js/common/messages.js b/assets/js/common/messages.js index ffab898f..4c8f332f 100644 --- a/assets/js/common/messages.js +++ b/assets/js/common/messages.js @@ -11,7 +11,7 @@ * @param {String} calloutMessage The message of the callout * @param {String} calloutType The type of the callout (danger, info, warning, success) */ -ComunicWeb.__createCalloutElem = function(calloutTitle, calloutMessage, calloutType){ +ComunicWeb.common.messages.createCalloutElem = function(calloutTitle, calloutMessage, calloutType){ //Prepare callout message calloutMessage = "

" + calloutMessage + "

"; diff --git a/assets/js/init.js b/assets/js/init.js new file mode 100644 index 00000000..42208310 --- /dev/null +++ b/assets/js/init.js @@ -0,0 +1,30 @@ +/** + * Comunic WebApp init script + * + * @author Pierre HUBERT + */ + +//Anonymous function +(function(){ + + //Start init + ComunicWeb.debug.logMessage("Start initialization..."); + + /** + * Language initator + */ + ComunicWeb.common.langs.initLanguages(); + + //End of init + ComunicWeb.debug.logMessage("Application is ready !"); +})(); + +//Create a quick language access function shorcut +function lang(stringName, stringParams){ + //Check if any params has been specified + if(!stringParams) + var stringParams = []; + + //Call function + return ComunicWeb.common.langs.getTranslatedText(stringName, stringParams); +} \ No newline at end of file diff --git a/assets/js/langs/en.inc.js b/assets/js/langs/en.inc.js new file mode 100644 index 00000000..50e2efa9 --- /dev/null +++ b/assets/js/langs/en.inc.js @@ -0,0 +1,11 @@ +/** + * English language + * + * @author Pierre HUBERT + */ +ComunicWeb.common.langs.en = { + //Basic messages + "hello": "hello", + + "__number_received_messages": "You have received %p messages", +} \ No newline at end of file diff --git a/assets/js/langs/fr.inc.js b/assets/js/langs/fr.inc.js new file mode 100644 index 00000000..6ff27956 --- /dev/null +++ b/assets/js/langs/fr.inc.js @@ -0,0 +1,10 @@ +/** + * French language + * + * @author Pierre HUBERT + */ +ComunicWeb.common.langs.fr = { + //Basic messages + "hello": "bonjour", + "__number_received_messages": "Vous avez reçu %p messages", +} \ No newline at end of file diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index c193790f..497c1766 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -6,7 +6,7 @@ */ //Path to assets -$config['pathAssets'] = "assets/"; +$config['pathAssets'] = $config['siteURL']."assets/"; //CSS files to include $config['CSSfiles'] = array( @@ -21,7 +21,21 @@ $config['CSSfiles'] = array( $config['JSfiles'] = array( "%PATH_ASSETS%adminLTE/plugins/jQuery/jquery-2.2.3.min.js", "%PATH_ASSETS%adminLTE/plugins/jquery-ui/jquery-ui.min.js", + "%PATH_ASSETS%js/common/functionsSchema.js", "%PATH_ASSETS%js/common/api.js", "%PATH_ASSETS%js/common/errors.js", "%PATH_ASSETS%js/common/messages.js", -); \ No newline at end of file + "%PATH_ASSETS%js/common/langs.js", + "%PATH_ASSETS%js/common/jsFiles.js", + "%PATH_ASSETS%js/common/debug.js", + "%PATH_ASSETS%js/langs/en.inc.js", + + //Init script + "%PATH_ASSETS%js/init.js", +); + +//JS language path +$config['languagesPath'] = "%PATH_ASSETS%js/langs/"; + +//Production mode +$config['productionMode'] = 0; \ No newline at end of file diff --git a/index.php b/index.php index 58acca70..4e54114d 100644 --- a/index.php +++ b/index.php @@ -23,18 +23,22 @@ } ?> - - -