Module: AbstractController::Translation
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | actionpack/lib/abstract_controller/translation.rb |
Instance Method Summary
-
#l(object, **options)
Alias for #localize.
-
#localize(object, **options)
(also: #l)
Delegates to
I18n.localize. -
#t(key, **options)
Alias for #translate.
-
#translate(key, **options)
(also: #t)
Delegates to
I18n.translate. - #controller_action_scope private
- #controller_scope private
- #scope_option_by_controller_path(scope) private
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.
# File 'actionpack/lib/abstract_controller/translation.rb', line 47
alias :l :localize
#localize(object, **options) Also known as: #l
Delegates to I18n.localize.
# File 'actionpack/lib/abstract_controller/translation.rb', line 44
def localize(object, **) I18n.localize(object, **) 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.
# 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").
# File 'actionpack/lib/abstract_controller/translation.rb', line 23
def translate(key, **) if key&.start_with?(".") defaults = [:"#{controller_scope}#{key}"] defaults << [:default] if [:default] [:default] = defaults.flatten key = "#{controller_action_scope}#{key}" elsif (scope = [:scope]) [:scope] = scope_option_by_controller_path(scope) end if [:default] && ActiveSupport::HtmlSafeTranslation.html_safe_translation_key?(key) [:default] = Array([:default]).map do |value| value.is_a?(String) ? ERB::Util.html_escape(value) : value end end ActiveSupport::HtmlSafeTranslation.translate(key, **) end