Class: Locale::Tag::Common

Inherits:
Simple
  • Object
show all
Defined in:
lib/locale/tag/common.rb

Overview

Common Language tag class for Ruby. Java and MS Windows use this format.

  • ja (language: RFC4646)

  • ja_JP (country: RFC4646(2 alpha or 3 digit))

  • ja-JP

  • ja_Hira_JP (script: 4 characters)

  • ja-Hira-JP

  • ja_Hira_JP_MOBILE (variants: more than 2 characters or 3 digit)

  • ja_Hira_JP_MOBILE_IPHONE (2 variants example)

Direct Known Subclasses

Cldr, Rfc

Constant Summary collapse

LANGUAGE =

RFC4646 (ISO639/reserved/registered)

"(#{ALPHA}{2,3}|#{ALPHA}{4}|#{ALPHA}{5,8})"
SCRIPT =
"(#{ALPHA}{4})"
VARIANT =

RFC3066 compatible

"(#{ALPHANUM}{3,}|#{DIGIT}#{ALPHANUM}{3})"
TAG_RE =
/\A#{LANGUAGE}(?:[-_]#{SCRIPT})?
(?:[-_]#{REGION})?((?:[-_]#{VARIANT})*)\Z/ix

Constants inherited from Simple

Simple::ALPHA, Simple::ALPHANUM, Simple::DIGIT, Simple::REGION

Constants included from Util::Memoizable

Util::Memoizable::MEMOIZED_IVAR

Instance Attribute Summary collapse

Attributes inherited from Simple

#language, #region, #tag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Simple

#==, #country, #eql?, #hash, #inspect, #to_str

Methods included from Util::Memoizable

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

Constructor Details

#initialize(language, script = nil, region = nil, variants = []) ⇒ Common

Create a Locale::Tag::Common.



36
37
38
39
40
# File 'lib/locale/tag/common.rb', line 36

def initialize(language, script = nil, region = nil, variants = [])
  @script, @variants = script, variants
  @script.capitalize!  if @script
  super(language, region)
end

Instance Attribute Details

#scriptObject

Returns the value of attribute script.



33
34
35
# File 'lib/locale/tag/common.rb', line 33

def script
  @script
end

#variantsObject

Returns the value of attribute variants.



33
34
35
# File 'lib/locale/tag/common.rb', line 33

def variants
  @variants
end

Class Method Details

.parse(tag) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/locale/tag/common.rb', line 43

def self.parse(tag)
  if tag =~ /\APOSIX\Z/  # This is the special case of POSIX locale but match this regexp.
    nil
  elsif tag =~ TAG_RE
    lang, script, region, subtag = $1, $2, $3, $4
    variants = subtag.scan(/(^|[-_])#{VARIANT}(?=([-_]|$))/i).collect{|v| v[1]}

    ret = self.new(lang, script, region, variants)
    ret.tag = tag
    ret
  else
    nil
  end
end

Instance Method Details

#candidatesObject

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

Locale::Tag::Rfc, Cldr don’t have their own candidates, because it’s meaningless to compare the extensions, privateuse, etc.



92
93
94
95
96
97
98
99
100
101
# File 'lib/locale/tag/common.rb', line 92

def candidates
  [self.class.new(language, script, region, variants),   #ja-Kana-JP-FOO
   self.class.new(language, script, region),             #ja-Kana-JP
   self.class.new(language, nil, region, variants),      #ja-JP-FOO
   self.class.new(language, nil, region),                #ja-JP
   self.class.new(language, script, nil, variants),      #ja-Kana-FOO
   self.class.new(language, script),                     #ja-Kana
   self.class.new(language, nil, nil, variants),         #ja-FOO
   self.class.new(language)]                             #ja
end

#to_sObject

Returns the common language tag with “_”.

<language>_<Script>_<REGION>_VARIANTS1_VARIANTS2
(e.g.) "ja_Hira_JP_VARIANTS1_VARIANTS2"


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/locale/tag/common.rb', line 75

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

  @variants.each do |v|
    s << "_#{v}"
  end
  s
end