最好能确定浏览器用户的语言环境的方式 [英] Best way to determine user's locale within browser

查看:184
本文介绍了最好能确定浏览器用户的语言环境的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经本地化为多种语言十几个网站(闪光),我想自动定义取决于用户的浏览器设置,以尽量减少步骤访问内容的默认值。

I have a website (Flash) localized into a dozen of languages and I want to auto-define a default value depending on the user's browser settings in order to minimize the steps to access the content.

仅供参考,我不能使用服务器脚本由于代理的限制,所以我想的JavaScript或ActionScript将是适当的解决问题。

FYI, I cannot use server-scripts due to proxy restrictions, so I guess JavaScript or ActionScript would be appropriate to solve the problem.

问题:


  1. 什么是为猜测用户的语言环境的最佳方法?

  1. What would be the best method to 'guess' the user's locale ?

是否有任何现有的简单类/函数,可以帮助我(没有复杂的本地化包)?特别对一个聪明的方式打破所有可能的语言到一个较小的数字(翻译我有)。

Are there any existing simple classes/functions that could help me out (no complex localization bundles) ? Specially to break down all the possible languages into a smaller number (the translations I have) on a smart way .

要我能相信这一点这样的解决方案?

To which point can I trust such a solution ?

任何其他的解决办法或建议?

Any other workarounds or suggestions ?

在此先感谢!

推荐答案

的正确方法是看HTTP的接受语言的报头。这包含的用户配置他们的浏览器以preFER语言有序,加权列表。

The proper way is to look at the HTTP Accept-Language header sent to the server. This contains the ordered, weighted list of languages the user has configured their browser to prefer.

不幸的是这头不适用于Javascript中读取;你得到的是 navigator.language ,它告诉你安装的是什么浏览器的本地化版本。这不一定是一回事用户的preferred语言(S)。在你的IE,而不是让 SYSTEMLANGUAGE (OS安装的语言),浏览器语言(等同于语言)和用户语言(用户配置的OS区),这些都是同样于事无补。

Unfortunately this header is not available for reading inside JavaScript; all you get is navigator.language, which tells you what localised version of the web browser was installed. This is not necessarily the same thing as the user's preferred language(s). On IE you instead get systemLanguage (OS installed language), browserLanguage (same as language) and userLanguage (user configured OS region), which are all similarly unhelpful.

如果我有这些属性之间做出选择,我会嗅探用户语言第一,回落至语言只有经过(如果这些不匹配任何可用的语言)寻找浏览器语言最后 SYSTEMLANGUAGE

If I had to choose between those properties, I'd sniff for userLanguage first, falling back to language and only after that (if those didn't match any available language) looking at browserLanguage and finally systemLanguage.

如果你可以把服务器端脚本别的地方上只是读取Accept-Language头吐出回来了作为一个JavaScript的字符串,如在标题中值文件网:

If you can put a server-side script somewhere else on the net that simply reads the Accept-Language header and spits it back out as a JavaScript file with the header value in the string, eg.:

var acceptLanguage= 'en-gb,en;q=0.7,de;q=0.3';

,那么你可以包括<脚本的src>在HTML中的外部服务指指点点,使用JavaScript解析语言头。我不知道任何现有的库code要做到这一点,虽然,因为接受语言解析几乎总是在服务器端完成的。

then you could include a <script src> pointing at that external service in the HTML, and use JavaScript to parse the language header. I don't know of any existing library code to do this, though, since Accept-Language parsing is almost always done on the server side.

,你肯定需要一个用户覆盖,因为它总是猜错了一些人。通常,最简单的方法就摆在URL中的语言设置(例如:http://www.example.com/en/site VS HTTP://www.example.com/de/site),并让用户点击两者之间的联系。有时候,你想这两个语言版本单个URL,在这种情况下,你必须在cookie中存储的设置,但是这可能会混淆用户代理与饼干和搜索引擎的支持。

Whatever you end up doing, you certainly need a user override because it will always guess wrong for some people. Often it's easiest to put the language setting in the URL (eg. http​://www.example.com/en/site vs http​://www.example.com/de/site), and let the user click links between the two. Sometimes you do want a single URL for both language versions, in which case you have to store the setting in cookies, but this may confuse user agents with no support for cookies and search engines.

这篇关于最好能确定浏览器用户的语言环境的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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