module I18n::Backend::Chain::Implementation

Attributes

backends[RW]

Public Class Methods

new(*backends) click to toggle source
# File lib/i18n/backend/chain.rb, line 27
def initialize(*backends)
  self.backends = backends
end

Public Instance Methods

available_locales() click to toggle source
# File lib/i18n/backend/chain.rb, line 52
def available_locales
  backends.map { |backend| backend.available_locales }.flatten.uniq
end
eager_load!() click to toggle source
# File lib/i18n/backend/chain.rb, line 44
def eager_load!
  backends.each { |backend| backend.eager_load! }
end
exists?(locale, key) click to toggle source
# File lib/i18n/backend/chain.rb, line 76
def exists?(locale, key)
  backends.any? do |backend|
    backend.exists?(locale, key)
  end
end
initialized?() click to toggle source
# File lib/i18n/backend/chain.rb, line 31
def initialized?
  backends.all? do |backend|
    backend.instance_eval do
      return false unless initialized?
    end
  end
  true
end
localize(locale, object, format = :default, options = EMPTY_HASH) click to toggle source
# File lib/i18n/backend/chain.rb, line 82
def localize(locale, object, format = :default, options = EMPTY_HASH)
  backends.each do |backend|
    catch(:exception) do
      result = backend.localize(locale, object, format, options) and return result
    end
  end
  throw(:exception, I18n::MissingTranslation.new(locale, format, options))
end
reload!() click to toggle source
# File lib/i18n/backend/chain.rb, line 40
def reload!
  backends.each { |backend| backend.reload! }
end
store_translations(locale, data, options = EMPTY_HASH) click to toggle source
# File lib/i18n/backend/chain.rb, line 48
def store_translations(locale, data, options = EMPTY_HASH)
  backends.first.store_translations(locale, data, options)
end
translate(locale, key, default_options = EMPTY_HASH) click to toggle source
# File lib/i18n/backend/chain.rb, line 56
def translate(locale, key, default_options = EMPTY_HASH)
  namespace = nil
  options = default_options.except(:default)

  backends.each do |backend|
    catch(:exception) do
      options = default_options if backend == backends.last
      translation = backend.translate(locale, key, options)
      if namespace_lookup?(translation, options)
        namespace = _deep_merge(translation, namespace || {})
      elsif !translation.nil? || (options.key?(:default) && options[:default].nil?)
        return translation
      end
    end
  end

  return namespace if namespace
  throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end

Protected Instance Methods

init_translations() click to toggle source
# File lib/i18n/backend/chain.rb, line 92
def init_translations
  backends.each do |backend|
    backend.send(:init_translations)
  end
end
namespace_lookup?(result, options) click to toggle source
# File lib/i18n/backend/chain.rb, line 109
def namespace_lookup?(result, options)
  result.is_a?(Hash) && !options.has_key?(:count)
end
translations() click to toggle source
# File lib/i18n/backend/chain.rb, line 98
def translations
  backends.reverse.each_with_object({}) do |backend, memo|
    partial_translations = backend.instance_eval do
      init_translations unless initialized?
      translations
    end

    memo.deep_merge!(partial_translations)
  end
end

Private Instance Methods

_deep_merge(hash, other_hash) click to toggle source

This is approximately what gets used in ActiveSupport. However since we are not guaranteed to run in an ActiveSupport context it is wise to have our own copy. We underscore it to not pollute the namespace of the including class.

# File lib/i18n/backend/chain.rb, line 118
def _deep_merge(hash, other_hash)
  copy = hash.dup
  other_hash.each_pair do |k,v|
    value_from_other = hash[k]
    copy[k] = value_from_other.is_a?(Hash) && v.is_a?(Hash) ? _deep_merge(value_from_other, v) : v
  end
  copy
end