In a Rails view, how does l(stay.check_in_on, format: :long) produce French output, and why prefer it over strftime or to_fs?
answer
- l is I18n.localize
- Date reads date.formats, Time reads time.formats
- %B and %A from month_names, day_names
- missing format raises MissingTranslationData
- to_fs uses DATE_FORMATS, English only
basics
~20 sl delegates to I18n.localize: a Date reads date.formats.long and a Time reads time.formats.long from the current locale, and %B, %b, %A and %a are filled from that locale's month and day names. strftime and to_fs ignore I18n and print English.
solid answer
~30 s`l` (alias of `localize`) hands the object to `I18n.localize`. A symbol format is looked up as `date.formats.<name>` for a `Date`, and as `time.formats.<name>` for anything that responds to `sec` (`Time`, `DateTime`, `ActiveSupport::TimeWithZone`). The value is a strftime pattern; before formatting, the i18n gem replaces `%a`, `%A`, `%b`, `%B`, `%p` and `%P` with the locale's `abbr_day_names`, `day_names`, `abbr_month_names`, `month_names` and `time.am`/`time.pm`. Rails ships that data for `en` only, so `fr.yml` must supply it. An unknown format symbol raises `I18n::MissingTranslationData`, and `l(nil)` raises unless you pass `default:`. `strftime("%B")` and `to_fs(:long)`, which reads `Date::DATE_FORMATS`, never consult I18n and always print English month names.
code
yaml · 14 linesfr:
date:
formats:
default: "%d/%m/%Y"
long: "%-d %B %Y"
day_names: [dimanche, lundi, mardi, mercredi, jeudi, vendredi, samedi]
abbr_day_names: [dim, lun, mar, mer, jeu, ven, sam]
month_names: [~, janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre]
abbr_month_names: [~, janv., févr., mars, avr., mai, juin, juil., août, sept., oct., nov., déc.]
time:
formats:
long: "%-d %B %Y à %H:%M"
am: am
pm: pmgo deeper
Recall that l formats dates and times from the locale file and that format: :long names a stored pattern.
Explain the date versus time namespaces, how %B and %A are swapped for locale names, and why strftime and to_fs stay English.
Show how you guard nil dates, keep format names consistent across locales, and catch missing name arrays before users see the placeholder text.
Weigh hand-maintained locale date data against a shared locale-data dependency, and where formatting decisions belong as locales multiply.
## What `l` does `l` is the view (and controller) alias of `localize`, and it delegates straight to **`I18n.localize`**. It formats dates and times using patterns and names stored in the locale files, so the same template prints "14 juillet 2026" for French visitors and "July 14, 2026" for English ones. ```erb <%# app/views/stays/show.html.erb %> <p><%= l(@stay.check_in_on, format: :long) %></p> ``` ## Which key it reads The format argument is either a **Symbol** naming a stored pattern or a **String** pattern used directly. For a Symbol, the backend chooses the namespace by the object's type: | Object | Lookup for `format: :long` | |---|---| | `Date` | `date.formats.long` | | `Time`, `DateTime`, `ActiveSupport::TimeWithZone` (respond to `sec`) | `time.formats.long` | | no `format:` given | `date.formats.default` or `time.formats.default` | | `format: "%-d %B"` (a String) | that pattern, no lookup | The stored value is an ordinary strftime pattern. Before calling `strftime`, the i18n gem **rewrites the name directives** using the current locale: - `%a` / `%A` → `date.abbr_day_names` / `date.day_names` - `%b` / `%B` → `date.abbr_month_names` / `date.month_names` - `%p` / `%P` → `time.am` or `time.pm`, upcased or downcased - `%^a`, `%^b` and friends → the same names, upcased Numeric directives such as `%d`, `%Y` and `%H` are left to Ruby. ## What the French file must carry Rails' Active Support ships `date` and `time` data for **`en` only**. A French app has to provide the formats and the name arrays itself (or pull them from a locale-data gem): ```yaml fr: date: formats: default: "%d/%m/%Y" long: "%-d %B %Y" day_names: [dimanche, lundi, mardi, mercredi, jeudi, vendredi, samedi] month_names: [~, janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre] time: formats: long: "%-d %B %Y à %H:%M" ``` `month_names` starts with `~` (nil) because months are numbered from 1. ## Failure modes 1. **Unknown format symbol.** `localize` looks the pattern up with `raise: true`, so `l(date, format: :brief)` with no `date.formats.brief` raises `I18n::MissingTranslationData`. There is no fallback to `:default` and no `translation_missing` span. 2. **Missing name arrays.** If the pattern exists but `fr.date.month_names` does not, the rewrite step fails and the output becomes the literal text "Translation missing: fr.date.month_names", which is easy to miss in a review. 3. **`nil`.** `l(nil)` raises an `I18n::ArgumentError` ("Object must be a Date, DateTime or Time object") unless you pass `default:`, for example `l(@stay.check_out_on, default: "—")`. 4. **Strings.** A date that is still a String (unparsed params) raises the same error; parse it first. 5. **Pattern and type mismatch.** A `date.formats` pattern containing `%H:%M` applied to a `Date` prints `00:00`, because a Date has no time of day. Keep time directives in `time.formats`. Controllers and mailers include `l` as well, through `AbstractController::Translation`, so an email's dates follow the same rules as a page's. That matters most in mailers, where the dates must match the recipient's language rather than the sender's. ## `l` versus `strftime` and `to_fs` | Call | Locale-aware? | Source of the pattern | |---|---|---| | `l(date, format: :long)` | Yes | `date.formats.long` in the current locale | | `date.strftime("%B")` | No | Ruby's built-in English names | | `date.to_fs(:long)` | No | `Date::DATE_FORMATS[:long]`, a Ruby Hash | `to_fs` (with `to_formatted_s` as its older alias) is fine for logs, filenames and machine formats such as `:db` or `:iso8601`. Anything a user reads in more than one language goes through `l`. ## Passing the locale explicitly `l` accepts a `locale:` option, so code that runs outside a request can format for a specific reader without switching the ambient locale: `l(@stay.check_in_on, format: :long, locale: :fr)`. The same data drives form helpers: `date_select` reads `date.order` and the month name arrays, so a complete French `date` block fixes both display and input widgets at once. Keep the **format names identical across locales** (`:default`, `:short`, `:long`) and let each locale decide the pattern. Templates then never branch on language, and a reviewer can compare `en.date.formats` with `fr.date.formats` line by line. ## Related ground `l` formats whatever instant it receives; converting it to the visitor's time zone (`Time.zone`, `in_time_zone`) happens before and belongs to time-zone handling, not to I18n. Number formatting helpers such as `number_to_currency` read their own `number.*` keys from the same locale files. The interview point is that names and patterns are data in the locale, chosen by the object's type, and that only `l` reads them.
- Why does l(@stay.created_at, format: :long) ignore the date.formats.long pattern you just added?`created_at` is an `ActiveSupport::TimeWithZone`, which responds to `sec`, so the backend reads `time.formats.long`. Dates and times have separate format namespaces; add the pattern under `time.formats` too, or call `l` on `created_at.to_date` when only the date matters.
- What happens with l(date, format: :brief) when no locale defines date.formats.brief?It raises `I18n::MissingTranslationData`, because `localize` looks the pattern up with `raise: true`. Unlike `t` in a view, there is no placeholder span and no fall back to `:default`; with locale fallbacks enabled, another locale's `brief` pattern would be used if one exists.
- date.strftime('%B') prints the month in the current I18n locale.
- l(time, format: :long) reads date.formats.long for every date-like object.
- Rails ships French month and day names out of the box.
- An unknown l format falls back to the :default pattern.
- to_fs(:long) is the locale-aware way to format a date for users.