0

I have tested with different people and from my understanding the best way to determine the language for my site on Internet Explorer is by using systemLanguage

But I have found an issue.

One of the testers has this languages in the Internet Options

Please see this

The default language is English (the first), which is correct but the browser detects the second language and get's the German.

Please see this

Can someone explain me how do I retrieve the default (first) language from that list? Why IE displays German instead of English? At least it should be an array.

My code:

 var lang = window.navigator.systemLanguage || window.navigator.language;
    if (lang.indexOf('-') !== -1) {
        lang = lang.substr(0, lang.indexOf('-'));
    }
Olian04
  • 6,480
  • 2
  • 27
  • 54
Tiago Silva
  • 229
  • 2
  • 9
  • https://zzz.buzz/2016/01/13/detect-browser-language-in-javascript/ – Olian04 Oct 24 '19 at 15:29
  • I'm in favor of pqnet's answer. I think the standard way is to use `navigator.language`. As mentioned in [MDN](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language) and [w3schools](https://www.w3schools.com/jsref/prop_nav_language.asp), this property returns *the language of the browser UI* and *get the language of your browser*. For more information, you could also refer to [this thread](https://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser). – Yu Zhou Oct 25 '19 at 06:18

1 Answers1

3

systemLanguage is a IE-only api that returns the language edition of the operating system. Don't read it, rely on window.navigator.language which is standard

pqnet
  • 6,070
  • 1
  • 30
  • 51
  • This only works if you're targeting later version then IE11. If you need to support earlier IE version then `navigator.language` wont work. – Olian04 Oct 24 '19 at 15:29
  • you should rely on polyfills when targeting older browser versions. See https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills . If you're still into cooking your own IE language detection code, go for `browserLanguage` or `userLanguage` – pqnet Oct 24 '19 at 15:36
  • also, Microsoft is ending support for IE10 in january, so you may not worry too much about supporting that browser: https://support.microsoft.com/en-us/help/4488955/support-ending-for-internet-explorer-10 – pqnet Oct 24 '19 at 15:45