Class: Locale::Tag::Cldr

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

Overview

Unicode locale identifier class for CLDR-1.6.1. (Unicode Common Locale Data Repository).

Constant Summary collapse

VARIANT =
"(#{ALPHANUM}{5,8}|#{DIGIT}#{ALPHANUM}{3})"
EXTENSION =
"#{ALPHANUM}+=[a-z0-9\-]+"
TAG_RE =
/\A#{LANGUAGE}(?:[-_]#{SCRIPT})?
(?:[-_]#{REGION})?((?:[-_]#{VARIANT})*
(?:@(#{EXTENSION};?)+)*)\Z/ix

Constants inherited from Common

Locale::Tag::Common::LANGUAGE, Locale::Tag::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 = {}) ⇒ Cldr

Create Locale::Tag::Cldr.

variants should be upcase.



58
59
60
61
62
# File 'lib/locale/tag/cldr.rb', line 58

def initialize(language, script = nil, region = nil, 
               variants = [], extensions = {})
  @extensions = extensions
  super(language, script, region, variants.map{|v| v.upcase})
end

Instance Attribute Details

#extensionsObject

Returns the value of attribute extensions.



27
28
29
# File 'lib/locale/tag/cldr.rb', line 27

def extensions
  @extensions
end

Class Method Details

.parse(tag) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locale/tag/cldr.rb', line 31

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 = {}
    subtag.scan(/#{EXTENSION}/i).each{|v| 
      subtag.sub!(v, "")
      key, type = v.split("=")
      extensions[key] = type
    }
    variants = subtag.scan(/#{VARIANT}/i).collect{|v| v[0].upcase}
    
    ret = self.new(lang, script, region, variants, extensions)
    ret.tag = tag
    ret
  else
    nil
  end
end