javascript 浏览器语言检测:用户代理和 window.navigator.language 不一致
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23415391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Browser language detection: inconsistency between user agent and window.navigator.language
提问by trante
I detect browser language like this:
我检测浏览器语言是这样的:
var language = window.navigator.userLanguage || window.navigator.language
Then I send this value to my webserver with AJAX call.
When I check the results I saw a weird thing. For example user agent of the browser says that current locale is tr-tr
, but when I check for window.navigator.language
I get the result as en
.
然后我通过 AJAX 调用将此值发送到我的网络服务器。
当我检查结果时,我看到了一个奇怪的事情。例如,浏览器的用户代理说当前语言环境是tr-tr
,但是当我检查时,window.navigator.language
我得到的结果是en
。
All these browsers says that their language is en
:
所有这些浏览器都说他们的语言是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 anything more for browser language detection ?
我应该考虑更多的浏览器语言检测吗?
My problem is detecting browser language. Regarding to user agents, current language is "tr", but window.navigator.language
gives "en". Is it a known problem ? Shouldn't I trust window.navigator.language
value ? Should I parse user agent for language ?
我的问题是检测浏览器语言。关于用户代理,当前语言是“tr”,但window.navigator.language
给出“en”。这是一个已知问题吗?我不应该相信window.navigator.language
价值吗?我应该解析语言的用户代理吗?
Edit:
I also checked Accept-language header for these requests. These values are consistent with request's user agent string.
So, window.navigator.language
says "en", Accept-language
header is "tr-TR, en-US", user agent string has "tr-tr" word.
编辑:
我还检查了这些请求的 Accept-language 标头。这些值与请求的用户代理字符串一致。
所以,window.navigator.language
说“en”,Accept-language
标题是“tr-TR,en-US”,用户代理字符串有“tr-tr”字样。
回答by Wladimir Palant
As far as browsers go, there are two relevant languages: the language of the browser's user interface and the language that the user prefers to view webpages in. These languages are often identical but they don't have to be. For example, a Norwegian user will often use a browser with an English-language user interface, yet prefer viewing web pages in Norwegian.
就浏览器而言,有两种相关语言:浏览器用户界面的语言和用户喜欢查看网页的语言。这些语言通常相同,但不一定相同。例如,挪威用户通常会使用具有英语用户界面的浏览器,但更喜欢以挪威语查看网页。
The user agent generally reflects the language of the browser's user interface. Typically, this language is fixed and can only be changed with significant effort - like reinstalling the browser (Chrome actually allows configuring browser language but even there you have to dig deep into browser preferences).
用户代理通常反映浏览器用户界面的语言。通常,这种语言是固定的,只能通过大量的努力来改变——比如重新安装浏览器(Chrome 实际上允许配置浏览器语言,但即使在那里你也必须深入研究浏览器首选项)。
The preferred content languages on the other hand are easily configured in all browsers. The user can select any number of languages and their priority. That list will be sent in the Accept-Language
headerand allows the server to adjust the content accordingly (e.g. redirect the user to a language-specific page version by default).
另一方面,首选的内容语言可以在所有浏览器中轻松配置。用户可以选择任意数量的语言及其优先级。该列表将在Accept-Language
标题中发送,并允许服务器相应地调整内容(例如,默认情况下将用户重定向到特定于语言的页面版本)。
So far it is very straightforward - you usually want to consider Accept-Language
for your decisions rather than the user agent. The only complication is knowing which one the window.navigator.language
property reflects. Originally, window.navigator.language
was meant to give you the language of the browser's user interface (meaning that it would match the user agent). However, websites use it to make decisions about what language version to present to the user - so some browsers went over to generating its value based on the Accept-Language
header (typically by taking the first entry in the list). That's definitely the case for Firefox 5 and higher, and Safari 7 uses the same logic (don't know when it was introduced there).
到目前为止,它非常简单 - 您通常希望考虑Accept-Language
您的决定而不是用户代理。唯一的复杂之处在于知道该window.navigator.language
属性反映的是哪一个。最初,window.navigator.language
旨在为您提供浏览器用户界面的语言(意味着它将与用户代理匹配)。然而,网站使用它来决定向用户呈现什么语言版本 - 因此一些浏览器开始根据Accept-Language
标题生成其值(通常通过获取列表中的第一个条目)。Firefox 5 及更高版本绝对是这种情况,而 Safari 7 使用相同的逻辑(不知道它是什么时候引入的)。
Side-notes:
旁注:
- As of Firefox 32 and Chrome 32 there is a new
window.navigator.languages
propertywhich returns the full list of content preferences that the user chose. - Internet Explorer has a non-standard
window.navigator.userLanguage
property.
- 从 Firefox 32 和 Chrome 32 开始,有一个新
window.navigator.languages
属性可以返回用户选择的内容首选项的完整列表。 - Internet Explorer 有一个非标准
window.navigator.userLanguage
属性。
回答by Adam Katz
Based on what you've said and your reluctance to use this answer, it looks like you should use the Accept-language
header or else parse the userAgent string (though e.g. Firefox doesn't place the language into that string any more). Because you actually have access to the HTTP headers, I would recommend using them.
根据您所说的以及您不愿使用此答案,您似乎应该使用Accept-language
标题或解析 userAgent 字符串(尽管例如 Firefox 不再将语言放入该字符串中)。因为您实际上可以访问 HTTP 标头,所以我建议使用它们。
My suspicion regarding the inconsistencies you found is that browsers can be compiledwith a locale and then runby a user who wants a different locale (consider somebody who just downloads the default en-US build or somebody who switches languages periodically).
我对您发现的不一致之处的怀疑是,浏览器可以使用语言环境编译,然后由想要不同语言环境的用户运行(考虑那些只下载默认 en-US 构建的人或定期切换语言的人)。
You'll have to test various configurations and see what works for you. Consider testing with various different Firefox locale buildsand Chrome configurations. You can also test by polling your users (use a cookie). Over time, perhaps you won't need the cookie because you'll get it "right" by default due to these tests, but there will always be an exception here or there, so it's still good to allow the end user to override your (smart) default.
您必须测试各种配置,看看哪些适合您。考虑使用各种不同的Firefox 语言环境构建和Chrome 配置进行测试。您还可以通过轮询用户进行测试(使用 cookie)。随着时间的推移,也许您将不需要 cookie,因为由于这些测试,默认情况下您会得到它“正确”,但是这里或那里总会有例外,因此允许最终用户覆盖您的(智能)默认。