Class: Air18n::LessSillyChain

Inherits:
I18n::Backend::Chain
  • Object
show all
Defined in:
lib/air18n/less_silly_chain.rb

Overview

I18n::Backend::Chain has the rather silly behavior that options is removed from the options hash for all Backends except for the last. It’s not clear why, know why because this behavior is not documented. LessSillyChain works just like Backend::Chain but without this behavior.

Instance Method Summary collapse

Instance Method Details

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/air18n/less_silly_chain.rb', line 9

def translate(locale, key, options = {})
  namespace = nil

  rescued_exception = nil
  caught_exception = nil
  backends.each do |backend|
    rescued_exception = nil
    caught_exception = nil

    # We jump through two hoops to make this work with both i18n-0.5.0 and
    # i18n-0.6.0.
    #   - i18n-0.5.0: Backends raise TranslationMissingData exception when
    #     a translation is missing
    #   - i18n-0.6.0: Backends throw TranslationMissing "exceptions" to the
    #     :exception tag when a translation is missing.
    #
    # To make both happy, we both rescue and catch.
    caught_exception = catch(:exception) do
      begin
        translation = backend.translate(locale, key, options.merge(:raise => true))
        if namespace_lookup?(translation, options)
          namespace ||= {}
          namespace.merge!(translation)
        elsif !translation.nil?
          return translation
        end
      rescue Exception => e
        rescued_exception = e
      end

      # Nothing caught.
      nil
    end
  end

  return namespace if namespace

  if caught_exception
    throw(:exception, caught_exception)
  end
  if rescued_exception
    raise(rescued_exception)
  end
end