Module: AcceptLanguage

Defined in:
lib/accept_language.rb,
lib/accept_language/parser.rb,
lib/accept_language/matcher.rb

Overview

This module provides a tiny library for parsing the Accept-Language header as specified in RFC 2616. It transforms the Accept-Language header field into a language range, providing a flexible way to determine user’s language preferences and match them with the available languages in your application.

Examples:

AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7")
# => #<AcceptLanguage::Parser:0x00007 @languages_range={"da"=>1.0, "en-GB"=>0.8, "en"=>0.7}>

See Also:

Defined Under Namespace

Classes: Matcher, Parser

Class Method Summary collapse

Class Method Details

.parse(field) ⇒ Parser

Parses an Accept-Language header field value into a Parser object, which can then be used to match user’s preferred languages against the languages your application supports. This method accepts a string argument in the format as described in RFC 2616 Section 14.4, and returns a Parser object which responds to the #match method.

Examples:

AcceptLanguage.parse("da, en-GB;q=0.8, en;q=0.7")
# => #<AcceptLanguage::Parser:0x00007 @languages_range={"da"=>1.0, "en-GB"=>0.8, "en"=>0.7}>

Parameters:

  • field (String)

    the Accept-Language header field value.

Returns:

  • (Parser)

    a Parser object that responds to #match method.



25
26
27
# File 'lib/accept_language.rb', line 25

def self.parse(field)
  Parser.new(field)
end