ActionView::Helpers::TranslationHelper

Alias: t

def translate(key, **options, &block)

Description

Looks up and returns the translation for a key.

Arguments

Changing a value updates the ERB and HTML.

The key to look up.
  • Dots separate levels. A symbol works the same way.
  • An array returns an array of the translations for each key. A block is ignored.
  • A key starting with . (".title") is appended to a key built from the template's path (articles.index.title in app/views/articles/index.html.erb). This site renders without a template file, so this page cannot try it (it raises RuntimeError).
What is returned depends on the key and the translation:
  • If the key ends in html (_html, .html, or just html), the returned string is marked html_safe, so <%= %> does not escape it.
  • If no translation is found, returns <span class="translation_missing">. Its content is the last part of the key run through titleize, and its title lists the key with the locale and the given options (except :scope). With config.action_view.debug_missing_translation set to false, returns the same string as the title, without the <span>.
  • If the translation is a hash (the key has keys under it, like "messages"), returns the hash as is, so <%= %> converts it to a string and escapes it. With :count, a translation is picked from the hash.
= nil
Used when no translation is found for the key. Rails takes it out of the options hash and uses it as follows:
  • A string is returned (with %{…} filled in from the options). It is not marked html_safe even if the key ends in html, so <%= %> escapes it.
  • A symbol is looked up as a key. If that key ends in html, its translation is marked html_safe.
  • An array is tried in order, and the first translation found is returned. A string in it is returned when reached.
nil returns nil (outputs nothing) when no translation is found.
options= {}
"<i>Alice</i>"
The value for %{name} in the translation (I18n interpolation). Any key that is not an I18n option name fills the %{…} with the same name. If the key ends in html, the value is escaped first. With no interpolation values at all, %{name} is returned as is. If any value is given and a %{…} in the translation has none, raises I18n::MissingInterpolationArgument.
3
If the translation is a hash with one and other keys ("messages"), returns the value of one (messages.one) when the number is 1, and the value of other (messages.other) otherwise (I18n's default rule). The number fills %{count} in the value. If the count is 0 and the translation has a zero key, its value is used.
[:articles, :index]
Prepends these levels to the key. A string ("articles.index") works too. When no translation is found, the title shows the key with these levels, and does not list :scope.
Looks up the translation in this locale instead of the current one.
What happens when no translation is found:
  • true: raises I18n::MissingTranslationData.
  • false: returns the string I18n returns, starting with Translation missing:, instead of the <span>.
  • Omitted: the same as true when config.i18n.raise_on_missing_translations is true.
In apps where config.i18n.raise_on_missing_translations is true, it raises even with false.
Other options that I18n's translate accepts (separator, etc.) are passed to I18n as is. Any other key, like :name, becomes a value for a %{…} in the translation. None of them become HTML attributes.
&block= nil
A block receives the translation and the key, and its return value is returned. If no translation is found, the block receives <span class="translation_missing"> instead of the translation. The key is the one whose translation was found (the symbol, if a default symbol was found). If nothing was found, or a default string was used, it is the key argument.

ERB

Changes from the previous output: Added Removed Changed

config/locales/examples/en.yml
en:
  hello: "Hello"
  greeting: "Hello, %{name}"
  welcome_html: "Welcome, <b>%{name}</b>"
  messages:
    one: "You have 1 message"
    other: "You have %{count} messages"
  articles:
    index:
      title: "Articles"

          

HTML


          

Preview