Module: Link2::I18n

Defined in:
lib/link2/i18n.rb

Constant Summary collapse

ScopeInterpolationError =
Class.new(::Link2::Error)
VALID_SCOPE_VARIABLES =
[:controller, :action, :resource, :resources].freeze
INTERPOLATION_SYNTAX_PATTERN =
/(\\)?\{\{([^\}]+)\}\}/

Class Method Summary collapse

Class Method Details

.translate_with_scoping(action, resource, options = {}) ⇒ Object Also known as: translate, t

Helper method to lookup I18n keys based on a additionally known conditions; such as current action, model, etc.. This makes I18n translations more flexible and maintainable: Bottom-up approach; if no translation is found by first scope; try next scope, etc.

Scoped I18n lookup (default):

1. links.{{resource}}.{{action}}
2. links.{{action}}
...

Valid value interpolations:

  • resource - resource humanized name (parsed with I18n if possible), e.g. CaptainMorgan / @captain_morgan => “captain morgan”

  • resources - pluralized resource humanized name (parsed with I18n if possible), e.g. CaptainMorgan / @captain_morgan => “captain morgans”

  • name - current resource name to_s-value, e.g. @captain_morgan.to_s => “Captain Morgan with Cola and lime #4”

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/link2/i18n.rb', line 30

def translate_with_scoping(action, resource, options = {})
  raise ArgumentError, "At least action must be specified." unless action.present?
  resource_name = self.localized_resource_class_name(resource)
  i18n_options = options.merge(
    :scope => nil,
    :default => self.substituted_scopes_for(action, resource, options),
    :resource => resource_name,
    :resources => resource_name.pluralize
  )
  key = i18n_options[:default].shift
  i18n_options[:default] << action.to_s.humanize
  ::I18n.t(key, i18n_options)
end