我一直在尝试使用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

其他回答

我认为这里的主要问题是浏览器设置实际上并不影响导航器。通过javascript获得的语言属性。

它们所影响的是HTTP 'Accept-Language'报头,但似乎这个值在javascript中根本不可用。(可能是为什么@anddoutoi说他找不到一个不涉及服务器端的参考。)

我已经编写了一个解决方案:我已经在http://ajaxhttpheaders.appspot.com上创建了一个谷歌应用程序引擎脚本,它将通过JSONP返回HTTP请求头。

(注意:这是一个黑客,只有当你没有一个可用的后端,可以为你这样做。一般来说,你不应该在你的页面中调用第三方托管的javascript文件,除非你对主机有很高的信任。)

我打算把它永久地留在那里,所以在你的代码中可以自由地使用它。

下面是一些示例代码(使用jQuery),告诉您如何使用它

$.ajax({ 
    url: "http://ajaxhttpheaders.appspot.com", 
    dataType: 'jsonp', 
    success: function(headers) {
        language = headers['Accept-Language'];
        nowDoSomethingWithIt(language);
    }
});

希望有人觉得这有用。

编辑:我在github上写了一个小的jQuery插件,它包装了这个功能:https://github.com/dansingerman/jQuery-Browser-Language

编辑2:以下是在AppEngine上运行的代码(真的非常简单):

class MainPage(webapp.RequestHandler):
    def get(self):
        headers = self.request.headers
        callback = self.request.get('callback')

        if callback:
          self.response.headers['Content-Type'] = 'application/javascript'
          self.response.out.write(callback + "(")
          self.response.out.write(headers)
          self.response.out.write(")")
        else:
          self.response.headers['Content-Type'] = 'text/plain'
          self.response.out.write("I need a callback=")

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=False)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

Edit3:在这里开放应用引擎代码:https://github.com/dansingerman/app-engine-headers

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

Let lang = window.navigator.languages ?window.navigator。[0]: null; || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage; let shortLang = lang; if (shortLang.indexOf('-') !== -1) shortLang = shortLang.split('-')[0]; if (shortLang.indexOf('_') !== -1) shortLang = shortLang.split('_')[0]; console.log(朗,shortLang);

我只需要主组件来满足我的需要,但是您可以轻松地使用完整的字符串。适用于最新的Chrome, Firefox, Safari和IE10+。

2014年更新。

现在有一种方法可以使用导航器在Firefox和Chrome中获得Accept-Languages。语言(适用于Chrome >= 32和Firefox >= 32)

另外,导航器。这些年来Firefox的语言反映了最受欢迎的内容语言,而不是UI语言。但是由于这个概念还没有得到其他浏览器的支持,所以它并不是很有用。

所以,尽可能获得最受欢迎的内容语言,并使用UI语言作为后备:

navigator.languages
    ? navigator.languages[0]
    : (navigator.language || navigator.userLanguage)

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

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