Class: LanguageTag

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/language_tag.rb

Overview

BCP 47

Constant Summary collapse

VERSION =
"5646.0.1"
A3_TO_A2 =
{
  'ara' => 'ar',
  'deu' => 'de',
  'eng' => 'en',
  'fra' => 'fr',
  'ita' => 'it',
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iso_code, extlang = nil, script = nil, region = nil, variant = [], extension = [], privateuse = nil) ⇒ LanguageTag

Returns a new instance of LanguageTag.



34
35
36
37
38
39
40
41
# File 'lib/rack/language_tag.rb', line 34

def initialize(iso_code, extlang = nil, script = nil, region = nil, variant = [], extension = [], privateuse = nil)
  case iso_code.length
  when 3
    @alpha3 = iso_code
  when 2
    @alpha2 = iso_code
  end
end

Class Method Details

.parse(raw_code) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/language_tag.rb', line 20

def self.parse(raw_code)
  iso_code = raw_code.split('-').flatten.first

  if iso_code.nil?
    raise "Unparseable language tag"
  end

  if !(A3_TO_A2.keys + A3_TO_A2.values).include?(iso_code)
    raise "Unknown language tag #{iso_code}" #FIXME
  end

  return LanguageTag.new(iso_code)
end

Instance Method Details

#==(other_code) ⇒ Object



63
64
65
# File 'lib/rack/language_tag.rb', line 63

def ==(other_code)
  return self.complete == LanguageTag.new(other_code).complete
end

#a2_to_a3(alpha2) ⇒ Object



55
56
57
# File 'lib/rack/language_tag.rb', line 55

def a2_to_a3(alpha2)
  return A3_TO_A2.invert[alpha2]
end

#a3_to_a2(alpha3) ⇒ Object



51
52
53
# File 'lib/rack/language_tag.rb', line 51

def a3_to_a2(alpha3)
  return A3_TO_A2[alpha3]
end

#alpha2Object



43
44
45
# File 'lib/rack/language_tag.rb', line 43

def alpha2
  @alpha2 ||= a3_to_a2(@alpha3)
end

#alpha3Object



47
48
49
# File 'lib/rack/language_tag.rb', line 47

def alpha3
  @alpha3 ||= a2_to_a3(@alpha2)
end

#completeObject



59
60
61
# File 'lib/rack/language_tag.rb', line 59

def complete
  alpha3
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/rack/language_tag.rb', line 71

def eql?(other)
  if self.equal?(other)
    return true
  elsif self.class != other.class
    return false
  end

  return self.alpha3 == other.alpha3
end

#hashObject



67
68
69
# File 'lib/rack/language_tag.rb', line 67

def hash
  self.complete.hash
end

#inspectObject



81
82
83
# File 'lib/rack/language_tag.rb', line 81

def inspect
  return "#<LocaleCode '#{complete}'>"
end