=begin locale/tag/common.rb - Locale::Tag::Common Copyright (C) 2008 Masao Mutoh You may redistribute it and/or modify it under the same license terms as Ruby. $Id: common.rb 27 2008-12-03 15:06:50Z mutoh $ =end module Locale module Tag # 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) # class Common < Simple LANGUAGE = "(#{ALPHA}{2,3}|#{ALPHA}{4}|#{ALPHA}{5,8})" #RFC4646 (ISO639/reserved/registered) SCRIPT = "(#{ALPHA}{4})" VARIANT = "(#{ALPHANUM}{3,}|#{DIGIT}#{ALPHANUM}{3})" #RFC3066 compatible TAG_RE = /\A#{LANGUAGE}(?:[-_]#{SCRIPT})? (?:[-_]#{REGION})?((?:[-_]#{VARIANT})*)\Z/ix attr_reader :script, :variants # Create a Locale::Tag::Common. def initialize(language, script = nil, region = nil, variants = []) @script, @variants = script, variants @script.capitalize! if @script super(language, region) end # Parse the language tag and return the new Locale::Tag::Common. 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 variants = subtag.scan(/(^|[-_])#{VARIANT}(?=([-_]|$))/i).collect{|v| v[1]} ret = self.new(lang, script, region, variants) ret.tag = tag ret else nil end end # Set the script (with capitalize) def script=(val) clear @script = val @script.capitalize! if @script @script end # Set the variants def variants=(val) clear @variants = val end # Returns the common language tag with "_". # _