For a project I have to provide 2 versions of a webpage: one English and one French. As I'm in the UK, the browser (IE11) is naturally installed in the local language (en-GB). To test french, I went to
Internet-Options->Languages->Set Language Preference->Add a Language (French-France)
(or select the language if already there)
and make sure French is at the top. I have a JSF page where the language is detected using Java, and that works fine, all text is taken from the French properties file, rather than the English properties file, switching automatically when changing the language between English and French. However, for one web page I need to detect the current language in JavaScript, and whatever I try, it remains en-GB.
I tried this code:
var language = navigator.languages && navigator.languages[0] ||
navigator.language ||
navigator.userLanguage;
It returns 'en-GB'. Tried on its own in the F12 debugger:
navigator.languages returns: undefined
navigator.userLanguage: en-GB
navigator.language: undefined
navigator.browserLanguage: en-US
This is IE11 version 11.0.9600.17031
I'm also in Windows 8, and the windows-home screen (after accidentally pressing on the windows key...) also shows everything in French (news, weather etc), so French is selected, but how to detect it in JavaScript?
Internet Explorer does honour the language preferences set in Windows, but apparently there is no way to access this information from JavaScript. The best you can do is to detect the language on the server, and return the information to your script.
If you want to manually view the Accept-Language
header, press [F12] to turn on developer tools, then [Ctrl]+[4] for the network tab, and make a request (you could simply press [F5]). On the right you'll see a panel for the headers. In "Request Headers" you'll see "Accept-Language", which should be the value(s) you want. I repeat though, as far as I know, there's no way to get the same information purely from JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With