Class: I18n::Backend::Chain

Inherits:
Simple show all
Defined in:
activesupport/lib/active_support/vendor/i18n-0.3.7/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

Constant Summary

Constants included from Base

Base::INTERPOLATION_SYNTAX_PATTERN, Base::RESERVED_KEYS

Constants included from Helpers

Helpers::SEPARATOR_ESCAPE_CHAR

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Base

#initialized?, #load_translations

Methods included from Helpers

#deep_symbolize_keys, #escape_default_separator, #unescape_default_separator, #unwind_keys, #wind_keys

Constructor Details

- (Chain) initialize(*backends)

Returns a new instance of Chain



22
23
24
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 22

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

Instance Attribute Details

- (Object) backends

Returns the value of attribute backends



20
21
22
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 20

def backends
  @backends
end

Instance Method Details

- (Object) available_locales



34
35
36
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 34

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

- (Object) localize(locale, object, format = :default, options = {})



59
60
61
62
63
64
65
66
67
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 59

def localize(locale, object, format = :default, options = {})
  backends.each do |backend|
    begin
      result = backend.localize(locale, object, format, options) and return result
    rescue MissingTranslationData
    end
  end
  raise(I18n::MissingTranslationData.new(locale, format, options))
end

- (Object) reload!



26
27
28
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 26

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

- (Object) store_translations(locale, data, options = {})



30
31
32
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 30

def store_translations(locale, data, options = {})
  backends.first.store_translations(locale, data, options = {})
end

- (Object) translate(locale, key, options = {})



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'activesupport/lib/active_support/vendor/i18n-0.3.7/i18n/backend/chain.rb', line 38

def translate(locale, key, options = {})
  return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)

  default = options.delete(:default)
  namespace = {}
  backends.each do |backend|
    begin
      options.update(:default => default) if default and backend == backends.last
      translation = backend.translate(locale, key, options)
      if namespace_lookup?(translation, options)
        namespace.update(translation)
      elsif !translation.nil?
        return translation
      end
    rescue MissingTranslationData
    end
  end
  return namespace unless namespace.empty?
  raise(I18n::MissingTranslationData.new(locale, key, options))
end