This question already has an answer here:
I have been trying to detect the browser language preference using JavaScript.
If I set the browser language in IE in Tools>Internet Options>General>Languages
, how do I read this value using JavaScript?
Same problem for Firefox. I'm not able to detect the setting for tools>options>content>languages
using navigator.language
.
Using navigator.userLanguage
, it detects the setting done thru
Start>ControlPanel>RegionalandLanguageOptions>Regional Options
tab.
I have tested with navigator.browserLanguage
and navigator.systemLanguage
but neither returns the value for the first setting(Tools>InternetOptions>General>Languages
)
I found a link which discusses this in detail, but the question remains unanswered :(
I can't find a single reference that state that it's possible without involving the serverside.
MSDN on:
From browserLanguage:
Furthermore, it looks like
browserLanguage
is deprecated cause IE8 doesn't list itFirst of all, excuse me for my English. I would like to share my code, because it works and it is different than the others given anwers. In this exemple,if you speak French (France, Belgium or other french language) you are redirected on the french page, otherwise on the english page, depending on the browser configuration :
window.navigator.userLanguage
is IE only and it's the language set in Windows Control Panel - Regional Options and NOT browser language, but you could suppose that a user using a machine with Window Regional settings set to France is probably a French user.navigator.language
is FireFox and all other browser.Some language code:
'it'
= italy,'en-US'
= english US, etc.As pointed out by rcoup and The WebMacheter in comments below, this workaround won't let you discriminate among English dialects when users are viewing website in browsers other than IE.
window.navigator.language
(Chrome/FF/Safari) returns always browser language and not browser's preferred language, but: "it's pretty common for English speakers (gb, au, nz, etc) to have an en-us version of Firefox/Chrome/Safari." Hencewindow.navigator.language
will still returnen-US
even if the user preferred language isen-GB
.I've just come up with this. It combines newer JS destructuring syntax with a few standard operations to retrieve the language and locale.
Hope it helps someone
I've been using Hamid's answer for a while, but it in cases where the languages array is like ["en", "en-GB", "en-US", "fr-FR", "fr", "en-ZA"] it will return "en", when "en-GB" would be a better match.
My update (below) will return the first long format code e.g. "en-GB", otherwise it will return the first short code e.g. "en", otherwise it will return null.
I think the main problem here is that the browser settings don't actually affect the
navigator.language
property that is obtained via javascript.What they do affect is the HTTP 'Accept-Language' header, but it appears this value is not available through javascript at all. (Probably why @anddoutoi states he can't find a reference for it that doesn't involve server side.)
I have coded a workaround: I've knocked up a google app engine script at http://ajaxhttpheaders.appspot.com that will return you the HTTP request headers via JSONP.
(Note: this is a hack only to be used if you do not have a back end available that can do this for you. In general you should not be making calls to third party hosted javascript files in your pages unless you have a very high level of trust in the host.)
I intend to leave it there in perpetuity so feel free to use it in your code.
Here's some example code (in jQuery) for how you might use it
Hope someone finds this useful.
Edit: I have written a small jQuery plugin on github that wraps this functionality: https://github.com/dansingerman/jQuery-Browser-Language
Edit 2: As requested here is the code that is running on AppEngine (super trivial really):
Edit3: Have open sourced the app engine code here: https://github.com/dansingerman/app-engine-headers