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

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

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

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

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

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


当前回答

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语言

如果您正在开发Chrome应用程序/扩展使用Chrome。i18n API。

chrome.i18n.getAcceptLanguages(function(languages) {
  console.log(languages);
  // ["en-AU", "en", "en-US"]
});

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

如果你控制了一个后端并且使用django, Dan的想法的4行实现是:

def get_browser_lang(request):
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
    return JsonResponse({'response': request.META['HTTP_ACCEPT_LANGUAGE']})
else:
    return JsonResponse({'response': settings.DEFAULT_LANG})

然后在urls.py中:

url(r'^browserlang/$', views.get_browser_lang, name='get_browser_lang'),

在前端:

$.get(lg('SERVER') + 'browserlang/', function(data){
    var lang_code = data.response.split(',')[0].split(';')[0].split('-')[0];
});

(当然,你必须在settings.py中设置DEFAULT_LANG)

如果你使用的是ASP . net MVC,你想从JavaScript中获得accept - languages头,那么这里有一个不涉及任何异步请求的变通例子。

在你的.cshtml文件中,将头部安全地存储在div的data- attribute中:

<div data-languages="@Json.Encode(HttpContext.Current.Request.UserLanguages)"></div>

然后你的JavaScript代码可以访问这些信息,例如使用JQuery:

<script type="text/javascript">
$('[data-languages]').each(function () {
    var languages = $(this).data("languages");
    for (var i = 0; i < languages.length; i++) {
        var regex = /[-;]/;
        console.log(languages[i].split(regex)[0]);
    }
});
</script>

当然,您也可以像其他人提到的那样,对其他服务器技术使用类似的方法。