A common issue in a web application is handling the automatic language detection and the selection of a specific language made by an user.
A possible approach is to keep the language handling client side and handle it through JavaScript. In order to do this you can manipulate the URL and append and recover values using the jsuri library hosted on google code and encapsulate the language detection in a script included in each page.
The steps the script has to perform are:
A possible approach is to keep the language handling client side and handle it through JavaScript. In order to do this you can manipulate the URL and append and recover values using the jsuri library hosted on google code and encapsulate the language detection in a script included in each page.
The steps the script has to perform are:
- Declare a varbiable to store the current language
- Recover the browser language
- Check if the URL already contains a language query string param
- Change the href value of your page or populate the variable that contains the current language
var currentLanguage;
if (navigator.appName == 'Netscape'){
var language = navigator.language;
}else{
var language = navigator.browserLanguage;
}
if (language.indexOf('en') > -1) currentLanguage = "en";
else if (language.indexOf('it') > -1) currentLanguage = "it";
else currentLanguage = "en";
var uri = new jsUri(location.href);
if(!uri.getQueryParamValue('lang')){
uri.setQuery('?lang=' + currentLanguage);
location.href = uri;
}else{
currentLanguage = getQueryParamValue('lang');
}


