summaryrefslogtreecommitdiff
path: root/vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb
diff options
context:
space:
mode:
authorhukl <hukl@eight.local>2009-02-07 15:50:40 +0100
committerhukl <hukl@eight.local>2009-02-07 15:50:40 +0100
commit36a2f1f3c085dd81171cf7d8220770d4ff921ab3 (patch)
tree5ded15331b192bf428af4c94d46d1abb28ef0690 /vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb
parentce9645d0092d42c7bf8781378181ffbd4ff3d088 (diff)
added globalize2 plugin as well as some modifications
which use the new translation facilities. backend mostly.
Diffstat (limited to 'vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb')
-rw-r--r--vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb63
1 files changed, 63 insertions, 0 deletions
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 @@
1require 'globalize/locale/language_tag'
2
3module I18n
4 @@fallbacks = nil
5
6 class << self
7 # Returns the current fallbacks. Defaults to +Globalize::Locale::Fallbacks+.
8 def fallbacks
9 @@fallbacks ||= Globalize::Locale::Fallbacks.new
10 end
11
12 # Sets the current fallbacks. Used to set a custom fallbacks instance.
13 def fallbacks=(fallbacks)
14 @@fallbacks = fallbacks
15 end
16 end
17end
18
19module Globalize
20 module Locale
21 class Fallbacks < Hash
22 def initialize(*defaults)
23 @map = {}
24 map defaults.pop if defaults.last.is_a?(Hash)
25
26 defaults = [I18n.default_locale.to_sym] if defaults.empty?
27 self.defaults = defaults
28 end
29
30 def defaults=(defaults)
31 @defaults = defaults.map{|default| compute(default, false) }.flatten << :root
32 end
33 attr_reader :defaults
34
35 def [](tag)
36 tag = tag.to_sym
37 has_key?(tag) ? fetch(tag) : store(tag, compute(tag))
38 end
39
40 def map(mappings)
41 mappings.each do |from, to|
42 from, to = from.to_sym, Array(to)
43 to.each do |to|
44 @map[from] ||= []
45 @map[from] << to.to_sym
46 end
47 end
48 end
49
50 protected
51
52 def compute(tags, include_defaults = true)
53 result = Array(tags).collect do |tag|
54 tags = LanguageTag::tag(tag.to_sym).parents(true).map! {|t| t.to_sym }
55 tags.each{|tag| tags += compute(@map[tag]) if @map[tag] }
56 tags
57 end.flatten
58 result.push *defaults if include_defaults
59 result.uniq
60 end
61 end
62 end
63end