diff options
| author | hukl <hukl@eight.local> | 2009-02-07 15:50:40 +0100 |
|---|---|---|
| committer | hukl <hukl@eight.local> | 2009-02-07 15:50:40 +0100 |
| commit | 36a2f1f3c085dd81171cf7d8220770d4ff921ab3 (patch) | |
| tree | 5ded15331b192bf428af4c94d46d1abb28ef0690 /vendor/plugins/globalize2/lib/globalize/backend | |
| parent | ce9645d0092d42c7bf8781378181ffbd4ff3d088 (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/backend')
3 files changed, 198 insertions, 0 deletions
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/chain.rb b/vendor/plugins/globalize2/lib/globalize/backend/chain.rb new file mode 100644 index 00000000..bb8679ec --- /dev/null +++ b/vendor/plugins/globalize2/lib/globalize/backend/chain.rb | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | module I18n | ||
| 2 | class << self | ||
| 3 | def chain_backends(*args) | ||
| 4 | self.backend = Globalize::Backend::Chain.new(*args) | ||
| 5 | end | ||
| 6 | end | ||
| 7 | end | ||
| 8 | |||
| 9 | module Globalize | ||
| 10 | module Backend | ||
| 11 | class Chain | ||
| 12 | def initialize(*args) | ||
| 13 | add(*args) unless args.empty? | ||
| 14 | end | ||
| 15 | |||
| 16 | # Change this to a) accept any number of backends and b) accept classes. | ||
| 17 | # When classes are passed instantiate them and add the instances as backends. | ||
| 18 | # Return the added backends from #add. | ||
| 19 | # | ||
| 20 | # Add an initialize method that accepts the same arguments and passes them | ||
| 21 | # to #add, so we could: | ||
| 22 | # I18n.backend = Globalize::Backend::Chain.new(Globalize::Backend::Foo, Globalize::Backend::Bar) | ||
| 23 | # Globalize::Backend::Chain.new(:foo, :bar) | ||
| 24 | # Globalize.chain_backends :foo, :bar | ||
| 25 | def add(*backends) | ||
| 26 | backends.each do |backend| | ||
| 27 | backend = Globalize::Backend.const_get(backend.to_s.capitalize) if backend.is_a? Symbol | ||
| 28 | backend = backend.new if backend.is_a? Class | ||
| 29 | self.backends << backend | ||
| 30 | end | ||
| 31 | end | ||
| 32 | |||
| 33 | def load_translations(*args) | ||
| 34 | backends.each{|backend| backend.load_translations(*args) } | ||
| 35 | end | ||
| 36 | |||
| 37 | # For defaults: | ||
| 38 | # Never pass any default option to the backends but instead implement our own default | ||
| 39 | # mechanism (e.g. symbols as defaults would need to be passed to the whole chain to | ||
| 40 | # be translated). | ||
| 41 | # | ||
| 42 | # For namespace lookup: | ||
| 43 | # Only return if the result is not a hash OR count is not present, otherwise merge them. | ||
| 44 | # So in effect the count variable would control whether we have a namespace lookup or a | ||
| 45 | # pluralization going on. | ||
| 46 | # | ||
| 47 | # Exceptions: | ||
| 48 | # Make sure that we catch MissingTranslationData exceptions and raise | ||
| 49 | # one in the end when no translation was found at all. | ||
| 50 | # | ||
| 51 | # For bulk translation: | ||
| 52 | # If the key is an array we need to call #translate for each of the | ||
| 53 | # keys and collect the results. | ||
| 54 | |||
| 55 | def translate(locale, key, options = {}) | ||
| 56 | raise I18n::InvalidLocale.new(locale) if locale.nil? | ||
| 57 | return key.map{|k| translate locale, k, options } if key.is_a? Array | ||
| 58 | |||
| 59 | default = options.delete(:default) | ||
| 60 | result = backends.inject({}) do |namespace, backend| | ||
| 61 | begin | ||
| 62 | translation = backend.translate(locale.to_sym, key, options) | ||
| 63 | if namespace_lookup?(translation, options) | ||
| 64 | namespace.merge! translation | ||
| 65 | elsif translation | ||
| 66 | return translation | ||
| 67 | end | ||
| 68 | rescue I18n::MissingTranslationData | ||
| 69 | end | ||
| 70 | end | ||
| 71 | result || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options)) | ||
| 72 | end | ||
| 73 | |||
| 74 | def localize(locale, object, format = :default) | ||
| 75 | backends.each do |backend| | ||
| 76 | result = backend.localize(locale, object, format) and return result | ||
| 77 | end | ||
| 78 | end | ||
| 79 | |||
| 80 | protected | ||
| 81 | def backends | ||
| 82 | @backends ||= [] | ||
| 83 | end | ||
| 84 | |||
| 85 | def default(locale, default, options = {}) | ||
| 86 | case default | ||
| 87 | when String then default | ||
| 88 | when Symbol then translate locale, default, options | ||
| 89 | when Array then default.each do |obj| | ||
| 90 | result = default(locale, obj, options.dup) and return result | ||
| 91 | end and nil | ||
| 92 | end | ||
| 93 | rescue I18n::MissingTranslationData | ||
| 94 | nil | ||
| 95 | end | ||
| 96 | |||
| 97 | def namespace_lookup?(result, options) | ||
| 98 | result.is_a?(Hash) and not options.has_key?(:count) | ||
| 99 | end | ||
| 100 | end | ||
| 101 | end | ||
| 102 | end \ No newline at end of file | ||
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb b/vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb new file mode 100644 index 00000000..80016f20 --- /dev/null +++ b/vendor/plugins/globalize2/lib/globalize/backend/pluralizing.rb | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | require 'i18n/backend/simple' | ||
| 2 | |||
| 3 | module Globalize | ||
| 4 | module Backend | ||
| 5 | class Pluralizing < I18n::Backend::Simple | ||
| 6 | def pluralize(locale, entry, count) | ||
| 7 | return entry unless entry.is_a?(Hash) and count | ||
| 8 | key = :zero if count == 0 && entry.has_key?(:zero) | ||
| 9 | key ||= pluralizer(locale).call(count) | ||
| 10 | raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key) | ||
| 11 | translation entry[key], :plural_key => key | ||
| 12 | end | ||
| 13 | |||
| 14 | def add_pluralizer(locale, pluralizer) | ||
| 15 | pluralizers[locale.to_sym] = pluralizer | ||
| 16 | end | ||
| 17 | |||
| 18 | def pluralizer(locale) | ||
| 19 | pluralizers[locale.to_sym] || default_pluralizer | ||
| 20 | end | ||
| 21 | |||
| 22 | protected | ||
| 23 | def default_pluralizer | ||
| 24 | pluralizers[:en] | ||
| 25 | end | ||
| 26 | |||
| 27 | def pluralizers | ||
| 28 | @pluralizers ||= { :en => lambda{|n| n == 1 ? :one : :other } } | ||
| 29 | end | ||
| 30 | |||
| 31 | # Overwrite this method to return something other than a String | ||
| 32 | def translation(string, attributes) | ||
| 33 | string | ||
| 34 | end | ||
| 35 | end | ||
| 36 | end | ||
| 37 | end \ No newline at end of file | ||
diff --git a/vendor/plugins/globalize2/lib/globalize/backend/static.rb b/vendor/plugins/globalize2/lib/globalize/backend/static.rb new file mode 100644 index 00000000..3903517c --- /dev/null +++ b/vendor/plugins/globalize2/lib/globalize/backend/static.rb | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | require 'globalize/backend/pluralizing' | ||
| 2 | require 'globalize/locale/fallbacks' | ||
| 3 | require 'globalize/translation' | ||
| 4 | |||
| 5 | module Globalize | ||
| 6 | module Backend | ||
| 7 | class Static < Pluralizing | ||
| 8 | def initialize(*args) | ||
| 9 | add(*args) unless args.empty? | ||
| 10 | end | ||
| 11 | |||
| 12 | def translate(locale, key, options = {}) | ||
| 13 | result, default, fallback = nil, options.delete(:default), nil | ||
| 14 | I18n.fallbacks[locale].each do |fallback| | ||
| 15 | begin | ||
| 16 | result = super(fallback, key, options) and break | ||
| 17 | rescue I18n::MissingTranslationData | ||
| 18 | end | ||
| 19 | end | ||
| 20 | result ||= default locale, default, options | ||
| 21 | |||
| 22 | attrs = {:requested_locale => locale, :locale => fallback, :key => key, :options => options} | ||
| 23 | translation(result, attrs) || raise(I18n::MissingTranslationData.new(locale, key, options)) | ||
| 24 | end | ||
| 25 | |||
| 26 | protected | ||
| 27 | |||
| 28 | alias :orig_interpolate :interpolate unless method_defined? :orig_interpolate | ||
| 29 | def interpolate(locale, string, values = {}) | ||
| 30 | result = orig_interpolate(locale, string, values) | ||
| 31 | translation = translation(string) | ||
| 32 | translation.nil? ? result : translation.replace(result) | ||
| 33 | end | ||
| 34 | |||
| 35 | def translation(result, meta = nil) | ||
| 36 | return unless result | ||
| 37 | |||
| 38 | case result | ||
| 39 | when Numeric | ||
| 40 | result | ||
| 41 | when String | ||
| 42 | result = Translation::Static.new(result) unless result.is_a? Translation::Static | ||
| 43 | result.set_meta meta | ||
| 44 | result | ||
| 45 | when Hash | ||
| 46 | Hash[*result.map do |key, value| | ||
| 47 | [key, translation(value, meta)] | ||
| 48 | end.flatten] | ||
| 49 | when Array | ||
| 50 | result.map do |value| | ||
| 51 | translation(value, meta) | ||
| 52 | end | ||
| 53 | else | ||
| 54 | raise "unexpected translation type: #{result.inspect}" | ||
| 55 | end | ||
| 56 | end | ||
| 57 | end | ||
| 58 | end | ||
| 59 | end \ No newline at end of file | ||
