Translation File Management — i18n

Keep dictionaries per locale and per namespace (locales/ja-JP/booking.json), so each feature can be lazy-loaded (only the translations needed are loaded) and…

Mrs. Sri adds a new line to the booking card: "Breakfast included". Now the Indonesian, Japanese and Arabic dictionaries each need that sentence, and the sentence for an old button that was renamed has to come out. Checked by hand, something always slips. Once an app has four languages, its translation files need rules: where files live, which format they use, how keys are named, and who checks which file fell behind. That last part is the job of an extract tool: it reads the code, lists the keys it uses, and compares them with every file.

File layout: per locale, per namespace

locales/
  en-US/booking.json   ← the source, written by Mrs. Sri
  en-US/admin.json     ← Mrs. Sri's dashboard
  id-ID/booking.json
  ja-JP/booking.json
  ar-EG/booking.json

Formats: JSON, ICU, XLIFF, PO

The same text, the book button, in four formats:

// i18next JSON v4 (locales/ja-JP/booking.json)
{ "book": "予約する(お支払いは現地で)" }

// ICU JSON (react-intl, next-intl, i18next-icu)
{ "booking.book": "予約する(お支払いは現地で)" }
<!-- XLIFF 2.0 -->
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="ja-JP">
  <file id="booking">
    <unit id="book">
      <notes><note category="description">Main button of the booking card. The guest pays cash on arrival. Max. 32 characters.</note></notes>
      <segment><source>Book now, pay at the homestay</source><target>予約する(お支払いは現地で)</target></segment>
    </unit>
  </file>
</xliff>
# PO (gettext)
#. Main button of the booking card. The guest pays cash on arrival. Max. 32 characters.
msgctxt "booking"
msgid "Book now, pay at the homestay"
msgstr "予約する(お支払いは現地で)"
Format Used by When to pick it
i18next JSON v4 i18next, react-i18next Apps that read the file directly; plurals through _one/_other suffixes
ICU JSON react-intl, next-intl, FormatJS Sentences with plural and select inside one message
XLIFF Professional translation tools (CAT tools, TMS), Angular Exchanging files with translators: source, translation, notes and status in one file
PO (gettext) PHP, WordPress, Python/Django, Poedit Projects already on gettext; plurals use their own Plural-Forms formula

Key names: semantic or source text

Finding missing and unused keys

The booking card code uses 10 keys: room, perNight, checkIn, group, nights and total (with count), book, cancel, breakfast, thanks. A plural key needs one form per Intl.PluralRules category of that language:

const used = ['room', 'perNight', 'checkIn', 'group', 'nights', 'total', 'book', 'cancel', 'breakfast', 'thanks'];
const plural = new Set(['nights', 'total']);
const ORDER = ['zero', 'one', 'two', 'few', 'many', 'other'];

function expected(locale) {
  const cats = new Intl.PluralRules(locale).resolvedOptions().pluralCategories.sort((a, b) => ORDER.indexOf(a) - ORDER.indexOf(b));
  return used.flatMap((k) => (plural.has(k) ? cats.map((c) => `${k}_${c}`) : [k]));
}
function check(locale, file) {
  const want = expected(locale);
  return { missing: want.filter((k) => !(k in file)), unused: Object.keys(file).filter((k) => !want.includes(k)) };
}
check('ja-JP', jaJP); // { missing: ["book", "cancel", "breakfast"], unused: ["payAtHomestay"] }
check('ar-EG', arEG); // { missing: ["nights_zero", "nights_few", "nights_many", "total_zero", "total_one", "total_two", "total_few", "total_many", "breakfast", "thanks"], unused: [] }

ja-JP still has payAtHomestay, the old name of book before Mrs. Sri renamed it; that's why book is missing and payAtHomestay is unused. ar-EG is missing 10 keys, eight of them plural forms English doesn't need.

Automatic extraction: i18next-cli and formatjs

i18next-parser is deprecated; npm says "use i18next-cli instead". i18next-cli 1.75.2 reads the code and the files together:

// i18next.config.js
export default {
  locales: ['en-US', 'id-ID', 'ja-JP', 'ar-EG'],   // the first one = primary language
  extract: { input: 'src/**/*.{ts,tsx}', output: 'locales/{{language}}/{{namespace}}.json' },
};
$ npx i18next-cli status ja-JP
Key Status for "ja-JP":
Overall: [■■■■■■■■■■■■■■□□□□□□] 70% (7/10)
         3 absent
  ✓ room
  ...
  ✗ book  (absent)
  ✗ cancel  (absent)
  ✗ breakfast  (absent)
  ✓ thanks

What extract did to our project, measured: the en-US source keeps nights_one and nights_other, and every missing key in the other languages gets "", plural forms included. But payAtHomestay in ja-JP counts as unused and is deleted: the Japanese text of the button, "予約する(お支払いは現地で)", is gone, and book comes back as "". A source language with only the other category, such as Indonesian or Japanese, has it worse. i18next-cli 1.75.2 then uses its plural keys without a suffix: it deletes nights_other and adds "nights": "nights". It also fills the missing plural forms of ar-EG with "nights", which status counts as translated. To prevent it: rename a key in every file before you run extract (move the Japanese text from payAtHomestay to book), run extract on a branch, and read the git diff before you commit.

For react-intl, formatjs (@formatjs/cli 6.16.34) collects messages from FormattedMessage and defineMessages: npx formatjs extract 'src/**/*.tsx' --out-file lang/en-US.json writes { "booking.book": { "defaultMessage": "Book now, pay at the homestay", "description": "..." } }.

Context for translators

Translators see only the text, not the page. Without context, "Book" may be translated as "本" (a book to read). Give every key: a description (the description field in FormatJS, <note> in XLIFF, a #. comment in PO), a length limit ("max. 32 characters"), what each slot means ({{count}} is the number of nights, {{name}} the guest's name), and a screenshot of that part of the page. Translation platforms (TMS) can show all of that next to the text.

⚠️ When can extract and status fool you?

TL;DR: Keep dictionaries per locale and per namespace (locales/ja-JP/booking.json), so each feature can be lazy-loaded (only the translations needed are loaded) and managed on its own. JSON v4 for i18next, ICU JSON for react-intl and next-intl, XLIFF for exchanging with translators and CAT tools, PO for gettext projects. Use semantic keys (booking.book), not source text. A missing key = used by the code (with every plural category of that language) but absent from the file; an unused key = in the file but not used by the code: ja-JP lacks book, cancel, breakfast and still holds payAtHomestay. i18next-parser is deprecated; use npx i18next-cli status and extract (i18next-cli 1.75.2), or formatjs extract for react-intl. Always read the diff: extract deleted payAtHomestay from ja-JP, and its Japanese text with it. Set returnEmptyString: false (the default since i18next 27), and give translators descriptions, length limits, slot meanings and screenshots.

⚡ One folder per locale, one file per feature

Write:

locales/
  en-US/booking.json   ← the source, written by Mrs. Sri
  en-US/admin.json     ← Mrs. Sri's dashboard
  id-ID/booking.json

Meaning: Each file is one namespace. Namespaces can be lazy-loaded: guest pages load only booking.json, and admin.json is fetched when the dashboard opens. Files per feature also mean fewer conflicts, because the admin team and the booking team don't edit the same file.

⚡ Missing keys and unused keys

Write:

check('ja-JP', jaJP); // { missing: ["book", "cancel", "breakfast"], unused: ["payAtHomestay"] }
Guess first: why is book missing, when the button is already translated into Japanese?

Its translation still sits under payAtHomestay, the old name of book before Mrs. Sri renamed it. So book is missing and payAtHomestay is unused.

Meaning: A missing key is used by the code (with every plural category of that language) but absent from the file. An unused key is in the file but not used by the code.

⚡ Read the diff after extract

Meaning: npx i18next-cli extract rewrites the files. In our project it deleted payAtHomestay from ja-JP as unused, and the Japanese text of the button went with it. Run extract on a branch, then read the git diff before you commit.

What you will learn