Module: Hanami::Helpers::I18nHelper
- Included in:
- Extensions::View::StandardHelpers
- Defined in:
- lib/hanami/helpers/i18n_helper.rb
Overview
Helper methods for translating and localizing content using the slice’s i18n backend.
These helpers will be automatically available in your view templates, part classes, and scope classes when the ‘i18n` gem is bundled.
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 ‘html` or 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, Hanami::View::HTML::SafeString
(also: #t)
Translates the given key using the slice’s i18n backend.
-
#translate!(key, **options) ⇒ String, Hanami::View::HTML::SafeString
(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.
128 129 130 |
# File 'lib/hanami/helpers/i18n_helper.rb', line 128 def localize(object, **) _context.i18n.localize(object, **) end |
#translate(key, **options) ⇒ String, Hanami::View::HTML::SafeString 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 currently-rendering template and expanded against its name (slashes become dots; partial basenames keep their leading underscore). For example, `translate(“.title”)` inside `users/index.html.erb` resolves to `users.index.title`, and `translate(“.label”)` inside `users/_form.html.erb` resolves to `users._form.label`. Raises `I18n::ArgumentError` if called outside a template render.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/hanami/helpers/i18n_helper.rb', line 69 def translate(key, **) key = _resolve_i18n_key(key) html_safe = _html_safe_translation_key?(key) = () if html_safe result = if .key?(:default) || [:raise] _context.i18n.translate(key, **) else begin _context.i18n.translate(key, **, raise: true) rescue ::I18n::MissingTranslationData => exception return _missing_translation_markup(key, exception) end end html_safe ? result.to_s.html_safe : result end |
#translate!(key, **options) ⇒ String, Hanami::View::HTML::SafeString Also known as: t!
Translates the given key, raising an exception if the translation is missing.
107 108 109 |
# File 'lib/hanami/helpers/i18n_helper.rb', line 107 def translate!(key, **) translate(key, **, raise: true) end |