A gem which helps you detect the users preferred language, as sent by the "Accept-Language" HTTP header.
The algorithm is based on RFC 2616, with one exception: when a user requests "en-US" and "en" is an available language, "en" is deemed compatible with "en-US". The RFC specifies that the requested language must either exactly match the available language or must exactly match a prefix of the available language. This means that when the user requests "en" and "en-US" is available, "en-US" would be compatible, but not the other way around. This is usually not what you're looking for.
Since version 2.0, this gem is Rack middleware.
The http_accept_language method is available in any controller:
classSomeController < ApplicationControllerdefsome_actionhttp_accept_language.user_preferred_languages# => %w(nl-NL nl-BE nl en-US en)available=%w(enen-USnl-BE)http_accept_language.preferred_language_from(available)# => 'nl-BE'http_accept_language.user_preferred_languages# => %w(en-GB)available=%w(en-US)http_accept_language.compatible_language_from(available)# => 'en-US'http_accept_language.user_preferred_languages# => %w(nl-NL nl-BE nl en-US en)available=%w(ennlde)# This could be from I18n.available_localeshttp_accept_language.preferred_language_from(available)# => 'nl'endendYou can easily set the locale used for i18n in a before-filter:
classSomeController < ApplicationControllerbefore_filter:set_localeprivatedefset_localeI18n.locale=http_accept_language.compatible_language_from(I18n.available_locales)endendIf you want to enable this behavior by default in your controllers, you can just include the provided concern:
classApplicationController < ActionController::BaseincludeHttpAcceptLanguage::AutoLocale#...endThen set available locales in config/application.rb:
config.i18n.available_locales=%w(ennldefr)To use the middleware in any Rack application, simply add the middleware:
require'http_accept_language'useHttpAcceptLanguage::MiddlewarerunYourAwesomeAppThen you can access it from env:
classYourAwesomeAppdefinitialize(app)@app=appenddefcall(env)available=%w(enen-USnl-BE)language=env.http_accept_language.preferred_language_from(available)[200,{},["Oh, you speak #{language}!"]]endend- user_preferred_languages: Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE, sanitized and all.
- preferred_language_from(languages): Finds the locale specifically requested by the browser
- compatible_language_from(languages): Returns the first of the user_preferred_languages that is compatible with the available locales. Ignores region.
- sanitize_available_locales(languages): Returns a supplied list of available locals without any extra application info that may be attached to the locale for storage in the application.
- language_region_compatible_from(languages): Returns the first of the user preferred languages that is also found in available languages. Finds best fit by matching on primary language first and secondarily on region. If no matching region is found, return the first language in the group matching that primary language.
Install the gem http_accept_language
Add the gem to your Gemfile:
gem'http_accept_language'Run bundle install to install it.
Released under the MIT license