Module: TwitterCldr::Shared::LanguageCodes

Defined in:
lib/twitter_cldr/shared/language_codes.rb

Constant Summary collapse

LANGUAGE_CODES_DUMP_PATH =
File.join(TwitterCldr::RESOURCES_DIR, 'shared', 'language_codes_table.dump')
NAME_STANDARD =

fake standard, mostly for internal use

:name
VALID_STANDARDS =
{
  bcp_47:    [:bcp_47, :bcp_47_alt],
  iso_639_1: [:iso_639_1],
  iso_639_2: [:iso_639_2, :iso_639_2_term],
  iso_639_3: [:iso_639_3]
}

Class Method Summary collapse

Class Method Details

.convert(code, from_and_to = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
# File 'lib/twitter_cldr/shared/language_codes.rb', line 53

def from_language(language, standard)
  convert(language, from: NAME_STANDARD, to: standard)
end

.languagesObject



26
27
28
# File 'lib/twitter_cldr/shared/language_codes.rb', line 26

def languages
  resource[NAME_STANDARD].keys
end

.standards_for(code, standard) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
# 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) ⇒ Object



57
58
59
# 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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# 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) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/twitter_cldr/shared/language_codes.rb', line 30

def valid_standard?(standard)
  VALID_STANDARDS.include?(standard.to_sym)
end