Class: Locale::Tag::Rfc

Inherits:
Common show all
Defined in:
lib/locale/tag/rfc.rb

Overview

Language tag class for RFC4646(BCP47).

Constant Summary collapse

SINGLETON =
'[a-wyz0-9]'
VARIANT =
"(#{ALPHANUM}{5,8}|#{DIGIT}#{ALPHANUM}{3})"
EXTENSION =
"(#{SINGLETON}(?:-#{ALPHANUM}{2,8})+)"
PRIVATEUSE =
"(x(?:-#{ALPHANUM}{1,8})+)"
GRANDFATHERED =
"#{ALPHA}{1,3}(?:-#{ALPHANUM}{2,8}){1,2}"
TAG_RE =
/\A#{LANGUAGE}(?:-#{SCRIPT})?
(?:-#{REGION})?((?:-#{VARIANT})*
(?:-#{EXTENSION})*(?:-#{PRIVATEUSE})?)\Z/ix

Constants inherited from Common

Common::LANGUAGE, Common::SCRIPT

Constants inherited from Simple

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

Instance Attribute Summary collapse

Attributes inherited from Common

#script, #variants

Attributes inherited from Simple

#language, #region, #tag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common

#candidates

Methods inherited from Simple

#<=>, #==, #candidates, #country, #eql?, #hash, #inspect, #to_s, #to_str

Constructor Details

#initialize(language, script = nil, region = nil, variants = [], extensions = [], privateuse = nil) ⇒ Rfc

Returns a new instance of Rfc.



61
62
63
64
65
# File 'lib/locale/tag/rfc.rb', line 61

def initialize(language, script = nil, region = nil, variants = [],
             extensions = [], privateuse = nil)
  @extensions, @privateuse = extensions, privateuse
  super(language, script, region, variants)
end

Instance Attribute Details

#extensionsObject

Returns the value of attribute extensions.



28
29
30
# File 'lib/locale/tag/rfc.rb', line 28

def extensions
  @extensions
end

#privateuseObject

Returns the value of attribute privateuse.



28
29
30
# File 'lib/locale/tag/rfc.rb', line 28

def privateuse
  @privateuse
end

Class Method Details

.parse(tag) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/locale/tag/rfc.rb', line 32

def parse(tag)
  case tag
  when /\APOSIX\Z/  # This is the special case of POSIX locale but match this regexp.
    nil
  when TAG_RE
    lang, script, region, subtag = $1, $2, $3, $4
    extensions = []
    variants = []
    if subtag =~ /#{PRIVATEUSE}/
        subtag, privateuse = $`, $1
      # Private use for CLDR.
      if /x-ldml(.*)/ =~ privateuse
        p_subtag = $1 
        extensions = p_subtag.scan(/(^|-)#{EXTENSION}/i).collect{|v| p_subtag.sub!(v[1], ""); v[1]}
        variants = p_subtag.scan(/(^|-)#{VARIANT}(?=(-|$))/i).collect{|v| v[1]}
      end
    end
    extensions += subtag.scan(/(^|-)#{EXTENSION}/i).collect{|v| subtag.sub!(v[1], ""); v[1]}
    variants += subtag.scan(/(^|-)#{VARIANT}(?=(-|$))/i).collect{|v| v[1]}
    
    ret = self.new(lang, script, region, variants, extensions, privateuse)
    ret.tag = tag
    ret
  else
    nil
  end
end