Sha256: 690c6b729e334aecba0aa5f362a31e357956eaad1d5f2d77ab6d1e414ab307fd

Contents?: true

Size: 623 Bytes

Versions: 2

Compression:

Stored size: 623 Bytes

Contents

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

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

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locale_setter-0.4.0 lib/locale_setter/matcher.rb
locale_setter-0.3.0 lib/locale_setter/matcher.rb