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

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

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

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

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

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


当前回答

Var语言=导航器。语言和导航器。// Chrome / Firefox . languages[0] || 导航器。language || //所有浏览器 navigator.userLanguage;// IE <= 10 console.log(语言);

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/languages https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language

尝试PWA模板https://github.com/StartPolymer/progressive-web-app-template

其他回答

如果您不想依赖外部服务器,并且您有自己的服务器,您可以使用一个简单的PHP脚本来实现与@DanSingerman answer相同的行为。

languageDetector.php:

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
echo json_encode($lang);
?>

只需从jQuery脚本中更改这几行:

url: "languageDetector.php",
dataType: 'json',
success: function(language) {
    nowDoSomethingWithIt(language);
}

导航器。用于IE的userLanguage

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

如果你控制了一个后端并且使用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)

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

对于这个问题,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');