javascript 确定 google Chrome 的浏览器语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16787138/
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
javascript determine browser language for google Chrome
提问by user2369009
I am trying to determine the browser's language set on Google Chrome, in Settings → Language and input settings, using JavaScript.
我正在尝试使用 JavaScript 在“设置”→“语言和输入设置”中确定 Google Chrome上设置的浏览器语言。
I'm trying to do this like so:
我正在尝试这样做:
var lng=window.navigator.language;
alert(lng);
In Firefox, is working fine, showing the correct language.
I am searching for a way to determine this in Google Chrome, because for the case can-Fr
, it shows to me en-US
.
在Firefox 中,工作正常,显示正确的语言。我正在寻找一种在 Google Chrome 中确定这一点的方法,因为对于这种情况can-Fr
,它会向我显示en-US
。
回答by CodingIntrigue
var lng = window.navigator.userLanguage || window.navigator.language;
Works in Chrome too, although sometimes it may not reflect the actual browser setting. In this case you might need to retrieve it from the HTTP Headers. Unfortunately the only way to retrieve this via Javascript is by pinging an AJAX request, see: JavaScript for detecting browser language preference
也适用于 Chrome,但有时它可能无法反映实际的浏览器设置。在这种情况下,您可能需要从 HTTP 标头中检索它。不幸的是,通过 Javascript 检索它的唯一方法是 ping AJAX 请求,请参阅:用于检测浏览器语言偏好的 JavaScript