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
Here is a well tested Ruby gem which does exactly what you want: https://github.com/ioquatix/http-accept
languages = HTTP::Accept::Language.parse("da, en-gb;q=0.8, en;q=0.7")
expect(languages[0].locale).to be == "da"
expect(languages[1].locale).to be == "en-gb"
expect(languages[2].locale).to be == "en"
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:
available_localizations = HTTP::Accept::Languages::Locales.new(["en-nz", "en-us"])
# Given the languages that the user wants, and the localizations available, compute the set of desired localizations.
desired_localizations = available_localizations & languages
The desired_localizations in the example above is a subset of available_localizations, according to user preference, available localisations, and RFC recommendations.