Translations

Translations move every word on the page into locale files, one per language, in app/locales/. A template reads them by key with t():

<a href={ link('/') }>{ t('nav.shop') }</a>

Nested keys flatten to dots, so { "nav": { "shop": "Shop" } } is nav.shop. puzzle.config.js turns translations on and lists the languages. A visitor starts in the language they last picked, else their browser's language if the app has it, else defaultLocale. A key a locale leaves out falls back to the default locale's text, so a half-done translation still renders.

Plurals. An entry whose keys are plural forms (zero, one, two, few, many, other) picks a form from a count:

{ t('playlist.count', { count: albums.length }) }

{count} inside the text prints the number. The form comes from the language's own rules: English has one and other, French counts 0 as singular too, and Polish needs few and many as well. A zero form, when the entry has one, wins for an exact 0 in every language. That is why pluralize() from the last lesson is for apps in English only: it adds an English s. In a translated app, counts go through t().

Music is now set up with English: the header reads its links from en.json, and the language switcher beside it lists the configured locales. fr.json has been started.

Your turn

  1. In puzzle.config.js, add 'fr' to locales. A Français button appears in the header.
  2. Click it. The shop link is French, the rest falls back to English.
  3. Finish fr.json: translate nav.playlist and playlist.title, and add the plural playlist.count with zero, one and other forms (Aucun album pour l'instant, {count} album, {count} albums).
  4. In Playlist.pzl, swap pluralize() for t('playlist.count', { count: albums.length }), then try the playlist empty and full, in both languages.

Switching language rebuilds the page in place. Store records survive it, so the playlist keeps its albums.

Chapters