Created and implemented language module

This commit is contained in:
Pierre 2017-01-08 15:54:25 +01:00
parent d8e5c85b58
commit 5b9b372ff9
12 changed files with 321 additions and 30 deletions

View File

@ -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);
};

20
assets/js/common/debug.js Normal file
View File

@ -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;
};

View File

@ -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%";

View File

@ -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){},
}
}

View File

@ -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;
}

78
assets/js/common/langs.js Normal file
View File

@ -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;
}

View File

@ -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 = "<p>" + calloutMessage + "</p>";

30
assets/js/init.js Normal file
View File

@ -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);
}

11
assets/js/langs/en.inc.js Normal file
View File

@ -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",
}

10
assets/js/langs/fr.inc.js Normal file
View File

@ -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",
}

View File

@ -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",
);
"%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;

View File

@ -23,18 +23,22 @@
}
?>
<!-- Main website object -->
<script>
var ComunicWeb = {};
</script>
<!-- Javascript config -->
<script>
//Configuration definition
ComunicWeb.__config = [];
var ComunicConfig = {
//Production mode
productionMode: <?php echo config['productionMode']; ?>,
//API URL
ComunicWeb.__config['API_URL'] = "<?php echo config['API_URL']; ?>";
//apiURL
apiURL: "<?php echo config['API_URL']; ?>",
//Default language
defaultLanguage: "en",
//LanguagesPath
languagesPath: "<?php echo str_replace("%PATH_ASSETS%", config['pathAssets'], config['languagesPath']); ?>",
};
</script>
</head>