用于检测浏览器语言偏好的 JavaScript [英] JavaScript for detecting browser language preference

查看:26
本文介绍了用于检测浏览器语言偏好的 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 JavaScript 检测浏览器语言偏好.

I have been trying to detect the browser language preference using JavaScript.

如果我在 IE 中的 Tools>Internet Options>General>Languages 中设置浏览器语言,我如何使用 JavaScript 读取该值?

If I set the browser language in IE in Tools>Internet Options>General>Languages, how do I read this value using JavaScript?

Firefox 也有同样的问题.我无法使用 navigator.language 检测 tools>options>content>languages 的设置.

Same problem for Firefox. I'm not able to detect the setting for tools>options>content>languages using navigator.language.

使用 navigator.userLanguage ,它检测通过Start>ControlPanel>RegionalandLanguageOptions>Regional Options 标签.

Using navigator.userLanguage , it detects the setting done thru Start>ControlPanel>RegionalandLanguageOptions>Regional Options tab.

我已经使用 navigator.browserLanguagenavigator.systemLanguage 进行了测试,但都没有返回第一个设置的值(Tools>InternetOptions>General>Languages代码>)

I have tested with navigator.browserLanguage and navigator.systemLanguage but neither returns the value for the first setting(Tools>InternetOptions>General>Languages)

我找到了一个链接详细讨论了这一点,但问题仍未得到解答:(

I found a link which discusses this in detail, but the question remains unanswered :(

推荐答案

我认为这里的主要问题是浏览器设置实际上并不影响通过 javascript 获取的 navigator.language 属性.

I think the main problem here is that the browser settings don't actually affect the navigator.language property that is obtained via javascript.

它们确实影响的是 HTTP 'Accept-Language' 标头,但该值似乎根本无法通过 javascript 获得.(可能是为什么@anddoutoi 说他找不到不涉及服务器端的参考.)

What they do affect is the HTTP 'Accept-Language' header, but it appears this value is not available through javascript at all. (Probably why @anddoutoi states he can't find a reference for it that doesn't involve server side.)

我编写了一个解决方法:我在 http://ajaxhttpheaders.appspot.com 上敲了一个谷歌应用引擎脚本 将通过 JSONP 返回 HTTP 请求标头.

I have coded a workaround: I've knocked up a google app engine script at http://ajaxhttpheaders.appspot.com that will return you the HTTP request headers via JSONP.

(注意:如果您没有可用的后端可以为您执行此操作,则仅可使用此技巧.通常,除非您有,否则不应调用页面中的第三方托管 javascript 文件对主机的高度信任.)

(Note: this is a hack only to be used if you do not have a back end available that can do this for you. In general you should not be making calls to third party hosted javascript files in your pages unless you have a very high level of trust in the host.)

我打算将其永久保留,因此请随时在您的代码中使用它.

I intend to leave it there in perpetuity so feel free to use it in your code.

这是一些示例代码(在 jQuery 中),用于说明如何使用它

Here's some example code (in jQuery) for how you might use it

$.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

I have written a small jQuery plugin on github that wraps this functionality: https://github.com/dansingerman/jQuery-Browser-Language

编辑 2:这里要求的是在 AppEngine 上运行的代码(真的超级简单):

Edit 2: As requested here is the code that is running on AppEngine (super trivial really):

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

Have open sourced the app engine code here: https://github.com/dansingerman/app-engine-headers

这篇关于用于检测浏览器语言偏好的 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆