199

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?

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
ghs.alshareef
  • 2,009
  • 2
  • 13
  • 10
  • 6
    Probably better off doing this with HTTP headers like `HTTP_ACCEPT_LANGUAGE` – Robert Nov 20 '11 at 05:59
  • Take a look at this similar question/answer. [StackOverflow - JavaScript for detecting browser language preference][1] [1]: http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – sicks Nov 20 '11 at 06:05

2 Answers2

464

Try this script to get your browser language

<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage; 
alert ("The language is: " + userLang);
</script>
starball
  • 20,030
  • 7
  • 43
  • 238
Alim Ul Gias
  • 6,351
  • 2
  • 28
  • 39
  • 8
    equivalent and more JS-typical: `navigator.language || navigator.userLanguage` – Eamon Nerbonne Sep 11 '12 at 15:06
  • 15
    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. – kybernetikos Jul 31 '13 at 14:04
  • 3
    +1 @kybernetikos, This is something to be addressed since most of the people use Chrome. – boburShox Sep 26 '13 at 12:10
  • 1
    Running Mac OS X, I tried Firefox, Chrome, and Safari, all returned the language of my box, did not matter what language the page is. – cevaris Feb 09 '14 at 22:24
  • 1
    will not work in IE11 – ses Mar 17 '15 at 16:22
  • 1
    @ses @cevaris @kybernetikos browser updates since 2011 mean that adding `|| navigator.languages` should increase compatbility especially with newer versions of browsers. – Mousey Aug 15 '15 at 01:47
  • 6
    If you want to use a library, you can also use `acceptedlanguages.js` which is entirely javascript and browser compatible covering all the cases: https://github.com/leighmcculloch/acceptedlanguages.js – Leigh McCulloch Sep 12 '15 at 21:13
  • It doesn't work for me on IE 11 and Microsoft Edge (64bit, Windows 10 - it seems to be configured by Control Panel settings on this system so it may be the issue) – SathOkh Mar 15 '16 at 18:40
  • Not working with IE always giving en-US as the language – Dark Army Aug 19 '16 at 07:31
  • @SathOkh + 1 on this. navigator.language exists in IE but never gets updated when changing the browser language. navigator.languages works fine in Chrome/FF – LT86 Feb 27 '17 at 13:07
  • 10
    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. – Cornelius Roemer Mar 09 '20 at 13:13
  • var userLang = (navigator.language || navigator.userLanguage).slice(0,2) – Extrange planet Jun 25 '23 at 13:29
55

The "JavaScript" way:

var lang = navigator.language || navigator.userLanguage; //no ?s necessary

Really you should be doing language detection on the server, but if it's absolutely necessary to know/use via JavaScript, it can be gotten.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 24
    why should it be done on the server? seems like a redundant comment. – Paul Sep 28 '14 at 04:45
  • 3
    @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 Sep 28 '14 at 05:09
  • 5
    @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. – Paul Sep 28 '14 at 05:59
  • 36
    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. – Wanny Miarelli Jul 13 '18 at 13:56