In Rails 8.1, what happens when a t() key is missing, and how do default: and config.i18n.raise_on_missing_translations change it?
answer
- soft miss by default
- default: symbols are keys, strings literal
- commented out in development.rb and test.rb
- since 7.1 any I18n.t raises
- :strict adds human_attribute_name
basics
~20 sA missing key is soft by default: the view t renders a translation_missing span and I18n.t returns 'Translation missing: …'. default: tries other keys or literal text; raise_on_missing_translations = true raises I18n::MissingTranslationData, since Rails 7.1 for any I18n.t.
solid answer
~40 sWith nothing configured, a miss never raises: the view helper `t` renders `<span class="translation_missing">` with the titleized last segment, and `I18n.t` returns the string `"Translation missing: fr.campsites.show.heading"`. `default:` gives the lookup alternatives: a Symbol is another key, an Array is tried in order, and the first String is returned as literal text (still interpolated). `config.i18n.raise_on_missing_translations = true`, left commented out in the generated `development.rb` and `test.rb`, turns misses into `I18n::MissingTranslationData`; since Rails 7.1 that covers every `I18n.t` call, not just the view and controller helpers, because Rails swaps in a raising `I18n.exception_handler`. The value `:strict` also makes `human_attribute_name` raise. Errors other than a miss, such as `I18n::MissingInterpolationArgument`, always raise.
code
ruby · 11 lines# config/environments/test.rb
Rails.application.configure do
config.i18n.raise_on_missing_translations = true
end
# app/models/review.rb
class Review < ApplicationRecord
def badge
I18n.t("reviews.badge.top_rated") # raises I18n::MissingTranslationData in test if absent
end
endgo deeper
Recall that a missing key shows a placeholder by default and that default: can supply fallback text or another key.
Explain Symbol versus String defaults, the generated commented-out setting, and what changed in Rails 7.1 for plain I18n.t.
Show how you make CI fail on missing keys without breaking production pages, and why String defaults and fallbacks hide gaps.
Weigh strict raising, custom exception handlers and key-coverage tooling as the translation catalogue and the number of locales grow.
## The default: a soft miss Rails 8.1 treats a missing translation as something to show, not to crash on. The behaviour depends on which entry point you call: | Call | Missing key result (default config) | |---|---| | view `t(".heading")` | `<span class="translation_missing" title="translation missing: fr.campsites.show.heading">Heading</span>` | | controller or mailer `t(".created")` | a `"Translation missing. Options considered were: …"` string listing the action key and the action-less key | | `I18n.t("campsites.badge")` anywhere | the string `"Translation missing: fr.campsites.badge"` | | `I18n.t!("campsites.badge")` or `t(..., raise: true)` | raises `I18n::MissingTranslationData` | The soft result comes from the i18n gem's default `I18n::ExceptionHandler`, which turns a `MissingTranslation` into its message. The view helper goes one step further and wraps it in a span so the page still reads and the `title` names the exact key. ## `default:` — alternatives before giving up `default:` tells the lookup what to try next: - a **Symbol** is another key: `t(".heading", default: :"campsites.heading")`; - an **Array** is tried in order, Symbols as keys, until one resolves; - the first **String** in the chain is returned as literal text and ends the search; - interpolation still applies, so `default: "%{count} avis"` gets its count. ```ruby t(".heading", default: [:"campsites.heading", "Camping"]) # 1. campsites.show.heading 2. campsites.heading 3. "Camping" ``` A default prevents the miss, so it also prevents raising when raise-on-missing is on. That makes a String default a quiet way to hide gaps; prefer Symbol defaults that point at real keys. ## `config.i18n.raise_on_missing_translations` The generated environment files carry the line commented out: ```ruby # config/environments/test.rb config.i18n.raise_on_missing_translations = true ``` What each value does in Rails 8.1: 1. **`false`** (the default): soft misses as above. 2. **`true`**: the view and controller helpers raise `I18n::MissingTranslationData`, and Rails replaces the gem's default exception handler with one that raises, so plain `I18n.t` in a model, job or service raises too. Before Rails 7.1 only the helpers raised; the upgrade guide calls out this change. 3. **`:strict`**: everything `true` does, plus `ActiveModel::Translation#human_attribute_name` raises instead of falling back to a humanized attribute name. Rails only swaps the handler when it is still the gem's default. If you have set your own `I18n.exception_handler`, Rails leaves it alone, and the guide recommends that route for finer rules, such as raising for everything except one legacy key. Retired switches show up in old answers: `config.action_view.raise_on_missing_translations` was removed in Rails 7.0, and `AbstractController::Translation.raise_on_missing_translations` was a private API removed in 7.1. Both are replaced by the single `config.i18n` setting. ## Errors that are not misses Several I18n errors bypass the soft path and raise regardless of configuration: - **`I18n::MissingInterpolationArgument`**: the string has `%{name}` and no `name:` was passed. - **`I18n::ReservedInterpolationKey`**: the string uses a reserved name such as `%{scope}` or `%{default}`. - **`I18n::InvalidPluralizationData`**: `count:` selected a plural subkey that is missing. - **`I18n::InvalidLocale`**: the requested locale is not in `I18n.available_locales`. These raise because they signal a **broken** translation rather than an absent one: the key exists but cannot be rendered with the data supplied. Treat each as a bug in either the call site or the locale file, and expect it to surface in whichever environment first renders that string, which is why a test that renders each page in each locale catches more than a key-diff script alone. ## Checking before you translate Two smaller tools round this out: - **`I18n.exists?("campsites.promo", :fr)`** returns true or false without triggering any handler, which suits optional copy such as a seasonal banner that only some locales provide. - **`ActionView::Base.debug_missing_translation`** (default `true`) controls the view marker; set to `false`, the helper returns the bare "translation missing: …" text instead of the span, which some teams prefer in production HTML. Neither replaces raise-on-missing in tests; they decide how a known, accepted gap is presented. ## How teams use this The usual setup is raise-on-missing in **test** (so CI fails on a missing key) and often in development, and soft misses in production. Production in a new app also has fallbacks on, so a missing French key shows English there; tests are what keep that from hiding gaps. An interviewer is checking that you know a miss is silent by default, that `default:` resolves it, and that the 7.1 change widened what the setting covers.
- Why can raise_on_missing_translations = true still let a missing key reach production unnoticed?Any `default:` that ends in a String satisfies the lookup, so nothing raises; and the generated production.rb enables fallbacks, so a missing French key quietly shows English. Run raise-on-missing in test, prefer Symbol defaults, and add a check that compares keys across locale files.
- How do you raise on missing keys everywhere except a handful of legacy ones?Leave `config.i18n.raise_on_missing_translations` false and set `I18n.exception_handler` to a callable taking `(exception, locale, key, options)` that raises unless the key is on an allow list. Rails only installs its raising handler when the gem's default handler is still in place.
- What does I18n.t! do differently from I18n.t?`I18n.t!` is `translate` with `raise: true`, so a miss raises `I18n::MissingTranslationData` regardless of configuration. It is useful in code that must never show a placeholder, such as building an email subject in a job.
- In Rails 8.1, raise_on_missing_translations only affects t inside views and controllers.
- A String in default: is looked up as another translation key.
- Missing %{name} arguments render a translation_missing span like a missing key.
- config.action_view.raise_on_missing_translations is the current switch for views.
- New Rails apps raise on missing translations in development out of the box.
Assumes Rails 8.1; since Rails 7.1, raise_on_missing_translations = true raises for every I18n.t call, not only the view and controller t helpers.