我一直在尝试使用JavaScript检测浏览器语言首选项。

如果我在IE的“工具>Internet选项>常规>语言”中设置浏览器语言,如何使用JavaScript读取此值?

火狐也有同样的问题。我无法检测工具>选项>内容>语言使用导航器.language的设置。

使用导航器。userLanguage,它检测设置通过 打开>ControlPanel>RegionalandLanguageOptions>Regional Options选项卡。

我已经用navigator测试过了。browserLanguage和navigator。systemLanguage但都不返回第一个设置的值(Tools>InternetOptions>General>Languages)

我找到了一个详细讨论这个问题的链接,但这个问题仍然没有答案:(


当前回答

I can't find a single reference that state that it's possible without involving the serverside.

MSDN上:

navigator.browserLanguage navigator.systemLanguage navigator.userLanguage

从browserLanguage:

In Microsoft Internet Explorer 4.0 and earlier, the browserLanguage property reflects the language of the installed browser's user interface. For example, if you install a Japanese version of Windows Internet Explorer on an English operating system, browserLanguage would be ja. In Internet Explorer 5 and later, however, the browserLanguage property reflects the language of the operating system regardless of the installed language version of Internet Explorer. However, if Microsoft Windows 2000 MultiLanguage version is installed, the browserLanguage property indicates the language set in the operating system's current menus and dialogs, as found in the Regional Options of the Control Panel. For example, if you install a Japanese version of Internet Explorer 5 on an English (United Kingdom) operating system, browserLanguage would be en-gb. If you install Windows 2000 MultiLanguage version and set the language of the menus and dialogs to French, browserLanguage would be fr, even though you have a Japanese version of Internet Explorer. Note This property does not indicate the language or languages set by the user in Language Preferences, located in the Internet Options dialog box.

此外,它看起来像是browserLanguage被弃用了,因为IE8没有列出它

其他回答

var language = window.navigator.userLanguage || window.navigator.language;
alert(language); //works IE/SAFARI/CHROME/FF

Window .navigator. userlanguage仅适用于IE,并且它是Windows控制面板-区域选项中设置的语言,而不是浏览器语言,但是你可以假设使用窗口区域设置为法国的机器的用户可能是法国用户。

导航器。语言是火狐和所有其他浏览器。

一些语言代码:'it' =意大利语,'en-US' =英语美国,等等。


正如rcoup和The WebMacheter在下面的评论中指出的那样,当用户使用IE以外的浏览器浏览网站时,这种解决方法不会让你区分英语方言。

window.navigator.language (Chrome/FF/Safari)总是返回浏览器语言,而不是浏览器的首选语言,但是:“对于说英语的人(gb, au, nz等)来说,有一个en-us版本的Firefox/Chrome/Safari是很常见的。”因此,window.navigator.language仍然会返回en-US,即使用户首选的语言是en-GB。

我有一个不同的方法,这可能会在未来帮助到一些人:

客户想要一个可以交换语言的页面。 我需要通过该设置格式化数字(不是浏览器设置/不是任何预定义的设置)

因此,我根据配置设置(i18n)设置了初始设置

$clang = $this->Session->read('Config.language');
echo "<script type='text/javascript'>var clang = '$clang'</script>";

后来在脚本中,我使用了一个函数来确定我需要的数字格式

function getLangsettings(){
  if(typeof clang === 'undefined') clang = navigator.language;
  //console.log(clang);
  switch(clang){
    case 'de':
    case 'de-de':
        return {precision : 2, thousand : ".", decimal : ","}
    case 'en':
    case 'en-gb':
    default:
        return {precision : 2, thousand : ",", decimal : "."}
  }
}

所以我使用了页面的设置语言,作为退步,我使用了浏览器设置。

这对测试也有帮助。

根据客户的不同,您可能不需要这些设置。

导航器。用于IE的userLanguage

窗口.导航员.萤火虫/歌剧/safari语言

对于这个问题,DanSingerman有一个很好的解决方案。

该语言的唯一可靠来源是http请求头。 因此,您需要一个服务器端脚本来回复请求报头,或者至少是Accept-Language字段。

这是一个非常简单的Node.js服务器,它应该与DanSingermans的jQuery插件兼容。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(JSON.stringify(req.headers));
}).listen(80,'0.0.0.0');

我也遇到了同样的问题,于是我编写了以下前端专用库,它为多种浏览器打包了代码。代码并不多,但不用在多个网站上复制和粘贴相同的代码。

Get it: acceptedlanguages.js

使用它:

<script src="acceptedlanguages.js"></script>
<script type="text/javascript">
  console.log('Accepted Languages:  ' + acceptedlanguages.accepted);
</script>

它总是返回一个数组,按用户偏好排序。在Safari和IE中,数组总是单长度的。在FF和Chrome中,它可能不止一种语言。