skip to content

In Rails 8.1, what does config.i18n.fallbacks = true in the generated production.rb do, and why can a missing French key look fine in production but not in development?

level: seniorimportance: should knowfreq 38%

answer

  1. generated production.rb only
  2. I18n::Backend::Fallbacks gets included
  3. fr-CA, then fr, then default_locale
  4. Array for defaults, Hash for mappings
  5. Symbol default before, String default after

basics

~20 s

It includes I18n::Backend::Fallbacks and builds I18n.fallbacks from default_locale, so a key missing in :fr is tried in its parent locales and then :en. Only production.rb sets it, so development shows the missing-key marker while production quietly prints English.

solid answer

~40 s

The generated `production.rb` sets `config.i18n.fallbacks = true`; `development.rb` and `test.rb` do not. At boot, `I18n::Railtie` includes `I18n::Backend::Fallbacks` into the backend and sets `I18n.fallbacks` to an `I18n::Locale::Fallbacks` built from `I18n.default_locale`. Each lookup then walks a chain: the requested locale, its parents (`:"fr-CA"` then `:fr`), then the default. A key missing from `fr.yml` therefore renders English in production but a `translation_missing` span in development, so gaps only show before deploy. An Array value sets the default chain (`[:en]`); combining defaults with a Hash such as `[:en, { ca: :es }]` adds cross-language mappings, while a Hash alone drops the default-locale step. Fallbacks change which data is read, never `I18n.locale`. A Symbol `default:` is tried before fallback locales, a String `default:` only after all of them.

code

ruby · 6 lines
ruby
# config/environments/production.rb (generated)
config.i18n.fallbacks = true

# alternatives
config.i18n.fallbacks = [:en]              # explicit default chain
config.i18n.fallbacks = [:en, { ca: :es }] # Catalan -> Spanish -> English

go deeper

for a junior

Recall that production falls back to the default locale for missing keys and development does not.

for a middle

Explain the chain for regional locales, the Array and Hash forms, and why I18n.locale itself does not change.

for a senior

Show how you keep fallbacks in production while catching gaps in tests, log fallbacks, and avoid the Hash-only trap.

for a principal

Weigh fallback policy per market: base-language versus default-language chains, partial launches, and how gaps are tracked over time.

## What the generated setting does A new Rails 8.1 app writes this into `config/environments/production.rb`: ```ruby # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true ``` Development and test have no such line. When the setting is present, `I18n::Railtie` does two things at boot: 1. Includes **`I18n::Backend::Fallbacks`** into the backend class, which wraps every lookup in a loop over candidate locales. 2. Sets **`I18n.fallbacks`** to an `I18n::Locale::Fallbacks` whose defaults are `[I18n.default_locale]`. ## How the chain is computed `I18n.fallbacks[locale]` returns the locales to try, in order: | Requested | `fallbacks = true`, default `:en` | |---|---| | `:fr` | `[:fr, :en]` | | `:"fr-CA"` | `[:"fr-CA", :fr, :en]` | | `:en` | `[:en]` | The parent step (`fr-CA` to `fr`) comes from the locale tag itself; no configuration is needed for it. Other value shapes give finer control: - **Array**: `config.i18n.fallbacks = [:en]` sets the default chain explicitly. - **Defaults plus mappings**: `config.i18n.fallbacks = [:en, { ca: :es }]` makes Catalan try Spanish before English. - **Hash alone**: `config.i18n.fallbacks = { ca: :es }` maps Catalan to Spanish but leaves the defaults empty, so nothing falls back to English. This surprises people. - **`defaults` and `map` keys**: `config.i18n.fallbacks.defaults = [:en]` and `config.i18n.fallbacks.map = { ca: :es }` express the same pair. ## Why production hides gaps On a tourism site with complete English copy and partial French copy: - **Development**: `t(".opening_hours")` on a French page renders `<span class="translation_missing" ...>Opening Hours</span>`; the gap is visible. - **Production**: the same call finds nothing in `:fr`, tries `:en`, and prints the English text. The page looks finished, just partly in English. Fallbacks are the right production behaviour, since a visitor should never see a placeholder, but they make production a poor place to discover missing keys. Teams pair them with raise-on-missing in the test environment (where fallbacks are off) and with a key-coverage check across locale files. To measure gaps in production, the backend's private `on_fallback` hook, called whenever a fallback locale supplies the result, can be overridden in an included module to log or count them. ## Interactions to know - **`default:`**: a Symbol default is another key and is tried before fallback locales; a String, Proc or Hash default is used only after every fallback locale fails. So `t(".title", default: "Visite")` on a French page returns the English title if one exists, not "Visite". - **`I18n.locale` does not change.** The request is still French; only the data source differs, so `l` formats, URLs and locale-specific logic still see `:fr`. - **Templates follow the chain too.** The view lookup context adds the fallback locales after the current one, so `show.fr.html.erb` is tried before `show.en.html.erb` and the unlocalized `show.html.erb`. - **Invalid fallback locales are skipped.** A fallback locale missing from `available_locales` is ignored rather than raising. - **Pluralization and formats** fall back as a whole entry: if `fr.tours.count` is missing, the English hash is used with English plural forms. ## A worked example A visitor whose browser asks for `fr-CA` lands on a tour page. `I18n.locale` is `:"fr-CA"` (it is on the list), and the app has `fr.yml` but no `fr-CA.yml`: - `t("tours.title")`: missing in `fr-CA`, found in `fr`, so French. - `t("tours.opening_hours")`: missing in `fr-CA` and `fr`, found in `en`, so English. - `l(@tour.starts_at, format: :long)`: `time.formats.long` from `fr`, month names from `fr`. Running `I18n.fallbacks[:"fr-CA"]` in a production console shows the chain directly, which is the quickest way to confirm a Hash-only configuration has not dropped the English step. ## Choosing a policy Falling back to the default locale suits sites where English is complete. Regional variants (`fr-CA`, `de-CH`) should fall back to their base language first, which the tag hierarchy gives for free. A product with no complete language at all needs explicit mappings and a deliberate default. The interview point is that the generated production config already makes this choice for you, differently from development, and that the difference changes what developers see.

  • Why does config.i18n.fallbacks = { ca: :es } stop French pages from falling back to English?
    A Hash alone becomes the mapping, and the defaults list stays empty, so `I18n.fallbacks[:fr]` is just `[:fr]`. Pass defaults with it, as `[:en, { ca: :es }]` or via `fallbacks.defaults` and `fallbacks.map`, to keep the default-locale step.
  • How do you find out which keys fall back in production?
    Override the private `on_fallback(original_locale, fallback_locale, key, options)` hook of `I18n::Backend::Fallbacks` in a module included into the backend, and log or count the key there. Pair it with raise-on-missing in tests, where fallbacks are off, to stop new gaps.
  • With fallbacks on, what does t('tours.title', default: 'Visite') return on a French page when only English has the key?
    The English title. String defaults are held back until every fallback locale has been tried, and English has the key. Use a Symbol default if a specific French key should win before the fallback chain.

  • New Rails apps enable locale fallbacks in every environment.
  • With fallbacks on, I18n.locale switches to :en when a French key is missing.
  • A String default: always beats the fallback locales.
  • fr-CA needs an explicit mapping before it falls back to fr.
  • Fallbacks make missing translations easy to spot in production.