iso639 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/iso639.rb +22 -35
- data/lib/iso639/insensitive_hash.rb +2 -2
- data/lib/iso639/language.rb +29 -0
- data/lib/iso639/version.rb +1 -1
- data/test/iso639_test.rb +15 -1
- metadata +2 -1
data/lib/iso639.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
require "iso639/version"
|
2
|
+
require "iso639/language"
|
2
3
|
require "iso639/insensitive_hash"
|
3
4
|
|
4
5
|
# Public: Various methods useful for performing ISO-639 language code lookup
|
5
6
|
# either given their ISO-639-1 or ISO-639-2 character code value or from human
|
6
7
|
# input.
|
7
8
|
#
|
9
|
+
# Language code mappings came from
|
10
|
+
# http://loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
|
11
|
+
#
|
8
12
|
# Examples
|
9
13
|
#
|
10
14
|
# Iso639["en"] # => #<Iso639::Language:...>
|
@@ -25,34 +29,6 @@ require "iso639/insensitive_hash"
|
|
25
29
|
# Iso639["German"].english_name # => "German"
|
26
30
|
# Iso639["German"].french_name # => "allemand"
|
27
31
|
module Iso639
|
28
|
-
class Language
|
29
|
-
attr_reader :alpha3_bibliographic, :alpha3_terminology, :alpha2, :english_name, :french_name
|
30
|
-
alias_method :alpha3, :alpha3_bibliographic
|
31
|
-
alias_method :name, :english_name
|
32
|
-
|
33
|
-
# Public: Initialize a Language.
|
34
|
-
#
|
35
|
-
# alpha3_bibliographic - A String representing the alpha-3 bibliographic code
|
36
|
-
# alpha3_terminology - A String representing the alpha-3 terminology code
|
37
|
-
# alpha2 - A String representing the alpha-2 code
|
38
|
-
# english_name - A String representing the English name
|
39
|
-
# french_name - A String representing the French name
|
40
|
-
def initialize(alpha3_bibliographic, alpha3_terminology, alpha2, english_name, french_name)
|
41
|
-
@alpha3_bibliographic = strip_to_nil(alpha3_bibliographic)
|
42
|
-
@alpha3_terminology = strip_to_nil(alpha3_terminology)
|
43
|
-
@alpha2 = strip_to_nil(alpha2)
|
44
|
-
@english_name = strip_to_nil(english_name)
|
45
|
-
@french_name = strip_to_nil(french_name)
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def strip_to_nil(val)
|
51
|
-
val = val.strip if val
|
52
|
-
return val unless val.nil? || val.empty?
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
32
|
LanguagesByAlpha2 = InsensitiveHash.new
|
57
33
|
LanguagesByAlpha3Bibliographic = LanguagesByAlpha3 = InsensitiveHash.new
|
58
34
|
LanguagesByAlpha3Terminology = InsensitiveHash.new
|
@@ -62,15 +38,26 @@ module Iso639
|
|
62
38
|
iso639_file = File.expand_path(File.join("..", "iso639", "ISO-639-2_utf-8.txt"), __FILE__)
|
63
39
|
File.readlines(iso639_file).each do |line|
|
64
40
|
lang = Language.new *line.split("|")
|
65
|
-
LanguagesByAlpha2[lang.alpha2
|
66
|
-
LanguagesByAlpha3[lang.alpha3
|
67
|
-
LanguagesByAlpha3Terminology[lang.alpha3_terminology
|
68
|
-
LanguagesByName[lang.name
|
69
|
-
LanguagesByFrenchName[lang.french_name
|
41
|
+
LanguagesByAlpha2[lang.alpha2] = lang if lang.alpha2
|
42
|
+
LanguagesByAlpha3[lang.alpha3] = lang if lang.alpha3
|
43
|
+
LanguagesByAlpha3Terminology[lang.alpha3_terminology] = lang if lang.alpha3_terminology
|
44
|
+
LanguagesByName[lang.name] = lang if lang.name
|
45
|
+
LanguagesByFrenchName[lang.french_name] = lang if lang.french_name
|
70
46
|
end
|
71
47
|
|
72
|
-
|
73
|
-
|
48
|
+
# Public: Find a language by any common lookup value
|
49
|
+
#
|
50
|
+
# lookup - A String representing an alpha-2, alpha-3, or language name
|
51
|
+
#
|
52
|
+
# Examples
|
53
|
+
#
|
54
|
+
# Iso639["en"].alpha3 # => "eng"
|
55
|
+
# Iso639["English"].alpha2 # => "en"
|
56
|
+
# Iso639["FRENCH"].alpha3 # => "fre"
|
57
|
+
# Iso639["deu"].alpha3 # => "ger"
|
58
|
+
#
|
59
|
+
# Returns an Iso639::Language object
|
60
|
+
def self.[](lookup)
|
74
61
|
LanguagesByAlpha2[lookup] ||
|
75
62
|
LanguagesByAlpha3Bibliographic[lookup] ||
|
76
63
|
LanguagesByAlpha3Terminology[lookup] ||
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Iso639
|
2
2
|
class InsensitiveHash < Hash # :nodoc:
|
3
3
|
def [](key)
|
4
|
-
|
4
|
+
super(key.to_s.downcase.strip)
|
5
5
|
end
|
6
6
|
|
7
7
|
def []=(key, value)
|
8
|
-
|
8
|
+
super(key.to_s.downcase.strip, value)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Iso639
|
2
|
+
class Language
|
3
|
+
attr_reader :alpha3_bibliographic, :alpha3_terminology, :alpha2, :english_name, :french_name
|
4
|
+
alias_method :alpha3, :alpha3_bibliographic
|
5
|
+
alias_method :name, :english_name
|
6
|
+
|
7
|
+
# Public: Initialize a Language.
|
8
|
+
#
|
9
|
+
# alpha3_bibliographic - A String representing the alpha-3 bibliographic code
|
10
|
+
# alpha3_terminology - A String representing the alpha-3 terminology code
|
11
|
+
# alpha2 - A String representing the alpha-2 code
|
12
|
+
# english_name - A String representing the English name
|
13
|
+
# french_name - A String representing the French name
|
14
|
+
def initialize(alpha3_bibliographic, alpha3_terminology, alpha2, english_name, french_name)
|
15
|
+
@alpha3_bibliographic = strip_to_nil(alpha3_bibliographic)
|
16
|
+
@alpha3_terminology = strip_to_nil(alpha3_terminology)
|
17
|
+
@alpha2 = strip_to_nil(alpha2)
|
18
|
+
@english_name = strip_to_nil(english_name)
|
19
|
+
@french_name = strip_to_nil(french_name)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def strip_to_nil(val)
|
25
|
+
val = val.strip if val
|
26
|
+
return val unless val.nil? || val.empty?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/iso639/version.rb
CHANGED
data/test/iso639_test.rb
CHANGED
@@ -78,7 +78,21 @@ describe Iso639 do
|
|
78
78
|
assert_equal "en", Iso639["EN"].alpha2
|
79
79
|
assert_equal "fr", Iso639["fre"].alpha2
|
80
80
|
assert_equal "fr", Iso639["fra"].alpha2
|
81
|
-
assert_equal "fr", Iso639["
|
81
|
+
assert_equal "fr", Iso639["French"].alpha2
|
82
82
|
assert_equal "fr", Iso639["français"].alpha2
|
83
83
|
end
|
84
|
+
|
85
|
+
it "should ignore case sensitivity" do
|
86
|
+
assert_equal "en", Iso639["EN"].alpha2
|
87
|
+
assert_equal "fr", Iso639["Fre"].alpha2
|
88
|
+
assert_equal "fr", Iso639["FRA"].alpha2
|
89
|
+
assert_equal "fr", Iso639["french"].alpha2
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should ignore whitespace" do
|
93
|
+
assert_equal "en", Iso639[" en"].alpha2
|
94
|
+
assert_equal "fr", Iso639["\tfre\t"].alpha2
|
95
|
+
assert_equal "fr", Iso639[" fra "].alpha2
|
96
|
+
assert_equal "fr", Iso639[" french\t"].alpha2
|
97
|
+
end
|
84
98
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: iso639
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ryan McGeary
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- lib/iso639.rb
|
61
61
|
- lib/iso639/ISO-639-2_utf-8.txt
|
62
62
|
- lib/iso639/insensitive_hash.rb
|
63
|
+
- lib/iso639/language.rb
|
63
64
|
- lib/iso639/version.rb
|
64
65
|
- test/insensitive_hash_test.rb
|
65
66
|
- test/iso639_test.rb
|