Class: I18n::Backend::Chain
- Includes:
- Base
- Defined in:
- lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb
Overview
Backend that chains multiple other backends and checks each of them when a translation needs to be looked up. This is useful when you want to use standard translations with a Simple backend but store custom application translations in a database or other backends.
To use the Chain backend instantiate it and set it to the I18n module. You can add chained backends through the initializer or backends accessor:
# preserves the existing Simple backend set to I18n.backend
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n.backend)
The implementation assumes that all backends added to the Chain implement a lookup method with the same API as Simple backend does.
Constant Summary
Constants included from Base
Base::DEPRECATED_INTERPOLATION_SYNTAX_PATTERN, Base::INTERPOLATION_SYNTAX_PATTERN, Base::RESERVED_KEYS, Base::RESERVED_KEYS_PATTERN
Constants included from Transliterator
Transliterator::DEFAULT_REPLACEMENT_CHAR
Instance Attribute Summary collapse
-
#backends ⇒ Object
Returns the value of attribute backends.
Instance Method Summary collapse
- #available_locales ⇒ Object
-
#initialize(*backends) ⇒ Chain
constructor
A new instance of Chain.
- #localize(locale, object, format = :default, options = {}) ⇒ Object
- #reload! ⇒ Object
- #store_translations(locale, data, options = {}) ⇒ Object
- #translate(locale, key, options = {}) ⇒ Object
Methods included from Base
Methods included from Transliterator
Constructor Details
#initialize(*backends) ⇒ Chain
Returns a new instance of Chain.
24 25 26 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 24 def initialize(*backends) self.backends = backends end |
Instance Attribute Details
#backends ⇒ Object
Returns the value of attribute backends.
22 23 24 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 22 def backends @backends end |
Instance Method Details
#available_locales ⇒ Object
36 37 38 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 36 def available_locales backends.map { |backend| backend.available_locales }.flatten.uniq end |
#localize(locale, object, format = :default, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 61 def localize(locale, object, format = :default, = {}) backends.each do |backend| begin result = backend.localize(locale, object, format, ) and return result rescue MissingTranslationData end end raise(I18n::MissingTranslationData.new(locale, format, )) end |
#reload! ⇒ Object
28 29 30 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 28 def reload! backends.each { |backend| backend.reload! } end |
#store_translations(locale, data, options = {}) ⇒ Object
32 33 34 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 32 def store_translations(locale, data, = {}) backends.first.store_translations(locale, data, = {}) end |
#translate(locale, key, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_support/vendor/i18n-0.4.1/i18n/backend/chain.rb', line 40 def translate(locale, key, = {}) return key.map { |k| translate(locale, k, ) } if key.is_a?(Array) default = .delete(:default) namespace = {} backends.each do |backend| begin .update(:default => default) if default and backend == backends.last translation = backend.translate(locale, key, ) if namespace_lookup?(translation, ) namespace.update(translation) elsif !translation.nil? return translation end rescue MissingTranslationData end end return namespace unless namespace.empty? raise(I18n::MissingTranslationData.new(locale, key, )) end |