public
Description: Ruby on Rails plugin. Fishes out the Accept-Language header into an array.
Homepage: http://rails-i18n.org/
Clone URL: git://github.com/iain/http_accept_language.git
iain (author)
Thu Mar 12 15:09:49 -0700 2009
commit  28c245a1217196503f742725e61e75e7df490863
tree    065eea110375108370a8698632addba721454e13
parent  159d2446c7a908afbe6b18471ef9be9a94b454a7
name age message
file MIT-LICENSE Wed Jul 30 15:33:26 -0700 2008 first commit [iain]
file README.rdoc Loading commit data...
file Rakefile Sat Aug 02 16:39:00 -0700 2008 corrects case to xx-XX and checks for form [iain]
file init.rb
directory lib/
directory tasks/ Sat Aug 02 16:39:00 -0700 2008 corrects case to xx-XX and checks for form [iain]
directory test/
README.rdoc

HttpAcceptLanguage

A small effort in making a plugin which helps you detect the users preferred language, as sent by the HTTP header.

Features

  • Splits the http-header into languages specified by the user
  • Returns empty array if header is illformed.
  • Corrects case to xx-XX
  • Sorted by priority given, as much as possible.
  • Gives you the most important language
  • Gives compatible languages

See also: www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Example

  class SomeController < ApplicationController
    def some_action

      request.user_preferred_languages
      # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]

      available = %w{en en-US nl-BE}
      request.preferred_language_from(available)
      # => 'nl-BE'

      request.user_preferred_language
      # => [ 'en-GB']
      available = %w{en-US}
      request.compatible_language_from(available)
      # => 'en-US'
    end
  end

Changenotes

2009-03-12: Rails 2.3 compatible

Copyright © 2008 Iain Hecker, released under the MIT license