Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

javascript - 用于检测浏览器语言偏好的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 :(

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

  ask by Annibigi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

What they do affect is the HTTP 'Accept-Language' header, but it appears this value is not available through javascript at all.

(它们确实会影响HTTP'Accept-Language'标头,但看来此值根本无法通过javascript使用。)

(Probably why @anddoutoi states he can't find a reference for it that doesn't involve server side.)

((可能是为什么@anddoutoi指出他找不到不涉及服务器端的引用)。)

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.

(我已经编码了一种解决方法:我在http://ajaxhttpheaders.appspot.com上敲了一个Google App引擎脚本,该脚本将通过JSONP返回HTTP请求标头。)

(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.)

((请注意:这是一种hack,仅当您没有可用的后端可以使用时才能使用。通常,除非您的浏览器具有很高的访问权限,否则不应在页面中调用第三方托管的javascript文件对主机的信任级别。))

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

(我打算将其永久保留在那里,请随时在您的代码中使用它。)

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

(这是一些使用jQuery的示例代码)

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

Hope someone finds this useful.

(希望有人觉得这有用。)

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

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

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

(编辑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: Have open sourced the app engine code here: https://github.com/dansingerman/app-engine-headers

(Edit3:在此处开源了应用引擎代码: https//github.com/dansingerman/app-engine-headers)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...