Class: Locale::Tag::Common
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)
Constants
Name | Value |
---|---|
LANGUAGE | "(#{ALPHA}{2,3}|#{ALPHA}{4}|#{ALPHA}{5,8})" |
SCRIPT | "(#{ALPHA}{4})" |
VARIANT | "(#{ALPHANUM}{3,}|#{DIGIT}#{ALPHANUM}{3})" |
TAG_RE | /\A#{LANGUAGE}(?:[-_]#{SCRIPT})? (?:[-_]#{REGION})?((?:[-_]#{VARIANT})*)\Z/ix |
Attributes
Name | Read/write? |
---|---|
script | R |
variants | R |
Public Class Methods
new (language, script = nil, region = nil, variants = [])
Create a Locale::Tag::Common.
# File lib/locale/tag/common.rb, line 36 36: def initialize(language, script = nil, region = nil, variants = []) 37: @script, @variants = script, variants 38: @script.capitalize! if @script 39: super(language, region) 40: end
parse (tag)
Parse the language tag and return the new Locale::Tag::Common.
# File lib/locale/tag/common.rb, line 43 43: def self.parse(tag) 44: if tag =~ /\APOSIX\Z/ # This is the special case of POSIX locale but match this regexp. 45: nil 46: elsif tag =~ TAG_RE 47: lang, script, region, subtag = $1, $2, $3, $4 48: variants = subtag.scan(/(^|[-_])#{VARIANT}(?=([-_]|$))/i).collect{|v| v[1]} 49: 50: ret = self.new(lang, script, region, variants) 51: ret.tag = tag 52: ret 53: else 54: nil 55: end 56: end
Public Instance Methods
candidates ()
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 92 92: def candidates 93: [self.class.new(language, script, region, variants), #ja-Kana-JP-FOO 94: self.class.new(language, script, region), #ja-Kana-JP 95: self.class.new(language, nil, region, variants), #ja-JP-FOO 96: self.class.new(language, nil, region), #ja-JP 97: self.class.new(language, script, nil, variants), #ja-Kana-FOO 98: self.class.new(language, script), #ja-Kana 99: self.class.new(language, nil, nil, variants), #ja-FOO 100: self.class.new(language)] #ja 101: end
script= (val)
Set the script (with capitalize)
# File lib/locale/tag/common.rb, line 59 59: def script=(val) 60: clear 61: @script = val 62: @script.capitalize! if @script 63: @script 64: end
to_s ()
Returns the common language tag with "_".
<language>_<Script>_<REGION>_VARIANTS1_VARIANTS2 (e.g.) "ja_Hira_JP_VARIANTS1_VARIANTS2"
# File lib/locale/tag/common.rb, line 75 75: def to_s 76: s = @language.dup 77: 78: s << "_" << @script if @script 79: s << "_" << @region if @region 80: 81: @variants.each do |v| 82: s << "_#{v}" 83: end 84: s 85: end
variants= (val)
Set the variants
# File lib/locale/tag/common.rb, line 67 67: def variants=(val) 68: clear 69: @variants = val 70: end