Detect browser language in Rails

后端 未结 5 672
一个人的身影
一个人的身影 2020-12-30 01:04

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

5条回答
  •  轮回少年
    2020-12-30 01:17

    Here's the solution:

    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...

    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)...

    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...

提交回复
热议问题