In a Rails app, which locale keys translate an Active Record model's name and its attribute names, and which helpers read them?
answer
- activerecord scope, not the view tree
- models.campsite with one and other
- attributes.campsite.nightly_price
- model_name.human defaults to count 1
- helpers.label wins over attributes
basics
~10 sModel names live under activerecord.models.campsite (one/other) and attribute names under activerecord.attributes.campsite.nightly_price. Campsite.model_name.human, Campsite.human_attribute_name, form labels, submit buttons and error full messages read them, falling back to humanized names.
solid answer
~30 sActive Record models translate under the `activerecord` scope: `activerecord.models.campsite` holds the model's name (a string, or `one:`/`other:` forms) and `activerecord.attributes.campsite.nightly_price` an attribute's name. `Campsite.model_name.human` reads the first with `count: 1` by default, so `human(count: 2)` gives the plural. `Campsite.human_attribute_name(:nightly_price)` reads the second and falls back to `"Nightly price"` when nothing is found. Views pick these up for free: `form.label :nightly_price` checks `helpers.label.campsite.nightly_price` first and then `human_attribute_name`, the default submit text comes from `helpers.submit.create` ("Create %{model}") with `model_name.human`, and validation full messages use the attribute name. Plain Active Model objects use the `activemodel` scope instead.
code
yaml · 12 linesfr:
activerecord:
models:
campsite:
one: "Camping"
other: "Campings"
attributes:
campsite:
nightly_price: "Prix par nuit"
helpers:
submit:
create: "Créer %{model}"go deeper
Recall the activerecord.models and activerecord.attributes keys and that form labels pick them up automatically.
Explain the lookup chain for human_attribute_name and labels, model_name.human's count default, and the activemodel scope for form objects.
Show how you keep one source of truth for names across forms and errors, and how you catch silent humanized fallbacks in tests.
Weigh model-scope naming against per-view wording when product copy and domain vocabulary diverge across many locales.
## The keys Model and attribute names do not follow the view tree. They live under a scope named after the library: **`activerecord`** for Active Record models, **`activemodel`** for plain classes that include `ActiveModel::Model` or `ActiveModel::API` (form objects, search objects). ```yaml fr: activerecord: models: campsite: one: "Camping" other: "Campings" attributes: campsite: name: "Nom" nightly_price: "Prix par nuit" pitch_count: "Nombre d'emplacements" ``` The model segment is `model_name.i18n_key`, the underscored class name: `Campsite` → `campsite`, `Admin::Campsite` → `admin/campsite`. ## Who reads them | Call | Keys tried | When nothing is found | |---|---|---| | `Campsite.model_name.human` | `activerecord.models.campsite` with `count: 1` | `"Campsite"` | | `Campsite.model_name.human(count: 2)` | same key, `other:` form | `"Campsite"` | | `Campsite.human_attribute_name(:nightly_price)` | `activerecord.attributes.campsite.nightly_price`, then `attributes.nightly_price` | `"Nightly price"` | | `form.label :nightly_price` | `helpers.label.campsite.nightly_price`, then `human_attribute_name` | humanized method name | | default `form.submit` | `helpers.submit.campsite.create`, then `helpers.submit.create` | `"Create Campsite"` | The **submit button** is a nice example of composition. Action View's English data has `helpers.submit.create: "Create %{model}"`, and the helper passes `model: Campsite.model_name.human`. A French file with `helpers.submit.create: "Créer %{model}"` and the model name above produces "Créer Camping" with no change to the form. It picks `update` instead of `create` when the record is persisted. Validation messages also use `human_attribute_name` when they build full messages, so a French attribute name appears in French error text; how the errors object finds message keys is a separate topic. ## Plural model names `model_name.human` passes `count: 1` unless you override it, so a model entry written with `one:`/`other:` returns the singular by default. Index headings that need the plural call `Campsite.model_name.human(count: 2)`. An entry written as a plain string ("Camping") ignores `count` entirely. ## Inheritance and namespaces - **Single-table inheritance**: the lookup walks the class's ancestors up to the base class. For `class GlampingSite < Campsite`, `human_attribute_name(:nightly_price)` tries `glamping_site.nightly_price` first and then `campsite.nightly_price`, so subclasses only override what differs. - **Namespaced models** use a slash in the key: `activerecord.attributes.admin/campsite.name`. YAML accepts `admin/campsite:` as a key. - **Shared attribute names** can sit at the top level under `attributes.created_at`, which `human_attribute_name` tries after the model-specific key. ## Checking the names in a console Both readers accept an explicit locale, which makes a quick check easy: ```ruby Campsite.model_name.human(count: 2, locale: :fr) # => "Campings" Campsite.human_attribute_name(:nightly_price, locale: :fr) # => "Prix par nuit" ``` ## Why the names live on the model Keeping names in the model scope gives one source for every place the attribute appears: labels, table headers built with `human_attribute_name`, error full messages, and admin screens. A per-view key for the same attribute drifts as soon as one page is reworded. Use view keys for sentences and headings, and model keys for the names of things. - Table headers: `Campsite.human_attribute_name(:pitch_count)` in an index view keeps headers in step with form labels. - Flash and page titles: interpolate `model_name.human` into a lazy key ("%{model} supprimé") instead of hard-coding the noun. - Exports and emails: the same calls work outside views because they are model class methods, not helpers. ## Pitfalls 1. **Wrong scope for form objects.** A `CampsiteSearch` class with `include ActiveModel::Model` reads `activemodel.attributes.campsite_search.*`; putting its keys under `activerecord` does nothing. 2. **Silent fallbacks.** `human_attribute_name` returns a humanized English name when keys are missing, so an untranslated French form looks plausible in review. Setting `config.i18n.raise_on_missing_translations = :strict` makes `human_attribute_name` raise instead. 3. **Mixing up `models` and `attributes`.** `activerecord.models.campsite.nightly_price` is never read. 4. **Plural model names in other locales** follow the same count rules as any translation, so a locale with extra plural categories needs those forms under `activerecord.models` too, or `human(count: 5)` can fail for it. 5. **Lazy keys for labels.** `t(".nightly_price")` in a form duplicates the attribute name per view; letting `form.label` find the model attribute keeps one source. Interviewers use this to check that a candidate knows names come from the model scope, not from each view, and that forms and error messages reuse them automatically.
- Where do the labels for a form object that includes ActiveModel::Model come from?From the `activemodel` scope: `activemodel.attributes.campsite_search.max_price` for a `CampsiteSearch` class. `ActiveModel::Translation` sets that scope and Active Record overrides it to `activerecord`, so form objects and models keep separate key trees even when attribute names match.
- How do you make the French form label differ from the attribute name used in error messages?Put the label text under `helpers.label.campsite.nightly_price`; `form.label` checks that key before falling back to `human_attribute_name`, while full error messages keep using `activerecord.attributes.campsite.nightly_price`.
- Attribute names go under the view's lazy-lookup path, such as campsites.form.nightly_price.
- model_name.human returns the plural form unless you pass count: 1.
- Form objects using ActiveModel::Model read keys under activerecord.attributes.
- human_attribute_name raises by default when the attribute key is missing.
- An STI subclass needs its own copy of every attribute translation.