skip to content

In a Rails app, what do config.i18n.default_locale and config.i18n.available_locales control, and what happens when code asks for an unlisted locale?

level: juniorimportance: should knowfreq 45%

answer

  1. the value I18n.locale falls back to
  2. default_locale is :en unless configured
  3. unset list: every locale with data
  4. enforce_available_locales defaults to true
  5. I18n::InvalidLocale for an unlisted locale

basics

~20 s

default_locale (:en unless configured) is what I18n.locale returns until something sets it; available_locales is the allow list, derived from loaded translation data when unset. Since enforce_available_locales defaults to true, using an unlisted locale raises I18n::InvalidLocale.

solid answer

~40 s

Rails copies the `config.i18n.*` settings onto the `I18n` module after initialization, so `config.i18n.default_locale = :fr` sets `I18n.default_locale`. `I18n.locale` returns the locale set for the current execution context, or `default_locale` when nothing has set one. `available_locales` is the list of locales the app accepts: set it explicitly (`[:en, :fr, :de]`), or I18n derives it from every locale that has data in the loaded files, including locales a gem ships. `I18n.enforce_available_locales` defaults to true, so `I18n.locale = :xx`, `I18n.with_locale(:xx)` or `t(..., locale: :xx)` raise `I18n::InvalidLocale`; with an explicit list, translation data for other locales is skipped at load. Check user input with `I18n.locale_available?` before using it, and make sure `default_locale` itself is on the list.

code

ruby · 8 lines
ruby
# config/application.rb
module CityGuide
  class Application < Rails::Application
    config.load_defaults 8.1
    config.i18n.available_locales = [:en, :fr, :de]
    config.i18n.default_locale = :en
  end
end

go deeper

for a junior

Recall that default_locale is :en unless configured, that available_locales is an allow list, and that an unlisted locale raises I18n::InvalidLocale.

for a middle

Explain how the derived list is built from loaded data, which calls enforce the list, and why default_locale must be on it.

for a senior

Show how you validate user-supplied locales, keep the list explicit, and avoid disabling enforcement to make errors go away.

for a principal

Weigh how locales are added to the product, who owns the allow list, and what launching a regional variant costs in data and routing.

## The three settings A city-tourism site that serves English, French and German pages configures the I18n gem through `config/application.rb`. Rails' `I18n::Railtie` copies every `config.i18n` setting onto the `I18n` module once the app has initialized. | Setting | Default | What it decides | |---|---|---| | `config.i18n.default_locale` | `:en` | The locale used when nothing else set one | | `config.i18n.available_locales` | derived from loaded data | Which locales may be used at all | | `config.i18n.enforce_available_locales` | `true` | Whether using an unlisted locale raises | ```ruby # config/application.rb config.i18n.available_locales = [:en, :fr, :de] config.i18n.default_locale = :en ``` ## How `I18n.locale` is resolved `I18n.locale` is **per execution context**: each thread (and, with i18n 1.15 on Ruby 3.2 and later, each fiber) has its own value. When nothing in that context has assigned a locale, the getter returns **`I18n.default_locale`**. Rails does not read the URL, a cookie or the `Accept-Language` header for you; a request runs in the default locale until your controller sets one. `default_locale`, `available_locales` and the backend are **process-wide**. Changing them at runtime affects every thread, which is why they belong in configuration, not in a controller. ## Where `available_locales` comes from If you never set it, `I18n.available_locales` asks the backend for every top-level locale that has translation data in `I18n.load_path`. That includes your own `config/locales` files and any locale data a gem adds. Two consequences: - A stray `es.yml` (or a gem that ships Spanish data) silently makes `:es` acceptable. - The list is only known after translations load, so it is harder to reason about. Setting the list explicitly makes it a deliberate product decision. It also filters the data: with enforcement on and an explicit list, translations for unlisted locales are skipped when files load. ## Enforcement and `I18n::InvalidLocale` With `enforce_available_locales` true, these all check the locale and raise **`I18n::InvalidLocale`** (message: `"xx" is not a valid locale`) when it is not listed: - `I18n.locale = :xx` and `I18n.with_locale(:xx) { ... }` - `I18n.t(key, locale: :xx)` and `I18n.l(date, locale: :xx)` - `I18n.default_locale = :xx` - `I18n.locale = nil`, since `nil` is never an available locale Strings and symbols are both accepted (`"fr"` and `:fr` match). Region tags are separate locales: `:"fr-CA"` is not available just because `:fr` is. The rails guide describes enforcement as a security measure against setting an invalid locale from user input, and recommends leaving it on. The practical point: a raw `params[:locale]` of `"xx"` passed to `with_locale` becomes a 500 error. Validate first: ```ruby locale = params[:locale].presence_in(I18n.available_locales.map(&:to_s)) || I18n.default_locale # or: I18n.locale_available?(params[:locale]) ? params[:locale] : I18n.default_locale ``` ## Configuration pitfalls 1. **Default not on the list.** `I18n::Railtie` turns enforcement off while it applies settings, so `default_locale = :nl` with `available_locales = [:en, :fr]` boots fine, then the first translation raises `I18n::InvalidLocale`. 2. **Forgetting a regional locale.** Accepting `fr-CA` from a header needs `:"fr-CA"` on the list or a mapping to `:fr` before use. 3. **Turning enforcement off** to silence errors lets arbitrary strings become the locale, which then appear in cache keys, template lookups and anything else derived from `I18n.locale`. 4. **Assigning in an initializer.** `I18n.locale = :fr` at boot sets the value only for the booting thread; use `default_locale` for an app-wide default. ## Adding a locale safely Launching German on the tourism site touches more than one line: 1. Add the translation data first (`config/locales/*.de.yml`, including `date` and `time` formats). 2. Add `:de` to `config.i18n.available_locales`. 3. Widen any route constraint such as `locale: /en|fr|de/` so `/de/...` URLs match. 4. Check the console: `I18n.available_locales`, `I18n.default_locale` and `I18n.enforce_available_locales` should print what you expect. Removing a locale reverses the order, and anything that stored the old value (user preferences, queued jobs) needs a plan, because enforcement will reject it. Interviewers use this as a warm-up: the default comes from `default_locale`, the list is an allow list, and an unlisted locale raises rather than falling back quietly.

  • What happens if config.i18n.default_locale is not in config.i18n.available_locales?
    The app boots, because `I18n::Railtie` disables enforcement while it applies the settings. The first translation then resolves `I18n.locale` to the default, checks it against the list and raises `I18n::InvalidLocale`. Keep the default on the list.
  • Why prefer an explicit available_locales list over the derived one?
    The derived list is whatever locales have data in the load path, so a stray file or a gem's locale data widens what users can select. An explicit list documents which languages the product supports, and with enforcement on it also skips loading data for other locales.

  • Rails reads the Accept-Language header into I18n.locale automatically.
  • An unlisted locale silently falls back to default_locale.
  • When available_locales is unset, any locale string is accepted.
  • I18n.locale is one process-wide value shared by all threads.
  • Listing :fr also makes :'fr-CA' an available locale.