instance method
translate
Ruby on Rails edge
Since v3.0.20Signature
translate(key, **options)
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”)`.
Parameters
-
keyreq -
optionskeyrest
Source
# File actionpack/lib/abstract_controller/translation.rb, line 21
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
Defined in actionpack/lib/abstract_controller/translation.rb line 21
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in AbstractController::Translation