Monday, 15 October 2018

How to maintain changing state of language in expressJS

I`m working on a project using nodeJs, handlebars and expressJs framework. I add change language functionality using i18n-express module.This module add query string in the end of url when we are going to change the language. Now the issue is that when i have move one page to another page then query string is removed and lose his state.so how can i maintain state of language?? if user choose french language then all pages are open in french. This is what i want.

Code:

var i18n =  require("i18n-express");

app.use(i18n({
  translationsPath: path.join(__dirname, 'lang'), // <--- use here. Specify translations files path.
  siteLangs: ["ar","en","cn","fr","ge","he","hu","it","ja","ko","es","ru"],
  cookieLangName : 'ulang',
  textsVarName: 'translation'  
}));

Link to change the language

<a href="#!" id="" onclick=" return changeLanguage(this)"></a>

Onclick function to change the language

function changeLanguage(event){
   $('#languages img').attr('src','/images/flag-icons/'+event.id+'.png');
   var url = window.location.href;
   url = url.split("?")[0];
   url += '?clang='+event.id;
   window.location.href = url;
   localStorage.setItem("clang", '?clang='+event.id); //event.id returns locale name such as en, ar, sp, fr etc.
   //console.log(url);
}



from How to maintain changing state of language in expressJS

No comments:

Post a Comment