Module: Hanami::Helpers::I18nHelper

Includes:
Methods
Included in:
Extensions::View::StandardHelpers
Defined in:
lib/hanami/helpers/i18n_helper.rb

Overview

View-layer translation and localization helpers.

These helpers are automatically available in your view templates, part classes, and scope classes when the i18n gem is bundled. They provide translate/t, translate!/t!, and localize/l, sourcing the i18n backend from the view context and expanding relative (leading-dot) translation keys against the currently-rendering template name.

The shared translate/localize logic lives in Methods, which is also included by the action-layer i18n helper (Extensions::Action::I18nHelper). This module supplies the two view-specific concrete implementations of Methods's abstract hooks: #_i18n returns the i18n backend from the view context, and #_resolve_i18n_key expands relative keys against the template name.

Examples:

Basic translation

<%= translate("messages.welcome") %>
# => "Welcome"

HTML-safe translation

# en.yml
#   greeting_html: "Hello, <strong>%{name}</strong>!"
<%= translate("greeting_html", name: "<script>") %>
# => "Hello, <strong>&lt;script&gt;</strong>!" (marked HTML-safe)

Missing translation

<%= translate("missing.key") %>
# => 'missing.key'

Relative key lookup

# In app/templates/users/index.html.erb:
<%= translate(".title") %>
# Looks up "users.index.title"

# In app/templates/users/_form.html.erb (a partial):
<%= translate(".label") %>
# Looks up "users._form.label"

Since:

  • 3.0.0

Defined Under Namespace

Modules: Methods

Constant Summary

Constants included from Methods

Methods::HTML_SAFE_TRANSLATION_KEY

Method Summary

Methods included from Methods

#localize, #translate, #translate!