在Rails中检测浏览器语言 [英] Detect browser language in Rails

查看:74
本文介绍了在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.

推荐答案

这是解决方案:

2.6从客户端提供的信息中设置区域设置

在特定情况下,从客户端提供的信息(而不是从URL)设置区域设置是有意义的.例如,此信息可能来自用户的首选语言(在其浏览器中设置),可以基于从其IP推断出的用户地理位置,或者用户可以通过在应用程序界面中选择语言环境并保存来提供信息.到他们的个人资料.这种方法更适合基于Web的应用程序或服务,而不适合网站-请参阅上面有关 session cookies 和RESTful体系结构的框...

2.6 Setting the Locale from the Client Supplied Information

In specific cases, it would make sense to set the locale from client-supplied information, i.e. not from the URL. This information may come for example from the users’ preferred language (set in their browser), can be based on the users’ geographical location inferred from their IP, or users can provide it simply by choosing the locale in your application interface and saving it to their profile. This approach is more suitable for web-based applications or services, not for websites — see the box about sessions, cookies and RESTful architecture above...

客户端提供的信息的一种来源是接受语言" HTTP标头.人们可以将此设置为他们的浏览器或其他客户端(例如 curl )...

One source of client supplied information would be an Accept-Language HTTP header. People may set this in their browser or other clients (such as curl)...

从客户端信息中选择语言环境的另一种方法是使用数据库将客户端IP映射到该区域,例如

Another way of choosing the locale from client information would be to use a database for mapping the client IP to the region, such as GeoIP Lite Country...

您还可以为您的应用程序用户提供在您的应用程序界面中设置(或可能超越)语言环境的方法.

You can also provide users of your application with means to set (and possibly over-ride) the locale in your application interface, as well...

这篇关于在Rails中检测浏览器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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