module TwitterCldr::Shared::LanguageCodes

Constants

LANGUAGE_CODES_DUMP_PATH
NAME_STANDARD
VALID_STANDARDS

Public Class Methods

convert(code, from_and_to = {}) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 39
def convert(code, from_and_to = {})
  from, to = extract_from_and_to_options(from_and_to)

  from.each do |from_std|
    to.each do |to_std|
      if result = (resource[from_std] || {}).fetch(code.to_sym, {})[to_std]
        return result
      end
    end
  end

  nil
end
from_language(language, standard) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 53
def from_language(language, standard)
  convert(language, from: NAME_STANDARD, to: standard)
end
languages() click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 26
def languages
  resource[NAME_STANDARD].keys
end
standards_for(code, standard) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 61
def standards_for(code, standard)
  standards = validate_standard(standard)

  standards.each do |std|
    if found = resource[std][code.to_sym]
      # exclude fake NAME_STANDARD standard
      return (found.keys & VALID_STANDARDS.keys) - [NAME_STANDARD]
    end
  end

  []
end
standards_for_language(language) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 74
def standards_for_language(language)
  standards_for(language, NAME_STANDARD)
end
to_language(code, standard) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 57
def to_language(code, standard)
  convert(code, from: standard, to: NAME_STANDARD).to_s
end
valid_code?(code, standard) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 34
def valid_code?(code, standard)
  standards = validate_standard(standard)
  standards.any? { |std| (resource[std] || {}).has_key?(code.to_sym) }
end
valid_standard?(standard) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 30
def valid_standard?(standard)
  VALID_STANDARDS.include?(standard.to_sym)
end

Private Class Methods

extract_from_and_to_options(from_and_to) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 84
def extract_from_and_to_options(from_and_to)
  TwitterCldr::Utils.deep_symbolize_keys(from_and_to).values_at(:from, :to).map do |standard|
    raise ArgumentError, 'options :from and :to are required' if standard.nil?
    validate_standard(standard)
  end
end
resource() click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 80
def resource
  @resource ||= File.open(LANGUAGE_CODES_DUMP_PATH) { |file| Marshal.load(file.read) }
end
validate_standard(standard) click to toggle source
# File lib/twitter_cldr/shared/language_codes.rb, line 91
def validate_standard(standard)
  raise ArgumentError, "standard can't be nil" if standard.nil?
  raise ArgumentError, "#{standard.inspect} is not a valid standard name" unless valid_standard?(standard)

  VALID_STANDARDS[standard.to_sym]
end