检测 Rails 中的浏览器语言
如何检测浏览器中设置的用户语言(在 RoR 中)?我将有一个用户可以随时使用的选择框,但我想默认为他们的浏览器语言。
How do you detect the users language (in RoR) as set in the browser? I will have a select box the user can use at anytime, but I'd like to default to whatever their browser language is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个老问题,但我在寻找答案时遇到了它,唯一可用的答案是没有任何上下文的链接。根据我随后的挖掘,这里有更深入的内容。
访问 Accept-Language 标头
查询相关控制器中的
request
对象:不过,这是简单的部分。为了在 Rails 中充分利用它,您需要解析该字符串(或者可能是
nil
?)。侧边栏:Accept-* 标头入门
因此,查看上面的示例字符串,某些语言具有“q 值” - 相对质量因子,介于 0 和 1 之间。较高的 q 值意味着客户端更喜欢该语言。缺少 q 值实际上是最高的 q 值 - 隐式
1.0
(与上面字符串中的en-AU
一样)。不过,有一点复杂——浏览器可能会向您发送 q 值为 0 的语言——我认为这意味着如果可能的话,您应该拒绝这些语言。解析 Accept-Language 标头
记住这一点,我研究过几种不同但相似的方法,用于将这样的字符串解析为语言列表,并按它们的 q 值排序。使用简单的 Ruby:
或者如果您精通正则表达式,您可以实现与上面相同的效果,并且可能同时改进我的正则表达式:
无论哪种方式,您都可以根据需要跟进,例如:
我开始了我通过查看这个要点进行解析,但最终对其进行了相当大的修改。正则表达式尤其抛弃了完美的辅助语言环境,例如,
en-AU
变成了en
,zh-TW
变成了zh
>。我试图通过对该正则表达式的修改来纠正这个问题。This is an old question, but I came across it as a soul in search of answers and the only available answer was a link without any context. So here's a bit more depth, based on my subsequent digging.
Accessing the Accept-Language header
Query the
request
object in the relevant controller:That's the easy part though. To make good use of it in Rails, you'll need to parse that string (or maybe it's
nil
?).Sidebar: A primer on Accept-* headers
So, looking at the example string above, some languages have "q-values" - the relative quality factor, between 0 and 1. Higher q-values mean that language is preferred by the client. Lack of a q-value is really the highest q-value - an implicit
1.0
(as withen-AU
in the above string). A slight complication though - the browser may send you languages with q-values that are, say, 0 - and I gather this means you should reject those languages if possible.Parsing the Accept-Language header
Bearing that in mind, here's a couple of different yet similar approaches I've looked at for parsing such a string into a list of languages, ordered by their q-values. With straightforward Ruby:
Or if you're proficient at regular expressions, you can achieve the same as above, and probably improve upon my butchered regex at the same time:
Either way, you can follow up as needed with something like:
I started out my parsing by looking at this gist, but ended up modifying it fairly significantly. The regex especially was throwing away perfectly good secondary locales, e.g.
en-AU
becameen
,zh-TW
becamezh
. I've attempted to rectify this with my modifications to that regex.解决方案如下:
Here's the solution:
昨晚我做了这个小宝石: accept_language
它可以集成到 Rails 应用程序中,如下所示:
我希望它能有所帮助。
Last night I did this tiny gem: accept_language
It can be integrated in a Rails app like that:
I hope it can help.
这是一个非常老的问题,但我想提一下 Rails 指南有关于如何检测用户浏览器语言的描述。
下面的代码基于 Ruby on Rails 指南中的解决方案并进行了改进:
:
有关的更多信息区域设置检测可以在 Ruby on Rails 指南中找到:
https://guides.rubyonrails.org/i18n.html#choosing-隐含区域设置
This is a really old question, but I like to mention that Rails guide has a description of how to detect user browser language.
Below code is based on the solution from Ruby on Rails guide with improvements:
code:
More information about locale detection can be found in Ruby on Rails guide:
https://guides.rubyonrails.org/i18n.html#choosing-an-implied-locale
这是一个经过充分测试的 Ruby gem,它完全可以满足您的需求: https://github.com/ioquatix/http -accept
它对各种输入具有 100% 的测试覆盖率。它按照相关 RFC 指定的顺序返回语言。
此外,如果您尝试将用户语言与特定区域设置中可用的内容相匹配,则可以执行以下操作:
上例中的desired_localizations是available_localizations的子集,根据用户首选项、可用本地化和RFC 建议。
Here is a well tested Ruby gem which does exactly what you want: https://github.com/ioquatix/http-accept
It has 100% test coverage on a wide range of inputs. It returns the languages in the order specified by the relevant RFCs.
In addition, if you are trying to match user language to content which is available in a specific set of locales, you can do the following:
The desired_localizations in the example above is a subset of available_localizations, according to user preference, available localisations, and RFC recommendations.