Class: I18n::Backend::Chain

Inherits:
Base
  • Object
show all
Defined in:
lib/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 inherited from Base

Base::INTERPOLATION_SYNTAX_PATTERN, Base::RESERVED_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialized?, #load_translations, #translate

Constructor Details

#initialize(*backends) ⇒ Chain

Returns a new instance of Chain.



20
21
22
# File 'lib/i18n/backend/chain.rb', line 20

def initialize(*backends)
  self.backends = backends
end

Instance Attribute Details

#backendsObject

Returns the value of attribute backends.



18
19
20
# File 'lib/i18n/backend/chain.rb', line 18

def backends
  @backends
end

Instance Method Details

#available_localesObject



32
33
34
# File 'lib/i18n/backend/chain.rb', line 32

def available_locales
  backends.map { |backend| backend.available_locales }.flatten.uniq
end

#localize(locale, object, format = :default) ⇒ Object



36
37
38
39
40
# File 'lib/i18n/backend/chain.rb', line 36

def localize(locale, object, format = :default)
  backends.each do |backend|
    result = backend.localize(locale, object, format) and return result
  end
end

#reload!Object



24
25
26
# File 'lib/i18n/backend/chain.rb', line 24

def reload!
  backends.each { |backend| backend.reload! }
end

#store_translations(locale, data) ⇒ Object



28
29
30
# File 'lib/i18n/backend/chain.rb', line 28

def store_translations(locale, data)
  backends.first.store_translations(locale, data)
end