In a Rails view, what does t('.title') look up, and where do the translations it reads come from?
answer
- relative key, the template decides
- slashes and partial underscore become dots
- config/locales/**/*.{rb,yml}, loaded automatically
- controller: path.action.key, then path.key
- span class translation_missing
basics
~10 sA leading dot makes the view helper t scope the key by the current template: t('.title') in app/views/campsites/index.html.erb reads campsites.index.title from YAML or Ruby files loaded automatically from config/locales by the Simple backend.
solid answer
~40 sRails adds every `.yml` and `.rb` file under `config/locales` (at any depth) to `I18n.load_path`, and the default `I18n::Backend::Simple` deep-merges them into an in-memory hash keyed by the top-level locale key (`fr:`), not by the file name. In a view, `t` (alias of `translate`) treats a key starting with a dot as relative: it takes the template's virtual path, turns slashes and a partial's leading underscore into dots, and prepends it. So `t(".title")` in `campsites/index.html.erb` reads `campsites.index.title`, and in `campsites/_review.html.erb` it reads `campsites.review.title`. In a controller, `t(".created")` inside `CampsitesController#create` reads `campsites.create.created`, then `campsites.created`. By default a missing key does not raise: the helper renders a `translation_missing` span with the titleized last segment.
code
yaml · 9 lines# config/locales/views/campsites.fr.yml
fr:
campsites:
index:
title: "Campings"
review:
posted_by: "Avis de %{name}"
create:
created: "Camping ajouté."go deeper
Recall that a leading dot means the key is relative to the template file, and that config/locales files load automatically by their top-level locale key.
Explain how the virtual path becomes the scope, why partials drop the underscore, and how controller lazy lookup adds an action-less fallback key.
Show how you keep keys stable through template moves, catch missing keys in CI with raise-on-missing, and decide between lazy and shared absolute keys.
Weigh lazy per-template keys against a shared key vocabulary for translator workflow, reuse and churn when views are refactored often.
## Where Rails finds translations Rails internationalization (**I18n**) is built on the `i18n` gem. At boot, the application and every engine add their `config/locales` directory to the load path with the glob `**/*.{rb,yml}`, so `config/locales/fr.yml`, `config/locales/views/campsites.fr.yml` and `config/locales/models/fr.yml` are all picked up without extra configuration. The **file name does not decide the locale**: the top-level key inside the file does (`fr:`). File names are a team convention only. The default backend is **`I18n::Backend::Simple`**. It reads each file, deep-merges the data into an in-memory hash keyed by locale, and answers every lookup from that hash. Because the merge is deep, two files can both contribute keys under `fr.campsites`; a later file overwrites an earlier one only at the deepest level. In development, Rails watches those directories and reloads changed or added locale files without a restart. A short French file for a campsite-review app: ```yaml fr: campsites: index: title: "Campings" review: posted_by: "Avis de %{name}" ``` ## How a leading dot becomes a full key `t` is the view alias of `ActionView::Helpers::TranslationHelper#translate`. When the key starts with `.`, the helper takes the template's **virtual path**, replaces each `/` (and a partial's leading `_`) with `.`, and prepends the result: | Template | Call | Key looked up | |---|---|---| | `app/views/campsites/index.html.erb` | `t(".title")` | `campsites.index.title` | | `app/views/campsites/_review.html.erb` | `t(".posted_by", name: review.author_name)` | `campsites.review.posted_by` | | `app/views/admin/campsites/show.html.erb` | `t(".heading")` | `admin.campsites.show.heading` | Two consequences matter in practice: - The key follows the **file the call is written in**, not the action that rendered it. A partial rendered from three pages reads one set of keys under its own name. - Moving or renaming a template silently changes its keys, so a rename needs a matching YAML edit. If the helper has no template path to work from, it raises a `RuntimeError` saying the shortcut cannot be used, rather than guessing a scope. ## Lazy lookup outside views Controllers and mailers include **`AbstractController::Translation`**, which has its own relative-key rule built from `controller_path` and `action_name`: 1. `t(".created")` inside `CampsitesController#create` first looks up `campsites.create.created`. 2. If that is missing it tries `campsites.created`, so several actions can share one flash message. 3. Any `default:` you pass is tried after those two keys. The plain **`I18n.t`** method has no lazy behaviour at all. `I18n.t(".title")` simply drops the empty segment and reads a top-level `title` key, which is usually missing, so lazy keys belong only in views, controllers and mailers. ## What a missing key looks like In a new app, `config.i18n.raise_on_missing_translations` is commented out in the generated `development.rb` and `test.rb`, so the view helper does not raise. It renders: ```html <span class="translation_missing" title="translation missing: fr.campsites.index.title">Title</span> ``` The visible text is the titleized last key segment, so a page with gaps still reads, and the `title` attribute names the exact key to add. `I18n.t` called directly returns the plain string `"Translation missing: fr.campsites.index.title"` instead. Turning on raise-on-missing in development and test is the usual way to catch gaps before they reach users. ## Other ways to address a key Lazy keys are one of several addressing forms the helper accepts: - **Absolute keys**: `t("campsites.index.title")` works from any template and is the right choice for text shared across pages. - **`scope:`**: `t(:title, scope: [:campsites, :index])` builds the same key from parts, which reads well in helpers that loop over several keys. - **Arrays**: `t([".title", ".subtitle"])` returns an Array of translations, each resolved with the same rules. - **Block form**: `t(".title") { |text, key| ... }` yields the translation and the fully resolved key, handy for wrapping text in markup that records which key produced it. Whichever form you use, the lookup ends at the same in-memory hash, so the YAML layout is the contract between templates and translators. ## Habits that keep locale files maintainable - **Mirror the view tree** in the YAML, so a key's location is obvious from the template path. - **Split by area and locale** (`campsites.fr.yml`, `campsites.en.yml`) so translators can compare locales side by side. - **Quote YAML words** such as `"yes"`, `"no"`, `"on"` and `"off"`; YAML reads them as booleans, a warning the generated `en.yml` itself carries. - **Prefer lazy keys for page copy** and absolute keys (`t("shared.save")`) for text reused across templates. - **Pass values, not fragments**: interpolate with `%{name}` rather than concatenating translated pieces, because word order differs between languages. Lazy lookup is the opening question because it tests whether a candidate understands that the scope is derived mechanically from file paths; everything else in Rails I18n builds on that key layout.
- Why does t('.title') inside a shared partial not pick up the key of the page that rendered it?Lazy lookup scopes by the virtual path of the template the call is written in. In `campsites/_review.html.erb` the prefix is always `campsites.review`, whichever page rendered the partial. If a partial needs page-specific wording, pass the text in as a local or use an absolute key; the caller's scope never leaks in.
- What does t('.created') read inside Admin::CampsitesController#create?`AbstractController::Translation` builds the prefix from `controller_path` with slashes turned into dots, so it looks up `admin.campsites.create.created` first and `admin.campsites.created` second; a `default:` is tried after both. The bare `I18n.t` method does none of this scoping.
- Does the YAML file name decide which locale its keys belong to?No. The Simple backend treats the top-level key inside each file (`fr:`) as the locale; the file name is a convention. A file called `campsites.fr.yml` that starts with `en:` loads English keys, which is why teams pair a naming habit with review or a key-checking tool.
- t('.title') is scoped by the controller action that rendered the template.
- I18n.t('.title') in a model or service scopes the key the same way the view helper does.
- Only top-level config/locales/*.yml files load; subfolders always need load_path edits.
- The file name fr.yml is what makes its keys French.
- A missing key in a view raises an exception in every environment by default.