Module: LocaleSetter::Matcher

Defined in:
lib/locale_setter/matcher.rb

Class Method Summary collapse

Class Method Details

.generate_lookup_table(locales) ⇒ Object



23
24
25
26
27
# File 'lib/locale_setter/matcher.rb', line 23

def self.generate_lookup_table(locales)
  table = {}
  locales.each { |l| table[sanitize_one(l)] = l}
  table
end

.match(requested, against) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/locale_setter/matcher.rb', line 3

def self.match(requested, against)
  table = generate_lookup_table(against)
  matched = (sanitize(requested) & table.keys).first
  if matched
    table[matched].to_sym
  end
end

.sanitize(input) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/locale_setter/matcher.rb', line 11

def self.sanitize(input)
  if input.respond_to? :map
    input.map{|l| sanitize_one(l)}
  else
    [sanitize_one(input)]
  end
end

.sanitize_one(locale) ⇒ Object



19
20
21
# File 'lib/locale_setter/matcher.rb', line 19

def self.sanitize_one(locale)
  locale.to_s.downcase.strip
end