用于检测浏览器语言偏好的JavaScript

本文翻译自:JavaScript for detecting browser language preference [duplicate]

I have been trying to detect the browser language preference using JavaScript. 我一直在尝试使用JavaScript检测浏览器的语言偏好。

If I set the browser language in IE in Tools>Internet Options>General>Languages , how do I read this value using JavaScript? 如果我在IE中的“ Tools>Internet Options>General>Languages设置了浏览器语言,如何使用JavaScript读取此值?

Same problem for Firefox. Firefox也有同样的问题。 I'm not able to detect the setting for tools>options>content>languages using navigator.language . 我无法使用navigator.language检测到tools>options>content>languages的设置。

Using navigator.userLanguage , it detects the setting done thru Start>ControlPanel>RegionalandLanguageOptions>Regional Options tab. 使用navigator.userLanguage ,它可以检测到通过“ Start>ControlPanel>RegionalandLanguageOptions>Regional Options卡完成的设置。

I have tested with navigator.browserLanguage and navigator.systemLanguage but neither returns the value for the first setting( Tools>InternetOptions>General>Languages ) 我已经使用navigator.browserLanguagenavigator.systemLanguage进行了测试,但均未返回第一个设置的值(“ Tools>InternetOptions>General>Languages )。

I found a link which discusses this in detail, but the question remains unanswered :( 我找到了一个详细讨论此问题的链接 ,但问题仍未得到解答:(


#1楼

参考:https://stackoom.com/question/4NQ3/用于检测浏览器语言偏好的JavaScript


#2楼

navigator.userLanguage for IE IE的navigator.userLanguage

window.navigator.language for firefox/opera/safari 用于Firefox / Opera / Safari的window.navigator.language


#3楼

I can't find a single reference that state that it's possible without involving the serverside. 我找不到一个单独的引用,指出在不涉及服务器端的情况下是可能的。

MSDN on: MSDN上:

From browserLanguage: 从浏览器语言:

In Microsoft Internet Explorer 4.0 and earlier, the browserLanguage property reflects the language of the installed browser's user interface. 在Microsoft Internet Explorer 4.0和更早版本中,browserLanguage属性反映了已安装浏览器用户界面的语言。 For example, if you install a Japanese version of Windows Internet Explorer on an English operating system, browserLanguage would be ja. 例如,如果在英语操作系统上安装日语版本的Windows Internet Explorer,则browserLanguage将为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. 但是,在Internet Explorer 5和更高版本中,无论安装了Internet Explorer的语言版本如何,browserLanguage属性都反映操作系统的语言。 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. 但是,如果安装了Microsoft Windows 2000 MultiLanguage版本,则browserLanguage属性指示在操作系统的当前菜单和对话框中设置的语言,可以在“控制面板”的“区域选项”中找到该语言。 For example, if you install a Japanese version of Internet Explorer 5 on an English (United Kingdom) operating system, browserLanguage would be en-gb. 例如,如果在英语(英国)操作系统上安装日语版的Internet Explorer 5,则browserLanguage将为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. 如果您安装Windows 2000 MultiLanguage版本并将菜单和对话框的语言设置为法语,则即使您具有日语版的Internet Explorer,browserLanguage也将是fr。

Note This property does not indicate the language or languages set by the user in Language Preferences, located in the Internet Options dialog box. 注意此属性不会指示用户在“ Internet选项”对话框中的“语言首选项”中设置的一种或多种语言。

Furthermore, it looks like browserLanguage is deprecated cause IE8 doesn't list it 此外,似乎不推荐使用browserLanguage ,因为IE8没有列出它


#4楼

For what it's worth, Wikimedia's Universal Language Selector library has hooks for doing this: https://www.mediawiki.org/wiki/Extension:UniversalLanguageSelector 对于它的价值,Wikimedia的通用语言选择器库具有执行此操作的挂钩: https : //www.mediawiki.org/wiki/Extension :UniversalLanguageSelector

See the function getFrequentLanguageList in resources/js/ext.uls.init.js . 请参阅resources / js / ext.uls.init.js中的getFrequentLanguageList函数。 Direct link: https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/extensions/UniversalLanguageSelector.git;a=blob;f=resources/js/ext.uls.init.js;hb=HEAD 直接链接: https : //gerrit.wikimedia.org/r/gitweb?p= mediawiki/extensions/ UniversalLanguageSelector.git;a=blob;f= resources/js/ ext.uls.init.js;hb=HEAD

It still depends on the server, or more specifically, the MediaWiki API. 它仍然取决于服务器,或更具体地说,取决于MediaWiki API。 The reason I'm showing it is that it may provide a good example of getting all the useful information about the user's language: browser language, Accept-Language, geolocation (with getting country/language info from the CLDR), and of course, user's own site preferences. 我显示它的原因是,它可能是获取有关用户语言的所有有用信息的一个很好的例子:浏览器语言,接受语言,地理位置(以及从CLDR获取国家/语言信息),当然,用户自己的网站偏好设置。


#5楼

There is no decent way to get that setting, at least not something browser independent. 没有合适的方法来获得该设置,至少不是与浏览器无关。

But the server has that info, because it is part of the HTTP request header (the Accept-Language field, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 ) 但是服务器具有该信息,因为它是HTTP请求标头的一部分(Accept-Language字段,请参见http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4

So the only reliable way is to get an answer back from the server. 因此,唯一可靠的方法是从服务器获取答案。 You will need something that runs on the server (like .asp, .jsp, .php, CGI) and that "thing" can return that info. 您将需要在服务器上运行的内容(例如.asp,.jsp,.php,CGI),并且“内容”可以返回该信息。 Good examples here: http://www.developershome.com/wap/detection/detection.asp?page=readHeader 此处的好例子: http : //www.developershome.com/wap/detection/detection.asp?page=readHeader


#6楼

Dan Singerman's answer has an issue that the header fetched has to be used right away, due to the asynchronous nature of jQuery's ajax. 丹·辛格曼(Dan Singerman)的答案有一个问题,由于jQuery的ajax具有异步特性,因此必须立即使用提取的标头。 However, with his google app server, I wrote the following, such that the header is set as part of the initial set up and can be used at later time. 但是,我使用他的google app服务器编写了以下内容,以便将标头设置为初始设置的一部分,并可以在以后使用。

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值