summaryrefslogtreecommitdiff
path: root/vendor/plugins/globalize2/lib/globalize/locale
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
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')
-rw-r--r--vendor/plugins/globalize2/lib/globalize/locale/fallbacks.rb63
-rw-r--r--vendor/plugins/globalize2/lib/globalize/locale/language_tag.rb81
2 files changed, 144 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
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 @@
1# for specifications see http://en.wikipedia.org/wiki/IETF_language_tag
2#
3# SimpleParser does not implement advanced usages such as grandfathered tags
4
5module Globalize
6 module Locale
7 module Rfc4646
8 SUBTAGS = [:language, :script, :region, :variant, :extension, :privateuse, :grandfathered]
9 FORMATS = {:language => :downcase, :script => :capitalize, :region => :upcase, :variant => :downcase}
10 end
11
12 class LanguageTag < Struct.new(*Rfc4646::SUBTAGS)
13 class << self
14 def parser
15 @@parser ||= SimpleParser
16 end
17
18 def parser=(parser)
19 @@parser = parser
20 end
21
22 def tag(tag)
23 matches = parser.match(tag)
24 new *matches if matches
25 end
26 end
27
28 Rfc4646::FORMATS.each do |name, format|
29 define_method(name) { self[name].send(format) unless self[name].nil? }
30 end
31
32 def to_sym
33 to_s.to_sym
34 end
35
36 def to_s
37 @tag ||= to_a.compact.join("-")
38 end
39
40 def to_a
41 members.collect {|attr| self.send(attr) }
42 end
43
44 def parent
45 segs = to_a.compact
46 segs.length < 2 ? nil : LanguageTag.tag(segs[0..(segs.length-2)].join('-'))
47 end
48
49 def parents(include_self = true)
50 result, parent = [], self.dup
51 result << parent if include_self
52 while parent = parent.parent
53 result << parent
54 end
55 result
56 end
57
58 module SimpleParser
59 PATTERN = %r{\A(?:
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
68 )\z}xi
69
70 class << self
71 def match(tag)
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?
74 rescue
75 false
76 end
77 end
78 end
79 end
80 end
81end