lib/iso639.rb in iso639-1.0.1 vs lib/iso639.rb in iso639-1.0.2
- old
+ new
@@ -1,12 +1,16 @@
require "iso639/version"
+require "iso639/language"
require "iso639/insensitive_hash"
# Public: Various methods useful for performing ISO-639 language code lookup
# either given their ISO-639-1 or ISO-639-2 character code value or from human
# input.
#
+# Language code mappings came from
+# http://loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
+#
# Examples
#
# Iso639["en"] # => #<Iso639::Language:...>
# Iso639["en"].alpha2 # => "en"
# Iso639["en"].alpha3 # => "eng"
@@ -23,55 +27,38 @@
# Iso639["German"].alpha3_terminology # => "deu"
# Iso639["German"].name # => "German"
# Iso639["German"].english_name # => "German"
# Iso639["German"].french_name # => "allemand"
module Iso639
- class Language
- attr_reader :alpha3_bibliographic, :alpha3_terminology, :alpha2, :english_name, :french_name
- alias_method :alpha3, :alpha3_bibliographic
- alias_method :name, :english_name
-
- # Public: Initialize a Language.
- #
- # alpha3_bibliographic - A String representing the alpha-3 bibliographic code
- # alpha3_terminology - A String representing the alpha-3 terminology code
- # alpha2 - A String representing the alpha-2 code
- # english_name - A String representing the English name
- # french_name - A String representing the French name
- def initialize(alpha3_bibliographic, alpha3_terminology, alpha2, english_name, french_name)
- @alpha3_bibliographic = strip_to_nil(alpha3_bibliographic)
- @alpha3_terminology = strip_to_nil(alpha3_terminology)
- @alpha2 = strip_to_nil(alpha2)
- @english_name = strip_to_nil(english_name)
- @french_name = strip_to_nil(french_name)
- end
-
- private
-
- def strip_to_nil(val)
- val = val.strip if val
- return val unless val.nil? || val.empty?
- end
- end
-
LanguagesByAlpha2 = InsensitiveHash.new
LanguagesByAlpha3Bibliographic = LanguagesByAlpha3 = InsensitiveHash.new
LanguagesByAlpha3Terminology = InsensitiveHash.new
LanguagesByEnglishName = LanguagesByName = InsensitiveHash.new
LanguagesByFrenchName = InsensitiveHash.new
iso639_file = File.expand_path(File.join("..", "iso639", "ISO-639-2_utf-8.txt"), __FILE__)
File.readlines(iso639_file).each do |line|
lang = Language.new *line.split("|")
- LanguagesByAlpha2[lang.alpha2.downcase.strip] = lang if lang.alpha2
- LanguagesByAlpha3[lang.alpha3.downcase.strip] = lang if lang.alpha3
- LanguagesByAlpha3Terminology[lang.alpha3_terminology.downcase.strip] = lang if lang.alpha3_terminology
- LanguagesByName[lang.name.downcase.strip] = lang if lang.name
- LanguagesByFrenchName[lang.french_name.downcase.strip] = lang if lang.french_name
+ LanguagesByAlpha2[lang.alpha2] = lang if lang.alpha2
+ LanguagesByAlpha3[lang.alpha3] = lang if lang.alpha3
+ LanguagesByAlpha3Terminology[lang.alpha3_terminology] = lang if lang.alpha3_terminology
+ LanguagesByName[lang.name] = lang if lang.name
+ LanguagesByFrenchName[lang.french_name] = lang if lang.french_name
end
- def self.[](alpha_code_or_name)
- lookup = alpha_code_or_name.to_s.downcase.strip
+ # Public: Find a language by any common lookup value
+ #
+ # lookup - A String representing an alpha-2, alpha-3, or language name
+ #
+ # Examples
+ #
+ # Iso639["en"].alpha3 # => "eng"
+ # Iso639["English"].alpha2 # => "en"
+ # Iso639["FRENCH"].alpha3 # => "fre"
+ # Iso639["deu"].alpha3 # => "ger"
+ #
+ # Returns an Iso639::Language object
+ def self.[](lookup)
LanguagesByAlpha2[lookup] ||
LanguagesByAlpha3Bibliographic[lookup] ||
LanguagesByAlpha3Terminology[lookup] ||
LanguagesByEnglishName[lookup] ||
LanguagesByFrenchName[lookup]