用于检测浏览器语言首选项的JavaScript [英] JavaScript for detecting browser language preference

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

问题描述

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

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

如果我在IE中设置浏览器语言工具> Internet选项> ;常规>语言,如何使用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 检测工具>选项>内容>语言的设置。

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

使用 navigator.userLanguage ,它会检测通过
开始> ControlPanel>完成的设置; RegionalandLanguageOptions>区域选项标签。

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

我已经使用 navigator.browserLanguage 进行了测试, navigator.systemLanguage 但两者均未返回第一个设置的值(工具> InternetOptions>一般>语言

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 上的Google应用引擎脚本,它将通过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);
    }
});

希望有人觉得这很有用。

Hope someone finds this useful.

编辑:我在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()

编辑3:在这里开源应用引擎代码: 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天全站免登陆