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

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

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

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

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

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


当前回答

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+。

其他回答

没有合适的方法来获得该设置,至少不是独立于浏览器的设置。

但是服务器有这个信息,因为它是HTTP请求头的一部分(Accept-Language字段,参见http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4)

所以唯一可靠的方法是从服务器获取答案。你需要一些在服务器上运行的东西(比如。asp, .jsp, .php, CGI),这个“东西”可以返回这些信息。 好的例子如下:http://www.developershome.com/wap/detection/detection.asp?page=readHeader

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

对于正在寻找Java服务器解决方案的人

这里是RestEasy

@GET
@Path("/preference-language")
@Consumes({"application/json", "application/xml"})
@Produces({"application/json", "application/xml"})
public Response getUserLanguagePreference(@Context HttpHeaders headers) {
    return Response.status(200)
            .entity(headers.getAcceptableLanguages().get(0))
            .build();
}

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

2014年更新。

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

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

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

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