1 # RFC 4646/47 compliant Locale tag implementation that parses locale tags to
2 # subtags such as language, script, region, variant etc.
4 # For more information see by http://en.wikipedia.org/wiki/IETF_language_tag
6 # Rfc4646::Parser does not implement grandfathered tags.
11 RFC4646_SUBTAGS = [ :language, :script, :region, :variant, :extension, :privateuse, :grandfathered ]
12 RFC4646_FORMATS = { :language => :downcase, :script => :capitalize, :region => :upcase, :variant => :downcase }
14 class Rfc4646 < Struct.new(*RFC4646_SUBTAGS)
16 # Parses the given tag and returns a Tag instance if it is valid.
17 # Returns false if the given tag is not valid according to RFC 4646.
19 matches = parser.match(tag)
20 new(*matches) if matches
24 @@parser ||= Rfc4646::Parser
34 RFC4646_FORMATS.each do |name, format|
35 define_method(name) { self[name].send(format) unless self[name].nil? }
43 @tag ||= to_a.compact.join("-")
47 members.collect { |attr| self.send(attr) }
52 ([a-z]{2,3}(?:(?:-[a-z]{3}){0,3})?|[a-z]{4}|[a-z]{5,8}) # language
53 (?:-([a-z]{4}))? # script
54 (?:-([a-z]{2}|\d{3}))? # region
55 (?:-([0-9a-z]{5,8}|\d[0-9a-z]{3}))* # variant
56 (?:-([0-9a-wyz](?:-[0-9a-z]{2,8})+))* # extension
57 (?:-(x(?:-[0-9a-z]{1,8})+))?| # privateuse subtag
58 (x(?:-[0-9a-z]{1,8})+)| # privateuse tag
59 /* ([a-z]{1,3}(?:-[0-9a-z]{2,8}){1,2}) */ # grandfathered
64 c = PATTERN.match(tag.to_s).captures
65 c[0..4] << (c[5].nil? ? c[6] : c[5]) << c[7] # TODO c[7] is grandfathered, throw a NotImplemented exception here?