Class: Locale::Tag::Simple

Inherits:
Object
  • Object
show all
Includes:
Util::Memoizable
Defined in:
lib/locale/tag/simple.rb

Overview

Abstract language tag class. This class has <language>, <region> which all of language tag specifications have.

  • ja (language: ISO 639 (2 or 3 alpha))

  • ja_JP (country: RFC4646 (ISO3166/UN M.49) (2 alpha or 3 digit)

  • ja-JP

  • ja-392

Direct Known Subclasses

Common, Illegular, Posix

Constant Summary collapse

ALPHA =
'[a-z]'
DIGIT =
'[0-9]'
ALPHANUM =
"[a-zA-Z0-9]"
LANGUAGE =

ISO 639

"(#{ALPHA}{2,3})"
REGION =

RFC4646 (ISO3166/UN M.49)

"(#{ALPHA}{2}|#{DIGIT}{3})"
TAG_RE =
/\A#{LANGUAGE}(?:[_-]#{REGION})?\Z/i

Constants included from Util::Memoizable

Util::Memoizable::MEMOIZED_IVAR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Memoizable

#clear, #freeze, #freeze_without_memoizable, included, #memoize

Constructor Details

#initialize(language, region = nil) ⇒ Simple

Create a Locale::Tag::Simple



71
72
73
74
75
76
# File 'lib/locale/tag/simple.rb', line 71

def initialize(language, region = nil)
  raise "language can't be nil." unless language
  @language, @region = language, region
  @language.downcase! if @language
  @region.upcase! if @region
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



36
37
38
# File 'lib/locale/tag/simple.rb', line 36

def language
  @language
end

#regionObject

Returns the value of attribute region.



36
37
38
# File 'lib/locale/tag/simple.rb', line 36

def region
  @region
end

#tagObject

tag is set when .parse method is called. This value is used when the program want to know the original String.



41
42
43
# File 'lib/locale/tag/simple.rb', line 41

def tag
  @tag
end

Class Method Details

.parse(tag) ⇒ Object

Parse the language tag and return the new Locale::Tag::Simple.



44
45
46
47
48
49
50
51
52
# File 'lib/locale/tag/simple.rb', line 44

def self.parse(tag)
  if tag =~ TAG_RE
    ret = self.new($1, $2)
    ret.tag = tag
    ret
  else
    nil
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



91
92
93
# File 'lib/locale/tag/simple.rb', line 91

def ==(other)  #:nodoc:
  other != nil and hash == other.hash
end

#candidatesObject

Returns an Array of tag-candidates order by priority. Use Locale.candidates instead of this method.



128
129
130
# File 'lib/locale/tag/simple.rb', line 128

def candidates
  [self.class.new(language, region), self.class.new(language)]
end

#countryObject

For backward compatibility.



108
# File 'lib/locale/tag/simple.rb', line 108

def country; region end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


95
96
97
# File 'lib/locale/tag/simple.rb', line 95

def eql?(other) #:nodoc:
  self.==(other)
end

#hashObject

:nodoc:



99
100
101
# File 'lib/locale/tag/simple.rb', line 99

def hash #:nodoc:
  "#{self.class}:#{to_s}".hash
end

#inspectObject

:nodoc:



103
104
105
# File 'lib/locale/tag/simple.rb', line 103

def inspect  #:nodoc:
  %Q[#<#{self.class}: #{to_s}>]
end

#to_sObject

Returns the language tag as the String.

<language>_<REGION>
(e.g.) "ja_JP"


81
82
83
84
85
# File 'lib/locale/tag/simple.rb', line 81

def to_s
  s = @language.dup
  s << "_" << @region if @region
  s
end

#to_strObject

:nodoc:



87
88
89
# File 'lib/locale/tag/simple.rb', line 87

def to_str  #:nodoc:
  to_s
end