In a Rails view, what does an _html suffix on a translation key change, and is t('reviews.thanks_html', name: @review.author_name) safe with user input?
answer
- markup belongs to the translator
- _html suffix or final html segment
- values escaped before interpolation
- html_safe values pass through
- bare I18n.t marks nothing
basics
~20 sAn _html suffix (or a final html segment) makes the t helper return an html_safe string, so markup written in the locale file renders. Interpolated values are HTML-escaped first, so passing user input stays safe.
solid answer
~40 sWhen the key ends in `_html` or its last segment is `html`, the `t` helper (in views, controllers and mailers) runs through `ActiveSupport::HtmlSafeTranslation`: it HTML-escapes every interpolation value, interpolates, and marks the result `html_safe`. So `t("reviews.thanks_html", name: @review.author_name)` keeps the `<b>` from the YAML and turns a malicious `<script>` in the name into inert text. Values that are already `html_safe`, such as `link_to` output, are interpolated verbatim, and a Numeric `count` is left alone. The protection lives only in the helper: `I18n.t("reviews.thanks_html")` returns a plain String that ERB escapes, and calling `raw` or `.html_safe` on a non-`_html` key that interpolates user input is how XSS gets in. A missing `_html` key falls back to the normal missing-translation output, never to trusted markup.
code
yaml · 4 linesfr:
reviews:
thanks_html: "Merci <b>%{name}</b> pour votre avis !"
cta_html: "Voir %{link}"go deeper
Recall that _html keys render their markup and that the t helper is what applies the rule.
Explain that interpolation values are escaped first, that safe helper output passes through, and why bare I18n.t does not apply the convention.
Show how you review locale files and templates for raw, html_safe and pre-marked input, and how you design keys for links inside sentences.
Weigh allowing markup in translations at all against translator workflow, review cost and the trust boundary locale files create.
## The problem the suffix solves Translators sometimes need markup inside a sentence: bold text, a line break, a link whose position differs by language. ERB escapes every String it outputs unless the String is marked **html_safe**, so markup in an ordinary translation shows up as visible `<b>` tags. Calling `raw` on the whole translation would fix the display but trust everything interpolated into it as well. The Rails convention is a **key-name marker**: a key ending in `_html`, or a key whose last segment is `html`, declares "this translation contains markup". ```yaml fr: reviews: thanks_html: "Merci <b>%{name}</b> pour votre avis !" cta: html: "Voir %{link}" ``` ## What the `t` helper does with such a key The view helper and the controller/mailer helper both route through `ActiveSupport::HtmlSafeTranslation`. For a key matching the `_html` / `.html` pattern it: 1. **Escapes every interpolation value** with `ERB::Util.html_escape`, except I18n's reserved options (`scope`, `default`, …) and a Numeric `count`. 2. **Interpolates** the escaped values into the translator's string. 3. **Marks the result `html_safe`**, so ERB outputs the markup as-is. | Value passed as `name:` | Output | |---|---| | `"Léa"` | `Merci <b>Léa</b> pour votre avis !` | | `"<script>alert(1)</script>"` | `Merci <b><script>alert(1)</script></b> …` | | `link_to("Le Pré Vert", campsite_path(c))` (already safe) | the link, interpolated verbatim | That last row matters: `html_escape` leaves already-safe strings untouched, so you can build links with helpers and drop them into the sentence through `%{link}` without the translator writing an `href`. ## Where the protection stops - **Bare `I18n.t` marks nothing.** `I18n.t("reviews.thanks_html")` returns an ordinary String; ERB escapes it and the `<b>` tags appear as text. Only the `t`/`translate` helpers apply the convention. - **Non-`_html` keys are not escaped at interpolation time.** `raw t("reviews.thanks", name: @review.author_name)` interpolates the raw name and then disables escaping for the whole string, an XSS hole. Rename the key to `_html` instead of reaching for `raw`. - **Calling `.html_safe` on user input before passing it** bypasses step 1, because safe values are trusted verbatim. - **Numbers other than `count`** are converted to strings and escaped like any other value, which is harmless; only a Numeric `count` skips the step, because it also selects the plural form. - **Both spellings qualify**: `t("reviews.cta.html")`, whose last segment is `html`, is treated exactly like `t("reviews.cta_html")`. - **Missing keys are not marked safe.** If the lookup fails, the helper returns its normal missing-translation output rather than a trusted string. - **Controller defaults are escaped.** In a controller, String `default:` values for an `_html` key are escaped before use, so a fallback cannot smuggle markup. ## Reviewing translations for safety 1. Treat locale files as code: markup in `_html` keys is trusted, so changes to them deserve the same review as template changes. 2. Keep `_html` keys rare and short; most copy needs no markup. 3. Pass links and formatted fragments as safe helper output, never as hand-built HTML strings. 4. Grep for `raw t(` and `t(...).html_safe` in reviews; both usually mean a key should have been named `_html`. ## A worked review A pull request adds `<%= t("reviews.thanks", name: @review.author_name).html_safe %>` because the French string contains `<b>`. The fix has three parts: 1. Rename the key to `reviews.thanks_html` in every locale file. 2. Drop `.html_safe` from the template; the helper now marks the result safe itself. 3. Confirm in a view test that an author name containing `<script>` renders escaped. The change removes a stored-XSS path without changing what translators write, which is exactly the trade the convention exists to make. ## Boundaries of this topic How `html_safe`, `SafeBuffer` and ERB escaping work in general is view-layer ground; this question is about the I18n naming convention and what the translation helper does with it. The senior signal is spotting that the suffix escapes values while trusting the template, and knowing the paths (bare `I18n.t`, `raw`, pre-marked input) where that guarantee does not apply.
- Why does <%= I18n.t('reviews.thanks_html') %> show the <b> tags as text?The `_html` convention lives in the `t` helpers through `ActiveSupport::HtmlSafeTranslation`. The bare `I18n.t` method knows nothing about it and returns a plain String, which ERB escapes. Use the helper in templates, or the controller's `t`, which applies the same rule.
- How do you put a link inside a translated sentence without the translator writing HTML attributes?Name the key with `_html`, leave a placeholder such as `%{link}`, and pass `link: link_to(...)`. The helper escapes plain values but passes already-safe ones verbatim, so the link renders and its text and URL come from code, not from the locale file.
- Values interpolated into an _html key are inserted raw, so user input there is an XSS risk.
- I18n.t('x_html') marks the result html_safe the same way the view helper does.
- raw t('reviews.thanks', name: input) is a fine alternative to renaming the key.
- Only keys ending exactly in _html qualify; a final html segment is escaped.
- Rails escapes the translator's markup inside _html keys too.