From 36a2f1f3c085dd81171cf7d8220770d4ff921ab3 Mon Sep 17 00:00:00 2001 From: hukl Date: Sat, 7 Feb 2009 15:50:40 +0100 Subject: added globalize2 plugin as well as some modifications which use the new translation facilities. backend mostly. --- .../globalize2/lib/globalize/locale/fallbacks.rb | 63 +++++++++++++++++ .../lib/globalize/locale/language_tag.rb | 81 ++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb create mode 100644 vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb (limited to 'vendor/plugins/globalize2/lib/globalize/locale') diff --git a/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb b/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb new file mode 100644 index 00000000..c4acd57a --- /dev/null +++ b/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb @@ -0,0 +1,63 @@ +require 'globalize/locale/language_tag' + +module I18n + @@fallbacks = nil + + class << self + # Returns the current fallbacks. Defaults to +Globalize::Locale::Fallbacks+. + def fallbacks + @@fallbacks ||= Globalize::Locale::Fallbacks.new + end + + # Sets the current fallbacks. Used to set a custom fallbacks instance. + def fallbacks=(fallbacks) + @@fallbacks = fallbacks + end + end +end + +module Globalize + module Locale + class Fallbacks < Hash + def initialize(*defaults) + @map = {} + map defaults.pop if defaults.last.is_a?(Hash) + + defaults = [I18n.default_locale.to_sym] if defaults.empty? + self.defaults = defaults + end + + def defaults=(defaults) + @defaults = defaults.map{|default| compute(default, false) }.flatten << :root + end + attr_reader :defaults + + def [](tag) + tag = tag.to_sym + has_key?(tag) ? fetch(tag) : store(tag, compute(tag)) + end + + def map(mappings) + mappings.each do |from, to| + from, to = from.to_sym, Array(to) + to.each do |to| + @map[from] ||= [] + @map[from] << to.to_sym + end + end + end + + protected + + def compute(tags, include_defaults = true) + result = Array(tags).collect do |tag| + tags = LanguageTag::tag(tag.to_sym).parents(true).map! {|t| t.to_sym } + tags.each{|tag| tags += compute(@map[tag]) if @map[tag] } + tags + end.flatten + result.push *defaults if include_defaults + result.uniq + end + end + end +end diff --git a/vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb b/vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb new file mode 100644 index 00000000..d9aae54f --- /dev/null +++ b/vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb @@ -0,0 +1,81 @@ +# for specifications see http://en.wikipedia.org/wiki/IETF_language_tag +# +# SimpleParser does not implement advanced usages such as grandfathered tags + +module Globalize + module Locale + module Rfc4646 + SUBTAGS = [:language, :script, :region, :variant, :extension, :privateuse, :grandfathered] + FORMATS = {:language => :downcase, :script => :capitalize, :region => :upcase, :variant => :downcase} + end + + class LanguageTag < Struct.new(*Rfc4646::SUBTAGS) + class << self + def parser + @@parser ||= SimpleParser + end + + def parser=(parser) + @@parser = parser + end + + def tag(tag) + matches = parser.match(tag) + new *matches if matches + end + end + + Rfc4646::FORMATS.each do |name, format| + define_method(name) { self[name].send(format) unless self[name].nil? } + end + + def to_sym + to_s.to_sym + end + + def to_s + @tag ||= to_a.compact.join("-") + end + + def to_a + members.collect {|attr| self.send(attr) } + end + + def parent + segs = to_a.compact + segs.length < 2 ? nil : LanguageTag.tag(segs[0..(segs.length-2)].join('-')) + end + + def parents(include_self = true) + result, parent = [], self.dup + result << parent if include_self + while parent = parent.parent + result << parent + end + result + end + + module SimpleParser + PATTERN = %r{\A(?: + ([a-z]{2,3}(?:(?:-[a-z]{3}){0,3})?|[a-z]{4}|[a-z]{5,8}) # language + (?:-([a-z]{4}))? # script + (?:-([a-z]{2}|\d{3}))? # region + (?:-([0-9a-z]{5,8}|\d[0-9a-z]{3}))* # variant + (?:-([0-9a-wyz](?:-[0-9a-z]{2,8})+))* # extension + (?:-(x(?:-[0-9a-z]{1,8})+))?| # privateuse subtag + (x(?:-[0-9a-z]{1,8})+)| # privateuse tag + /* ([a-z]{1,3}(?:-[0-9a-z]{2,8}){1,2}) */ # grandfathered + )\z}xi + + class << self + def match(tag) + c = PATTERN.match(tag.to_s).captures + c[0..4] << (c[5].nil? ? c[6] : c[5]) << c[7] # TODO c[7] is grandfathered, throw a NotImplemented exception here? + rescue + false + end + end + end + end + end +end -- cgit v1.3