Module: ISOCodes

Defined in:
lib/iso_codes.rb,
lib/iso_codes/version.rb

Defined Under Namespace

Classes: IndividualLanguage, Language, Macrolanguage, SpecialSituationLanguage

Constant Summary

ISO_639_3_VERSION =

Version of the ISO 639-3 code set supported.

'20130531'
ISO_639_3_MACROLANGUAGE_MAPPINGS_VERSION =

Version of the ISO 639-3 macrolanguage mappings supported.

'20130531'
VERSION =
'1.0.1'

Class Method Summary collapse

Class Method Details

.all_iso_639_3_codesObject

Returns an array containing all ISO 639-3 language codes.



87
88
89
# File 'lib/iso_codes.rb', line 87

def all_iso_639_3_codes
  @@iso_639_3.keys
end

.find_iso_639_3_language(code) ⇒ Object

Returns an object describing the language identified by the ISO 639-3 identifier +code+.



99
100
101
102
103
104
105
106
# File 'lib/iso_codes.rb', line 99

def find_iso_639_3_language(code)
  if @@iso_639_3.has_key?(code)
    klass, *rest = @@iso_639_3[code]
    klass.new(*rest)
  else
    nil
  end
end

.find_language(code) ⇒ Object

Returns an object describing the language identified by the language code +code+.



93
94
95
# File 'lib/iso_codes.rb', line 93

def find_language(code)
  find_iso_639_3_language(code)
end

.get_data_filename(filename) ⇒ Object



114
115
116
# File 'lib/iso_codes.rb', line 114

def get_data_filename(filename)
  File.join(DATA_PATH, filename)
end

.load_iso_639_3Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/iso_codes.rb', line 129

def load_iso_639_3
  data = {}

  read_data_file("iso-639-3.tab.txt", 8, "\t", true) do |args|
    identifier, part2b, part2t, part1, scope, language_type, ref_name, _ = args

    # Sanity checks
    raise ArgumentError, "missing identifier" if identifier.nil?
    raise ArgumentError, "missing reference name" if ref_name.nil?

    klass =
      case scope
      when 'I'
        IndividualLanguage
      when 'M'
        Macrolanguage
      when 'S'
        SpecialSituationLanguage
      else
        raise ArgumentError, "invalid scope"
      end

    language_type =
      case language_type
      when 'L'
        :living
      when 'E'
        :extinct
      when 'A'
        :ancient
      when 'H'
        :historic
      when 'C'
        :constructed
      when 'S'
        :special
      else
        raise ArgumentError, "invalid language type"
      end

    data[identifier] = [klass, nil, nil, identifier, part2b, part2t, part1, language_type, ref_name]
  end

  read_data_file("iso-639-3-macrolanguages.tab.txt", 3, "\t", true) do |args|
    macrolanguage_identifier, individual_language_identifier, status = args

    case status
    when 'R'
      next #FIXME
    when 'A'
    else
      raise ArgumentError, "invalid status"
    end

    # Add macrolanguage to the individual language
    raise "individual language already has a macrolanguage " if data[individual_language_identifier][2]
    data[individual_language_identifier][2] = macrolanguage_identifier

    # Add individual language to macrolanguage
    data[macrolanguage_identifier][1] ||= []
    data[macrolanguage_identifier][1] << individual_language_identifier
  end

  data.each_pair { |k, v| v.freeze }

  data
end

.read_data_file(filename, field_count, delimiter, skip_first) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/iso_codes.rb', line 118

def read_data_file(filename, field_count, delimiter, skip_first)
  File.open(get_data_filename(filename)).each_line do |l|
    if skip_first
      skip_first = false
      next
    end

    yield l.chomp.split(delimiter, field_count)
  end
end