Module: I18n::Message::Cascade

Defined in:
lib/i18n/message/cascade.rb

Overview

Encapsulates the pattern of looking up a translation key from several scopes.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
# File 'lib/i18n/message/cascade.rb', line 10

def self.included(base)
  base.class_eval do
    cattr_accessor :cascade_options # FIXME use inheritable_attribute_accessor
    self.cascade_options = { :step => 2, :skip_root => true }
  end
end

Instance Method Details

#optionsObject



17
18
19
# File 'lib/i18n/message/cascade.rb', line 17

def options
  super.update(:cascade => self.class.cascade_options)
end

#scopeObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/i18n/message/cascade.rb', line 21

def scope
  scopes = [super] + Array(self.class.cascade_options[:scopes]).map do |scope|
    if options[scope]
      value = options[scope]
      value = value.class.name unless value.is_a?(String) || value.is_a?(Symbol)
      "#{scope}s.#{value.to_s.underscore}"
    end
  end
  scopes.compact.join('.')
end