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.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
Here's the solution:
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 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.
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
Last night I did this tiny gem: accept_language
It can be integrated in a Rails app like that:
I hope it can help.