Class: Globalize::Backend::Chain
- Inherits:
-
Object
- Object
- Globalize::Backend::Chain
- Defined in:
- lib/globalize/backend/chain.rb
Instance Method Summary collapse
-
#add(*backends) ⇒ Object
Change this to a) accept any number of backends and b) accept classes.
-
#initialize(*args) ⇒ Chain
constructor
A new instance of Chain.
- #load_translations(*args) ⇒ Object
- #localize(locale, object, format = :default) ⇒ Object
-
#translate(locale, key, options = {}) ⇒ Object
For defaults: Never pass any default option to the backends but instead implement our own default mechanism (e.g. symbols as defaults would need to be passed to the whole chain to be translated).
Constructor Details
#initialize(*args) ⇒ Chain
Returns a new instance of Chain.
12 13 14 |
# File 'lib/globalize/backend/chain.rb', line 12 def initialize(*args) add(*args) unless args.empty? end |
Instance Method Details
#add(*backends) ⇒ Object
Change this to a) accept any number of backends and b) accept classes. When classes are passed instantiate them and add the instances as backends. Return the added backends from #add.
Add an initialize method that accepts the same arguments and passes them to #add, so we could:
I18n.backend = Globalize::Backend::Chain.new(Globalize::Backend::Foo, Globalize::Backend::Bar)
Globalize::Backend::Chain.new(:foo, :bar)
Globalize.chain_backends :foo, :bar
25 26 27 28 29 30 31 |
# File 'lib/globalize/backend/chain.rb', line 25 def add(*backends) backends.each do |backend| backend = Globalize::Backend.const_get(backend.to_s.capitalize) if backend.is_a? Symbol backend = backend.new if backend.is_a? Class self.backends << backend end end |
#load_translations(*args) ⇒ Object
33 34 35 |
# File 'lib/globalize/backend/chain.rb', line 33 def load_translations(*args) backends.each{|backend| backend.load_translations(*args) } end |
#localize(locale, object, format = :default) ⇒ Object
74 75 76 77 78 |
# File 'lib/globalize/backend/chain.rb', line 74 def localize(locale, object, format = :default) backends.each do |backend| result = backend.localize(locale, object, format) and return result end end |
#translate(locale, key, options = {}) ⇒ Object
For defaults: Never pass any default option to the backends but instead implement our own default mechanism (e.g. symbols as defaults would need to be passed to the whole chain to be translated).
For namespace lookup: Only return if the result is not a hash OR count is not present, otherwise merge them. So in effect the count variable would control whether we have a namespace lookup or a pluralization going on.
Exceptions: Make sure that we catch MissingTranslationData exceptions and raise one in the end when no translation was found at all.
For bulk translation: If the key is an array we need to call #translate for each of the keys and collect the results.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/globalize/backend/chain.rb', line 55 def translate(locale, key, = {}) raise I18n::InvalidLocale.new(locale) if locale.nil? return key.map{|k| translate locale, k, } if key.is_a? Array default = .delete(:default) result = backends.inject({}) do |namespace, backend| begin translation = backend.translate(locale.to_sym, key, ) if namespace_lookup?(translation, ) namespace.merge! translation elsif translation return translation end rescue I18n::MissingTranslationData end end result || default(locale, default, ) || raise(I18n::MissingTranslationData.new(locale, key, )) end |