Oct 11
31
Detect Locale with javascript
Leave a comment »
There may be times where you would want to detect the language of the users browser. I would normally use php to detect the users browser language. However, when this is not possible, it can also be done using the following javascript:
thelocale = 'en-GB'; // set default locale
if ( navigator ) { if ( navigator.language ) {// for netscape thelocale = navigator.language; } else if ( navigator.browserLanguage ) {// internet explorer 5.5 or above
thelocale = navigator.browserLanguage;
}
else if ( navigator.systemLanguage ) {// windows specific
thelocale = navigator.systemLanguage;
}
else if ( navigator.userLanguage ) {
thelocale = navigator.userLanguage;
}
}