1 # for specifications see http://en.wikipedia.org/wiki/IETF_language_tag
3 # SimpleParser does not implement advanced usages such as grandfathered tags
8 SUBTAGS = [:language, :script, :region, :variant, :extension, :privateuse, :grandfathered]
9 FORMATS = {:language => :downcase, :script => :capitalize, :region => :upcase, :variant => :downcase}
12 class LanguageTag < Struct.new(*Rfc4646::SUBTAGS)
15 @@parser ||= SimpleParser
23 matches = parser.match(tag)
24 new *matches if matches
28 Rfc4646::FORMATS.each do |name, format|
29 define_method(name) { self[name].send(format) unless self[name].nil? }
37 @tag ||= to_a.compact.join("-")
41 members.collect {|attr| self.send(attr) }
46 segs.length < 2 ? nil : LanguageTag.tag(segs[0..(segs.length-2)].join('-'))
49 def parents(include_self = true)
50 result, parent = [], self.dup
51 result << parent if include_self
52 while parent = parent.parent
60 ([a-z]{2,3}(?:(?:-[a-z]{3}){0,3})?|[a-z]{4}|[a-z]{5,8}) # language
61 (?:-([a-z]{4}))? # script
62 (?:-([a-z]{2}|\d{3}))? # region
63 (?:-([0-9a-z]{5,8}|\d[0-9a-z]{3}))* # variant
64 (?:-([0-9a-wyz](?:-[0-9a-z]{2,8})+))* # extension
65 (?:-(x(?:-[0-9a-z]{1,8})+))?| # privateuse subtag
66 (x(?:-[0-9a-z]{1,8})+)| # privateuse tag
67 /* ([a-z]{1,3}(?:-[0-9a-z]{2,8}){1,2}) */ # grandfathered
72 c = PATTERN.match(tag.to_s).captures
73 c[0..4] << (c[5].nil? ? c[6] : c[5]) << c[7] # TODO c[7] is grandfathered, throw a NotImplemented exception here?