Sub topic 3
Message Catalogs and Translation Keys
This chapter assumes you can write React function components and hooks, read TypeScript, and use npm with a bundler such as Vite. It covers the data side of internationalisation: where translated text lives and how you refer to it. Library setup comes in the next chapter, so here we only look at the shape of the data.
What a message catalog is
A message catalog is a data file that maps stable identifiers to translated text for one locale. The identifiers are called translation keys. The code refers to the key, and the catalog supplies the words.
Without a catalog, the text sits inside the component:
export function Banner() {
return <h1>Welcome back</h1>;
}With a catalog, the component names a key, and each language has its own file with that key:
{
"banner": {
"title": "Welcome back"
}
}{
"banner": {
"title": "Willkommen zurück"
}
}The component code is identical for every language. Adding a language means adding a catalog, not editing components.
Catalog structure
Catalogs are usually JSON, one file per locale. Two layouts are common.
- Flat keys:
"banner.title": "Welcome back". Every key is a single string. They are easy to diff and to search with grep. - Nested keys:
{ "banner": { "title": "Welcome back" } }. They group related messages visually and match how i18next resolves dotted paths by default.
Both resolve to the same lookup, banner.title. Pick one per project. Mixing them makes extraction tools and reviews harder.
A project layout that scales to several languages and areas of the app:
src/
locales/
en/
common.json
checkout.json
de/
common.json
checkout.jsonEach JSON file is a namespace in i18next terms. Splitting by feature keeps files small and lets you load only the namespaces a page needs, which matters for the lazy-loading chapter later.
The source and target relationship
One locale acts as the source. It is the language your developers write in, and it is where new keys are added first. The other locales are targets. Translators work from the source text to the target text.
The source catalog is also the natural fallback. If a key is missing in German, the app can show the English text instead of an empty string or the raw key. You configure this in the next chapter with fallbackLng.
Naming keys
There are two schools of thought on what a key should be.
| Style | Example | Strength | Weakness |
|---|---|---|---|
| Natural-language keys | `"Welcome back"` | Readable in code, no catalog needed for the source language | Any wording change breaks the key. Identical English words may need different translations. |
| Semantic keys | `banner.title` | Stable when wording changes. Context is part of the name. | You must open the catalog to see the text. |
Semantic keys are the safer default for a team. Consider the English word "Open". As a button that opens a file, it might be file.actions.open. As a store status, it might be store.status.open. Many languages translate these differently. With natural-language keys, both would collapse into one entry.
Practical rules for semantic keys:
- Name by place and purpose, not by wording:
checkout.submitButton, notcheckout.placeOrderNow. - Use a consistent case, such as camelCase segments separated by dots.
- Keep nesting to two or three levels. Deeper trees are hard to scan.
- Do not reuse one key in two different contexts just because the English text matches. Translators may need to differ.
- Never build a key by joining strings at runtime, such as `
t(status.${value})` with values you cannot list. Static analysis cannot find those keys, so they are never extracted or checked.
A dynamic key is acceptable when the possible values are a known, closed set. In that case, list them in a typed map so tooling still sees them.
Messages are whole sentences
Translators need the full sentence to produce a correct translation, because word order differs between languages. A catalog entry should therefore hold a complete message, with placeholders for the variable parts.
Avoid splitting a sentence across keys:
{
"cart": {
"youHave": "You have",
"itemsInCart": "items in your cart"
}
}This forces the code to glue pieces together as youHave + count + itemsInCart. It breaks in languages where the number does not sit between those two parts. Keep one message instead:
{
"cart": {
"summary": "You have {{count}} items in your cart"
}
}Placeholders use the {{name}} syntax in i18next. The next chapters show how values are filled in and how plural forms are chosen. For now, the rule is that a placeholder belongs inside the message and the sentence stays in one piece.
Plural keys in a catalog
i18next stores plural variants as sibling keys that share a base name and end in a category suffix. English uses two categories, one and other:
{
"items_one": "{{count}} item in your cart",
"items_other": "{{count}} items in your cart"
}The code asks for items, and the library picks items_one or items_other based on count. Languages with more categories need more suffixes. Arabic, for example, uses zero, one, two, few, many and other. A target catalog must provide the categories that language needs, even when the source catalog has only two. Plurals get a full chapter later.
Keeping catalogs in sync
Over time, catalogs drift. Developers add keys to the source, and the target files lag behind. Two kinds of mistakes appear.
- Missing keys: present in the source, absent in a target. The user sees fallback text or the raw key.
- Unused keys: present in a catalog, but no longer referenced in code. They waste translator effort.
A small script can compare locales. This one reports keys that exist in the English catalog but not in the German one:
import { readFileSync } from 'node:fs';
type Tree = { [key: string]: string | Tree };
function flatten(tree: Tree, prefix = ''): string[] {
return Object.entries(tree).flatMap(([key, value]) =>
typeof value === 'string'
? [prefix + key]
: flatten(value, `${prefix}${key}.`),
);
}
function load(path: string): Tree {
return JSON.parse(readFileSync(path, 'utf8')) as Tree;
}
const source = new Set(flatten(load('src/locales/en/common.json')));
const target = new Set(flatten(load('src/locales/de/common.json')));
const missing = [...source].filter((key) => !target.has(key));
if (missing.length > 0) {
console.error('Missing in de:', missing.join(', '));
process.exitCode = 1;
}Running this in continuous integration turns a silent runtime problem into a failed build. Plural suffixes need care here. English has items_one and items_other, while Polish needs items_few and items_many too, so a plain key comparison will report false differences for plural keys. Compare by the base name for those keys, or use a dedicated extraction tool. A later chapter covers that workflow.
Common errors
- Editing a key's name in code but not in catalogs. The screen shows the raw key, such as
banner.title. Search all locale files when you rename. - Invalid JSON, usually a trailing comma or an unescaped double quote. The whole catalog fails to load. Escape quotes inside messages as
\", or prefer typographic quotes that need no escaping. - Translating the keys. Keys are identifiers and stay identical in every locale. Only the values change.
- Putting HTML or formatting in values without a plan. Links and emphasis need the
Transcomponent, covered later, so translators can move them within a sentence. - Keeping stale keys after a feature is removed. Review unused keys when you delete UI.
Summary of decisions
Choose JSON catalogs, one per locale and namespace. Use semantic, stable keys. Keep each message a complete sentence with placeholders. Treat one locale as the source and check the others against it automatically.
Exercise
A developer writes t('Save') using the English word as the key. Later the design team changes the button label to "Save changes". What problem does this cause, and what key style avoids it?
Show answer
The key is the wording, so changing the label means changing the key in code and in every catalog, or the source and target entries no longer match. Translators may also be forced to give one translation to two different uses of "Save". A semantic key such as editor.actions.save stays the same when the English wording changes, and each use of the word can have its own entry.
Exercise
Why is a catalog entry like "youHave": "You have" combined in code with a separate "itemsInCart" entry a poor design for translation?
Show answer
Word order differs between languages, so the number may need to appear before, after or inside the phrase in a different way. Splitting the sentence prevents translators from reordering it and prevents correct plural handling. A single message with a {{count}} placeholder keeps the whole sentence available to the translator.