Module: I18nMultisite::Fallbacks

Defined in:
lib/i18n_multisite/fallbacks.rb

Instance Method Summary collapse

Instance Method Details

#extract_non_symbol_default!(options) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/i18n_multisite/fallbacks.rb', line 27

def extract_non_symbol_default!(options)
  defaults = [options[:default]].flatten
  first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)}
  if first_non_symbol_default
    options[:default] = defaults[0, defaults.index(first_non_symbol_default)]
  end
  return first_non_symbol_default
end

#translate(locale, key, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/i18n_multisite/fallbacks.rb', line 5

def translate(locale, key, options = {})
  return super if ::I18n.namespace.blank?
  default = extract_non_symbol_default!(options) if options[:default]

  scope     = Array.wrap(options.delete(:scope))
  namespace = Array.wrap(::I18n.namespace)

  (namespace.size + 1).times do |part|
    catch(:exception) do
      options[:scope] = (namespace | scope).reject(&:nil?)

      result = super(locale, key, options)
      result = super(locale, nil, options.merge(:default => default)) if result.nil? && default
      return result unless result.nil?
    end
    namespace.pop
  end

  return super(locale, nil, options.merge(:default => default)) if  default
  throw(:exception, ::I18n::MissingTranslation.new(locale, key, options))
end