accept_language

Parses the Accept-Language header from an HTTP request and produces a list of languages sorted by quality.


License
MIT
Install
gem install accept_language

Documentation

Accept Language

A tiny library for parsing the Accept-Language header from browsers (as defined in RFC 2616).

Status

Gem Version TravisCI Inline Docs

Installation

Add this line to your application's Gemfile:

gem 'accept_language'

And then execute:

$ bundle

Or install it yourself as:

$ gem install accept_language

Usage

It's intended to be used in a Web server that supports some level of internationalization (i18n), but can be used anytime an Accept-Language header string is available.

Examples:

AcceptLanguage.parse('da, en-gb;q=0.8, en;q=0.7')                     # => {:da=>1.0, :"en-gb"=>0.8, :en=>0.7}
AcceptLanguage.parse('fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5')  # => {:"fr-ch"=>1.0, :fr=>0.9, :en=>0.8, :de=>0.7, :*=>0.5}

In order to help facilitate better i18n, a method is provided to return the intersection of the languages the user prefers and the languages your application supports.

Examples:

AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :ja, :ro, :da) # => :da
AcceptLanguage.intersection('da, en-gb;q=0.8, en;q=0.7', :ja, :ro)      # => nil
AcceptLanguage.intersection('fr-CH', :fr, two_letter_truncate: false)   # => nil
AcceptLanguage.intersection('fr-CH', :fr, two_letter_truncate: true)    # => :fr
AcceptLanguage.intersection('de, zh;q=0.4, fr;q=0', :fr)                # => nil
AcceptLanguage.intersection('de, zh;q=0.4, *;q=0.5, fr;q=0', :fr)       # => nil
AcceptLanguage.intersection('de, zh;q=0.4, *;q=0.5, fr;q=0', :ar)       # => :ar

Rails integration example

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :best_locale_from_request!

  def best_locale_from_request!
    I18n.locale = best_locale_from_request
  end

  def best_locale_from_request
    return I18n.default_locale unless request.headers.key?('HTTP_ACCEPT_LANGUAGE')

    string = request.headers.fetch('HTTP_ACCEPT_LANGUAGE')
    locale = AcceptLanguage.intersection(string, I18n.default_locale, *I18n.available_locales)

    # If the server cannot serve any matching language,
    # it can theoretically send back a 406 (Not Acceptable) error code.
    # But, for a better user experience, this is rarely done and more
    # common way is to ignore the Accept-Language header in this case.
    return I18n.default_locale if locale.nil?

    locale
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cyril/accept_language.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Versioning

AcceptLanguage uses Semantic Versioning 2.0.0

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the AcceptLanguage project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.