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

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

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

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

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

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


当前回答

如果你只需要支持某些现代浏览器,那么你现在可以使用:

navigator.languages

它按用户指定的顺序返回用户的语言首选项数组。

截至目前(2014年9月),这项工作: 铬(v37), Firefox (v32)和 歌剧(v24)

但不是在: IE (v11)

其他回答

Javascript方法:

var language = window.navigator.userLanguage || window.navigator.language;//returns value like 'en-us'

如果你正在使用jQuery。I18n插件,你可以使用:

jQuery.i18n.browserLang();//returns value like '"en-US"'

Dan Singerman的回答有一个问题,因为jQuery ajax的异步特性,获取的头文件必须立即使用。但是,使用他的谷歌应用服务器,我编写了以下内容,以便将报头设置为初始设置的一部分,并可以在以后使用。

<html>
<head>
<script>

    var bLocale='raw'; // can be used at any other place

    function processHeaders(headers){
        bLocale=headers['Accept-Language'];
        comma=bLocale.indexOf(',');
        if(comma>0) bLocale=bLocale.substring(0, comma);
    }

</script>

<script src="jquery-1.11.0.js"></script>

<script type="application/javascript" src="http://ajaxhttpheaders.appspot.com?callback=processHeaders"></script>

</head>
<body>

<h1 id="bLocale">Should be the browser locale here</h1>

</body>

<script>

    $("#bLocale").text(bLocale);

</script>
</html>

导航器。用于IE的userLanguage

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

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没有列出它

我想分享我的代码,因为它的工作,它是不同于其他给出的答案。 在这个例子中,如果你说法语(法国,比利时或其他法语语言),你会被重定向到法语页面,否则会重定向到英语页面,这取决于浏览器的配置:

<脚本type = " text / javascript”> 美元(文档)。Ready (function () { var userLang = navigator。||导航语言; if (userLang.startsWith("fr")) { Window.location.href = '../fr/index.html'; } 其他{ Window.location.href = '../en/index.html'; } }); > < /脚本