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 only needed the primary component for my needs, but you can easily just use the full string. Works with latest Chrome, Firefox, Safari and IE10+.
If you don't want to rely on an external server and you have one of your own you can use a simple PHP script to achieve the same behavior as @DanSingerman answer.
languageDetector.php:
And just change this lines from the jQuery script:
i had a diffrent approach, this might help someone in the future:
the customer wanted a page where you can swap languages. i needed to format numbers by that setting (not the browser setting / not by any predefined setting)
so i set an initial setting depending on the config settings (i18n)
later in the script i used a function to determine what numberformating i need
so i used the set language of the page and as a fallback i used the browser settings.
which should be helpfull for testing purposes aswell.
depending on your customers you might not need that settings.
If you are using ASP .NET MVC and you want to get the Accepted-Languages header from JavaScript then here is a workaround example that does not involve any asynchronous requests.
In your .cshtml file, store the header securely in a div's data- attribute:
Then your JavaScript code can access the info, e.g. using JQuery:
Of course you can use a similar approach with other server technologies as others have mentioned.
If you have control of a backend and are using django, a 4 line implementation of Dan's idea is:
then in urls.py:
and on the front end:
(you have to set DEFAULT_LANG in settings.py of course)
There is no decent way to get that setting, at least not something browser independent.
But the server has that info, because it is part of the HTTP request header (the Accept-Language field, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4)
So the only reliable way is to get an answer back from the server. You will need something that runs on the server (like .asp, .jsp, .php, CGI) and that "thing" can return that info. Good examples here: http://www.developershome.com/wap/detection/detection.asp?page=readHeader