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)