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

Constants included from Util::Memoizable

Util::Memoizable::MEMOIZED_IVAR

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_str

Methods included from Util::Memoizable

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

Constructor Details

#initialize(language, script = nil, region = nil, variants = [], extensions = {}) ⇒ Cldr

Create Locale::Tag::Cldr.

variants should be upcase.



31
32
33
34
35
# File 'lib/locale/tag/cldr.rb', line 31

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.



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

def extensions
  @extensions
end

Class Method Details

.parse(tag) ⇒ Object

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



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/cldr.rb', line 38

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

    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

Instance Method Details

#to_sObject

Returns the language tag. (e.g.) “ja_Hira_JP_VARIANT1_VARIANT2@foo1=var1;foo2=var2”



62
63
64
65
66
67
68
# File 'lib/locale/tag/cldr.rb', line 62

def to_s
  s = super
  if @extensions.size > 0
    s << "@" << @extensions.to_a.sort.map{|k, v| "#{k}=#{v}"}.join(";")
  end
  s
end