如何在浏览器中确定用户的语言环境? [英] How to determine user's locale within browser?

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

问题描述

我有一个网站 (Flash) 本地化为十几种语言,我想根据用户的浏览器设置自动定义默认值,以最大限度地减少访问内容的步骤.

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)? Especially to reduce all possible languages to a smaller number (the translations I have) in a smart way.

我可以在多大程度上相信这样的解决方案?

To which point can I trust such a solution?

还有其他解决方法或建议吗?

Any other workarounds or suggestions?

推荐答案

正确的做法是看 HTTP Accept-Language 标头发送到服务器.这包含用户已将其浏览器配置为首选语言的有序加权列表.

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,它告诉您安装了哪个本地化版本的 Web 浏览器.这不一定与用户的首选语言相同.在 IE 上,您会获得 systemLanguage(操作系统安装的语言)、browserLanguage(与 language 相同)和 userLanguage(用户配置的操作系统区域),这些都同样没有帮助.

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.

如果我必须在这些属性之间进行选择,我会首先嗅探 userLanguage,然后回退到 language,然后仅在此之后(如果那些不匹配任何可用语言)查看 browserLanguage 和最后 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';

然后您可以在 HTML 中包含一个指向该外部服务的 <script src>,并使用 JavaScript 来解析语言标头.不过,我不知道有任何现有的库代码可以执行此操作,因为 Accept-Language 解析几乎总是在服务器端完成.

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 中,但这可能会使不支持 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天全站免登陆