Module: I18n::InterpolateNested

Defined in:
lib/i18n/interpolate_nested/railtie.rb,
lib/i18n/interpolate_nested/version.rb,
lib/i18n/interpolate_nested.rb

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.initvoid

This method returns an undefined value.

Hooks into I18n and enables nested values when interpolating. This method is idempotent.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/i18n/interpolate_nested.rb', line 12

def self.init
  return if defined?(@@initialized)

  original_handler = I18n.config.missing_interpolation_argument_handler
  I18n.config.missing_interpolation_argument_handler = ->(key, values, string) do
    keys = key.to_s.split(".")

    if keys.length > 1
      keys.reduce(values) do |vs, k|
        break original_handler.call(key, values, string) if vs.nil?
        vs[k.to_sym]
      end
    else
      original_handler.call(key, values, string)
    end
  end

  pattern = Regexp.union(I18n::INTERPOLATION_PATTERN, /%\{[\w.]+\}/)
  I18n.send(:remove_const, :INTERPOLATION_PATTERN)
  I18n.const_set(:INTERPOLATION_PATTERN, pattern)

  @@initialized = true
end