Project

al

0.0
No commit activity in last 3 years
No release in over 3 years
Al picks the best match from an `Accept-Language` header given a set of available languages.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.2.2, ~> 1.2
 Project Readme

Al

Al stands for Accept-Language

What does Al do?

Al picks the best match from an Accept-Language header given a set of available languages.

  • Al respects quality values
  • Al respects language tag prefixes
  • Al ignores character case
  • Al ignores spaces (but not other whitespace)
  • Al makes an effort to put up with invalid Accept-Language headers
  • Al is fast!

Usage

require "al"

# Define the available languages and their values
al          = Al.new("en", :greeting => "Hello")
al["en-GB"] = { :greeting => "How do you do?" }
al["en-US"] = { :greeting => "Howdy partner!" }

# Let Al make a sensible match
lang, match = al.pick("de-DE;q=0.5, de-AT;q=0.4, en-AU")
puts lang # => "en"
puts match # => { :greeting => "Hello" }

lang, match = al.pick("en-AU;q=0.4, en-US;q=0.5")
puts lang # => "en-us"
puts match # => { :greeting => "Howdy partner!" }

lang, match = al.pick("da")
puts lang # => "en"
puts match # => { :greeting => "Hello" }

The default language is optional. Without a default language Al returns [nil, nil] if it can't find a match.

Al and RFC 2616

RFC 2616 says a more specific language tag than the requested language tag can be used if an exactly matching language tag is not available. The reverse is illegal. This means that en is not a match when en-GB is requested, but en-GB is a match when en is requested.

Al thinks this is silly.

The #pick method will ignore this rule and treat en as a valid match when en-GB is requested. It otherwise follows RFC 2616. A #strict_pick method is provided which follows RFC 2616 exactly.

Installation

gem install al

Tests

To be able to run the tests install Al with development dependencies (gem install al --development). Then run rake test.

License

See the LICENSE file.