Module: Rack::Acceptable::Languages

Defined in:
lib/rack/acceptable/mixin/languages.rb

Instance Method Summary collapse

Instance Method Details

#accept_language?(tag) ⇒ Boolean

Checks if the Language-Tag (as a String or Rack::Acceptable::LanguageTag) passed acceptable. Works case-insensitively.

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/rack/acceptable/mixin/languages.rb', line 35

def accept_language?(tag)
  langtag = LanguageTag.parse(tag)
  acceptable_language_ranges.any? { |l,q| q > 0 && langtag.matched_by_extended_range?(l) }
rescue
  false
end

#acceptable_language_rangesObject

Returns

An Array with wildcards / Language-Tags (as Strings) and associated quality factors (qvalues). Default qvalue is 1.0.

Raises

ArgumentError

Syntax of the Accept-Language request-header is bad. For example, one of Language-Ranges is not in a RFC ‘Language-Range’ pattern, one of quality factors is malformed etc.

Notes

  • It uses Extended Language-Range pattern.

  • It does not perform ‘convenient transformations’ (downcasing of primary tags etc). In other words, it parses Accept-Language header in unpretentious manner.



22
23
24
25
26
27
28
29
# File 'lib/rack/acceptable/mixin/languages.rb', line 22

def acceptable_language_ranges
  Utils.parse_header(
    env[Const::ENV_HTTP_ACCEPT_LANGUAGE].to_s,
    Utils::HTTP_ACCEPT_LANGUAGE_REGEX)
rescue
  raise ArgumentError,
  "Malformed Accept-Language header: #{env[Const::ENV_HTTP_ACCEPT_LANGUAGE].inspect}"
end