Class: AcceptLanguage::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/accept_language/matcher.rb

Overview

A utility class that provides functionality to match the Accept-Language header value against the languages supported by your application. This helps in identifying the most suitable languages to present to the user based on their preferences.

Examples:

Matcher.new("da" => 1.0, "en-GB" => 0.8, "en" => 0.7).call(:ug, :kk, :ru, :en) # => :en
Matcher.new("da" => 1.0, "en-GB" => 0.8, "en" => 0.7).call(:fr, :en, :"en-GB") # => :"en-GB"

Constant Summary collapse

WILDCARD =
"*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**languages_range) ⇒ Matcher

Initialize a new Matcher object with the languages_range parameter representing the user’s preferred languages and their respective quality values.

Parameters:

  • languages_range (Hash<String, BigDecimal>)

    A hash where keys represent languages and values are the quality of preference for each language. A value of zero means the language is not acceptable.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/accept_language/matcher.rb', line 21

def initialize(**languages_range)
  @excluded_langtags = ::Set[]
  langtags = []

  languages_range.select do |langtag, quality|
    if quality.zero?
      @excluded_langtags << langtag unless wildcard?(langtag)
    else
      level = (quality * 1_000).to_i
      langtags[level] = langtag
    end
  end

  @preferred_langtags = langtags.compact.reverse
end

Instance Attribute Details

#excluded_langtagsObject (readonly)

Returns the value of attribute excluded_langtags.



14
15
16
# File 'lib/accept_language/matcher.rb', line 14

def excluded_langtags
  @excluded_langtags
end

#preferred_langtagsObject (readonly)

Returns the value of attribute preferred_langtags.



14
15
16
# File 'lib/accept_language/matcher.rb', line 14

def preferred_langtags
  @preferred_langtags
end

Instance Method Details

#call(*available_langtags) ⇒ String, ...

Matches the user’s preferred languages against the available languages of your application. It prioritizes higher quality values and returns the most suitable match.

Examples:

When Uyghur, Kazakh, Russian and English languages are available.

call(:ug, :kk, :ru, :en)

Parameters:

  • available_langtags (Array<String, Symbol>)

    An array representing the languages available in your application.

Returns:

  • (String, Symbol, nil)

    The language that best matches the user’s preferences, or nil if there is no match.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/accept_language/matcher.rb', line 46

def call(*available_langtags)
  available_langtags = drop_unacceptable(*available_langtags)

  preferred_langtags.each do |preferred_tag|
    if wildcard?(preferred_tag)
      langtag = any_other_langtag(*available_langtags)
      return langtag unless langtag.nil?
    else
      available_langtags.each do |available_langtag|
        return available_langtag if available_langtag.match?(/\A#{preferred_tag}/i)
      end
    end
  end

  nil
end