Module: Hanami::Helpers::I18nHelper::Methods
- Included in:
- Extensions::Action::I18nHelper, Hanami::Helpers::I18nHelper
- Defined in:
- lib/hanami/helpers/i18n_helper.rb
Overview
Shared translate / localize (and shorthand) helper methods used by both view-layer
consumers and action-layer consumers.
This module is abstract. Including modules must override two private hooks:
- #_i18n — returns the i18n backend to delegate to.
- #_resolve_i18n_key — expands relative (leading-dot) keys against the consumer's context, and is a no-op for absolute keys.
Constant Summary collapse
- HTML_SAFE_TRANSLATION_KEY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Matches keys whose final segment is
htmlor whose final segment ends in_html. These translated values are treated as HTML-safe and any string interpolation values are HTML-escaped before substitution. /(\b|_)html\z/
Instance Method Summary collapse
-
#localize(object, **options) ⇒ String
(also: #l)
Localizes the given object (e.g. a date, time, or number) using the slice's i18n backend.
-
#translate(key, **options) ⇒ String
(also: #t)
Translates the given key using the slice's i18n backend.
-
#translate!(key, **options) ⇒ String
(also: #t!)
Translates the given key, raising an exception if the translation is missing.
Instance Method Details
#localize(object, **options) ⇒ String Also known as: l
Localizes the given object (e.g. a date, time, or number) using the slice's i18n backend.
140 141 142 |
# File 'lib/hanami/helpers/i18n_helper.rb', line 140 def localize(object, **) _i18n.localize(object, **) end |
#translate(key, **options) ⇒ String Also known as: t
Translates the given key using the slice's i18n backend.
When the key's final segment is html or ends in _html, the result is marked HTML-safe
and any string interpolation values are HTML-escaped first.
When a translation is missing and neither :default nor :raise was supplied, returns a
<span class="translation_missing"> element containing the missing key, useful for
spotting missing translations during development.
When the key begins with a ., it is treated as relative to the consumer's context and
expanded by #_resolve_i18n_key.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/hanami/helpers/i18n_helper.rb', line 86 def translate(key, **) key = _resolve_i18n_key(key) html_safe = _html_safe_translation_key?(key) = () if html_safe result = if .key?(:default) || [:raise] _i18n.translate(key, **) else begin _i18n.translate(key, **, raise: true) rescue ::I18n::MissingTranslationData => exception return _missing_translation_markup(key, exception) end end html_safe ? _i18n_mark_html_safe(result.to_s) : result end |
#translate!(key, **options) ⇒ String Also known as: t!
Translates the given key, raising an exception if the translation is missing.
122 123 124 |
# File 'lib/hanami/helpers/i18n_helper.rb', line 122 def translate!(key, **) translate(key, **, raise: true) end |