42

Does anyone know how to obtain the browser culture from Firefox and Google Chrome using javascript? Note: This is an asp.net 3.5 web application.

The requirement is to try and set the application's display culture based on the browser culture. I have found very few bits and pieces of information for the other browsers but they do not seem to work.

I am able to get it in IE with the following snippet of code:

var browserCulture = this.clientInformation.browserLanguage;

Any info would be great!

The Sheek Geek
  • 4,126
  • 6
  • 38
  • 48
  • 3
    By "culture" do you mean the current locale preference set in the browser? (I have never heard that called "culture" before, but I'm uncultured.) – Pointy Apr 20 '10 at 20:05
  • Yes that is exactly what I mean. I'm used to working with asp localization and that's what it is called. Therefore, my scope on it being called "culture" is limited. No wonder I had such a hard time finding this information online! – The Sheek Geek Apr 21 '10 at 11:44
  • this is a related question: http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – Damien Jul 31 '14 at 14:33

4 Answers4

75

The following properties exist on the navigator object (which can also be known as clientInformation on IE but there's no reason ever to use that name):

  • language (non-IE, browser install language)
  • browserLanguage (IE, browser install language)
  • userLanguage (IE, user-level OS-wide language setting)
  • systemLanguage (IE, OS installation language)

But! You should never use any of these properties! They will be the wrong language in many cases.

None of them reflect the language settings the user actually gets to configure in the browser's ‘preferred languages’ UI, and they are difficult-to-impossible for users to change. You will cause big frustration by using any of these values without an additional easy manual way to switch languages.

The correct place you should sniff to decide what language to use by default, as configured by the normal browser UI, is the Accept-Language header passed to your server in the HTTP request. This is a ranked list of preferred languages from which you can pick, and it's what ASP.NET uses to guess an automatic client Culture, if you use that.

Unfortunately, this property is not available from JavaScript!

What you typically do is use your server side to parse the Accept-Language header and choose a single appropriate language to use from it. In ASP.NET you can get a pre-sorted list from HttpRequest.UserLanguages and pick the first that you like.

You then spit that language name out into a <script> element to pass the language information to the client side.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 4
    See https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.language ... all Browsers have since use the value in Accept-Language for their language property here. Don't worry about it anymore. – timdream Jun 14 '13 at 07:59
  • 5
    @timdream Careful, the phrase 'all browsers' seems a bit misleading, given that the document you linked to says that `window.navigator.language` isn't supported in IE and would require a proprietary IE option. – StackExchange What The Heck Aug 29 '13 at 10:52
  • 2
    I think that document is also just wrong about Chrome—I always get the browser-language *not* the first settings-language. The only browser where I see the first settings-language is Firefox, so far from “all browsers”. Even then, it's suboptimal because you only get the first language: if the user has said “give me English for preference, and French if you don't have that” but you only have French and German, that second preference won't be picked up. – bobince Aug 29 '13 at 11:07
  • 1
    I've created this library `acceptedlanguages.js` that provides you a simple function call and is lightweight: https://github.com/leighmcculloch/acceptedlanguages.js – Leigh McCulloch Sep 12 '15 at 21:14
  • 2017 note: use `navigator.languages` for Chrome and Firefox to get a list of user-preferred languages (defined by the user in browser; note though that `navigator.languages[0]` might be totally different than `navigator.language`). Then `navigator.language` which works in IE11+ and the rest. Then `navigator.userLanguage` for compat with old IEs. In IE11 `navigator.language === navigator.userLanguage`. The value of `userLanguage` can be changed by the user on Windows in Control Panel > Region and Language. – jakub.g Jan 24 '17 at 16:35
  • @jakub.g, in IE11 you simply cannot reliably determine the most preferred and supported language, you have to resort to your server sending back the `Accept-Language` header (processed or not). Also please do not solely use `navigator.languages[0]` (e.g. 'cn-CN') when available as that may be a language you have no translations for, in which case you would fall back to a default (e.g. 'en-GB') language. But `navigator.language[1]` (e.g. 'fr-FR') might still be supported by your site, yet your site would be displayed using the default culture. Please iterate through the array to find the best. – The Victor Jan 26 '17 at 10:27
12

Try this:

var l_lang;
  if (navigator.userLanguage) // Explorer
    l_lang = navigator.userLanguage;
  else if (navigator.language) // FF
    l_lang = navigator.language;
  else
    l_lang = "en";

Source: http://forums.digitalpoint.com/showthread.php?t=631706

Sologoub
  • 5,312
  • 6
  • 37
  • 65
  • 1
    realise this is a little old, but just for anyone coming across this now (like myself) navigator.language works for IE 11, so this code should work fine. – TygerKrash Jun 14 '16 at 15:03
7

navigator.languages is an array and contains all the selected languages in order. And only works for Chrome and Firefox.

It is not the same as navigator.language and by that I mean that navigator.language does not necessarily match navigator.languages[0].

Just to be clear.

Mats
  • 805
  • 9
  • 14
-1

To get locale in IE you need to write the server side code. I tried with servlet and got the accept language of the IE. It shows the languages which are selected by the user.Call the servlet in your client side and get the locale.

String name=request.getHeader("accept-language");
        AcceptClass ac=new AcceptClass();
        ac.setAccLang(name);
        Gson gs=new Gson();
          String json = gs.toJson(ac);
          response.setContentType("application/json");
          response.getWriter().write(json);
Prithivi Thiyagu
  • 129
  • 1
  • 11