class Locale::Tag::Common

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

Constants

LANGUAGE
SCRIPT
TAG_RE
VARIANT

Attributes

script[R]
variants[R]

Public Class Methods

new(language, script = nil, region = nil, variants = []) click to toggle source

Create a Locale::Tag::Common.

Calls superclass method
# File lib/locale/tag/common.rb, line 56
def initialize(language, script = nil, region = nil, variants = [])
  @script, @variants = script, variants
  @script = @script.capitalize  if @script
  super(language, region)
end
parse(tag) click to toggle source

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

# File lib/locale/tag/common.rb, line 38
def 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

Public Instance Methods

candidates() click to toggle source

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.

# File lib/locale/tag/common.rb, line 81
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
script=(val) click to toggle source

Set the script (with capitalize)

# File lib/locale/tag/common.rb, line 63
def script=(val)
  clear
  @script = val
  @script = @script.capitalize if @script
  @script
end
variants=(val) click to toggle source

Set the variants as an Array.

# File lib/locale/tag/common.rb, line 71
def variants=(val)
  clear
  @variants = val
end

Private Instance Methods

to_string() click to toggle source

Returns the common language tag with “_”.

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

This is used in internal only. Use to_s instead.

# File lib/locale/tag/common.rb, line 116
def to_string
  s = @language.dup
    
  s << "_" << @script if @script
  s << "_" << @region if @region

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