Browser language detection: inconsistency between user agent and window.navigator.language

I define the browser language as follows:

var language = window.navigator.userLanguage || window.navigator.language 

Then I send this value to my web server with an AJAX call.
When I check the results, I saw a strange thing. For example, the browser user agent says that the current locale is tr-tr , but when I check window.navigator.language , I get the result as en .

All of these browsers say their language is en :

 Mozilla/5.0 (Linux; U; Android 2.2.2; tr-tr; GM FOX Build/HuaweiU8350) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1, Mozilla/5.0 (Linux; U; Android 2.2.2; tr-tr; LG-P503 Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MMS/LG-Android-MMS-V1.0/1.2 Mozilla/5.0 (Linux; U; Android 2.3.4; tr-tr; GT-S5670 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 2.3.6; tr-tr; GT-S5360 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 2.3.6; tr-tr; GT-S5830i Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S7233E/S723EJVKB1; U; Bada/1.0; tr-tr) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.0 Mobile WQVGA SMM-MMS/1.2.0 OPN-B Mozilla/5.0 (SAMSUNG; SAMSUNG-GT-S8500/S8500JVJE6; U; Bada/1.0; tr-tr) AppleWebKit/533.1 (KHTML, like Gecko) Dolfin/2.0 Mobile WVGA SMM-MMS/1.2.0 OPN-B Mozilla/5.0 (X11; U; Linux x86_64; tr-tr) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.114 Safari/537.36 Puffin/3.7.0.177AP Mozilla/5.0 (Series40; NokiaC2-02/07.63; Profile/MIDP-2.1 Configuration/CLDC-1.1) Gecko/20100401 S40OviBrowser/5.0.0.0.31 Mozilla/5.0 (Series40; Nokia311/07.36; Profile/MIDP-2.1 Configuration/CLDC-1.1) Gecko/20100401 S40OviBrowser/2.3.0.0.54 Mozilla/5.0 (Series40; NokiaC3-00/08.70; Profile/MIDP-2.1 Configuration/CLDC-1.1) Gecko/20100401 S40OviBrowser/2.3.0.0.49 Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; ASU2JS; rv:11.0) like Gecko 

Should I consider something else for browser detection?

My problem is detecting browser language. For user agents, the current language is "tr", but window.navigator.language gives "en". Is this a known issue? Should I trust the window.navigator.language value? Should I parse user agent for language?

Edit:
I also checked the Accept-language header for these requests. These values ​​are consistent with the query user agent string.
So window.navigator.language says "ru", the Accept-language heading is "tr-TR, en-US", the user agent string has the word "tr tr".

+6
source share
2 answers

For browsers, there are two corresponding languages: the browser user interface language and the language in which the user prefers to browse the web. These languages ​​are often identical, but they do not have to be. For example, a Norwegian user often uses a browser with an English-language user interface, but prefers to browse the web in Norwegian.

The user agent typically displays the language of the browser user interface. As a rule, this language is fixed and can only be changed with significant efforts - for example, when reinstalling the browser (Chrome actually allows you to customize the browser language, but even there you need to delve into the browser settings).

Preferred content languages, on the other hand, are easily customizable in all browsers. The user can select any number of languages ​​and their priority. This list will be sent to the Accept-Language header and allow the server to configure the content accordingly (for example, by default redirecting the user to the version for a specific page).

So far it is very simple - usually you want to consider Accept-Language for your solutions, and not for the user agent. The only complication is knowing what the window.navigator.language property window.navigator.language . Initially, window.navigator.language intended to provide you with the language of the browser user interface (which means that it will correspond to the user agent). However, websites use it to decide which version of the language the user is presented with - so some browsers have moved to generate their value based on the Accept-Language header (usually by taking the first entry in the list). This is definitely for Firefox 5 and above, and Safari 7 uses the same logic (I don't know when it was introduced there).

Side notes:

+18
source

According to what you said and your reluctance to use this answer , it looks like you should use the Accept-language header or parse the userAgent string (although, for example, Firefox no longer puts the language on that string). Since you have access to the HTTP headers, I would recommend using them.

My suspicion about the inconsistencies you discovered is that browsers can be compiled using a locale and then run by a user who wants a different language (think about who just loads the default assembly by default or someone who periodically switches languages).

You will need to check out the various configurations and see what works for you. Consider testing with various language versions of Firefox and Chrome configurations . You can also test by polling your users (use a cookie). Over time, you may not need a cookie because you will get it β€œright” by default due to these tests, but there will always be an exception, so it’s still good for the end user to override your (smart) default.

+2
source

All Articles