module I18nData

Constants

VERSION

Public Instance Methods

countries(language_code='EN') click to toggle source
# File lib/i18n_data.rb, line 17
def countries(language_code='EN')
  fetch :countries, language_code do
    data_provider.codes(:countries, normal_to_region_code(language_code.to_s.upcase))
  end
end
country_code(name) click to toggle source
# File lib/i18n_data.rb, line 23
def country_code(name)
  recognise_code(:countries, name)
end
data_provider() click to toggle source
# File lib/i18n_data.rb, line 31
def data_provider
  if @data_provider
    @data_provider
  else
    require 'i18n_data/file_data_provider'
    FileDataProvider
  end
end
data_provider=(provider) click to toggle source
# File lib/i18n_data.rb, line 40
def data_provider=(provider)
  @cache = nil
  @data_provider = provider
end
language_code(name) click to toggle source
# File lib/i18n_data.rb, line 27
def language_code(name)
  recognise_code(:languages, name)
end
languages(language_code='EN') click to toggle source
# File lib/i18n_data.rb, line 11
def languages(language_code='EN')
  fetch :languages, language_code do
    data_provider.codes(:languages, normal_to_region_code(language_code.to_s.upcase))
  end
end

Private Instance Methods

fetch(*args) { || ... } click to toggle source
# File lib/i18n_data.rb, line 47
def fetch(*args)
  @cache ||= {}
  if @cache.key?(args)
    @cache[args]
  else
    @cache[args] = yield
  end
end
normal_to_region_code(normal) click to toggle source

hardcode languages that do not have a default type e.g. zh does not exist, but zh_CN does

# File lib/i18n_data.rb, line 58
def normal_to_region_code(normal)
  {
    "ZH" => "zh_CN",
    "BN" => "bn_IN",
  }[normal] || normal
end
recognise_code(type, search) click to toggle source
# File lib/i18n_data.rb, line 65
def recognise_code(type, search)
  search = search.strip

  # common languages first <-> faster in majority of cases
  common_languages = ['EN','ES','FR','DE','ZH']
  langs = (common_languages + (languages.keys - common_languages))

  langs.each do |code|
    begin
      send(type, code).each do |code, name|
        # supports "Dutch" and "Dutch; Flemish", checks for inclusion first -> faster
        match_found = (name.include?(search) and name.split(';').map{|s| s.strip }.include?(search))
        return code if match_found
      end
    rescue NoTranslationAvailable
    end
  end
  nil
end