220

Possible Duplicate:
JavaScript for detecting browser language preference

I want to detect the language of the browser that is entering my site, if it's En or Fr. So I can redirect to the En page or the other page.

Also, can I detect mobile language?

2
  • 6
    Probably better off doing this with HTTP headers like HTTP_ACCEPT_LANGUAGE Commented Nov 20, 2011 at 5:59
  • Take a look at this similar question/answer. [StackOverflow - JavaScript for detecting browser language preference][1] [1]: stackoverflow.com/questions/1043339/… Commented Nov 20, 2011 at 6:05

2 Answers 2

497

Try this script to get your browser language

<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage; 
alert ("The language is: " + userLang);
</script>

Sign up to request clarification or add additional context in comments.

12 Comments

equivalent and more JS-typical: navigator.language || navigator.userLanguage
My testing suggests that while this works in IE and Firefox, it does not work in Chrome. Chrome's navigator.language is always the language configuration of the installation of windows, rather than the language configuration of the browser.
+1 @kybernetikos, This is something to be addressed since most of the people use Chrome.
If you want to use a library, you can also use acceptedlanguages.js which is entirely javascript and browser compatible covering all the cases: github.com/leighmcculloch/acceptedlanguages.js
Update: There is now (2020) an experimental feature supported by all modern browsers that returns an array of language preference: navigator.languages //["en-US", "zh-CN", "ja-JP"] This should work on at least 95% of browsers in 2020.
|
55

At this point navigator.userLanguage has long since been deprecated, so you can get the user's language tags via navigator.language directly:

const lang = navigator.language;

If you need more granular information, you can create an Intl.Locale object:

const locale = new Intl.Locale(navigator.language);

switch (locale.language) {
  case 'en':
    // do something for English
    break;
  case 'fr':
    // do something for French
    break;
  ...
}

In my original answer from 14 years ago, I mentioned that language detection should be done on the server. While I don't think that's fully wrong, it's also not fully right either. A lot has changed in the last 14 years.

It's worth calling out the existence of the Accept-Language header in browser http requests, and that for some payloads it is probably beneficial to perform translation server-side with the initial request to avoid additional unnecessary round-trips. However, there are also situations where it's entirely valid to use the user's language in client-side scripts, so use your best judgement.

7 Comments

why should it be done on the server? seems like a redundant comment.
@Paul, language detection should be done on the server so that the content is served in the appropriate language. To do otherwise would be wasteful of the user's bandwidth.
@zzzzBov I've been working on a Ember.js app with full language support, where I use the navigator.language as part of my API calls, so no user bandwidth is wasted. But I guess your right. It's an edge case.
This answer is pretty old but today SPA works most of the time on static server-less env, so doing this with Javascript is more than legit.
@SeLeCtRa, I appreciate the enthusiasm behind your edit, but I think you'd be better off writing your own answer than trying to revive this one.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.