123456789_123456789_123456789_123456789_123456789_

Module: AbstractController::Translation

Instance Method Summary

Instance Method Details

#controller_action_scope (private)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 54

def controller_action_scope
  "#{controller_scope}.#{action_name}"
end

#controller_scope (private)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 50

def controller_scope
  controller_path.tr("/", ".")
end

#l(object, **options)

Alias for #localize.

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 47

alias :l :localize

#localize(object, **options) Also known as: #l

Delegates to I18n.localize.

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 44

def localize(object, **options)
  I18n.localize(object, **options)
end

#scope_option_by_controller_path(scope) (private)

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 58

def scope_option_by_controller_path(scope)
  if (scope.is_a?(String) || scope.is_a?(Symbol)) && scope.start_with?(".")
    resolved = "#{controller_action_scope}#{scope}"
    scope.is_a?(Symbol) ? resolved.to_sym : resolved
  else
    scope
  end
end

#t(key, **options)

Alias for #translate.

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 41

alias :t :translate

#translate(key, **options) Also known as: #t

Delegates to I18n.translate.

When the key or the :scope option starts with a period, it will be scoped by the current controller and action. So if you call translate(".foo") from PeopleController#index, it will convert the call to I18n.translate("people.index.foo"); calling translate("bar", scope: ".foo") converts to I18n.translate("bar", scope: "people.index.foo"). This makes it less repetitive to translate many keys within the same controller / action and gives you a simple framework for scoping them consistently. The key takes precedence: if the key itself starts with a period, the :scope option's own leading period is left as-is rather than expanded, preserving the pre-existing behavior for calls like translate(".bar", scope: ".foo").

[ GitHub ]

  
# File 'actionpack/lib/abstract_controller/translation.rb', line 23

def translate(key, **options)
  if key&.start_with?(".")
    defaults = [:"#{controller_scope}#{key}"]
    defaults << options[:default] if options[:default]
    options[:default] = defaults.flatten
    key = "#{controller_action_scope}#{key}"
  elsif (scope = options[:scope])
    options[:scope] = scope_option_by_controller_path(scope)
  end

  if options[:default] && ActiveSupport::HtmlSafeTranslation.html_safe_translation_key?(key)
    options[:default] = Array(options[:default]).map do |value|
      value.is_a?(String) ? ERB::Util.html_escape(value) : value
    end
  end

  ActiveSupport::HtmlSafeTranslation.translate(key, **options)
end