Sub topic 3
Message catalogs and translation keys
This chapter assumes you know React function components and hooks, basic TypeScript, and how to import JSON in a Vite or webpack project. It covers what a message catalog is, how keys map to text, and the mistakes that cause trouble later. Library setup comes in the next chapter.
What a message catalog is
A message catalog is a data structure that maps stable keys to translated text, one catalog per locale. Components ask for a key, and the library returns the text for the active locale. The text lives in data, not in JSX, so translators can work without touching component code.
In react-i18next, a catalog is a JSON object. The following example shows two catalogs, one for English and one for German, that share the same keys.
export const en = {
nav: {
home: 'Home',
cart: 'Cart',
},
greeting: 'Hello, {{name}}!',
items_one: '{{count}} item in your cart',
items_other: '{{count}} items in your cart',
};
export const de = {
nav: {
home: 'Startseite',
cart: 'Warenkorb',
},
greeting: 'Hallo, {{name}}!',
items_one: '{{count}} Artikel in Ihrem Warenkorb',
items_other: '{{count}} Artikel in Ihrem Warenkorb',
};Each locale has the same set of keys. The values differ.
How keys resolve
Keys can be nested. i18next joins the levels with a dot, so nav.cart finds cart inside nav. Keys such as items_one and items_other use a suffix that i18next reads as a plural category. Plurals get their own chapter, so for now treat the suffix as part of the key.
When no catalog has the key, i18next returns the key string. A label reading nav.cart on screen is the usual sign of a missing or misspelled key.
Naming keys
Good key names make a catalog easier to maintain. These conventions work well in most teams:
- Name keys by purpose, not by wording. Use
checkout.submit, notplaceOrderButtonorPlace order. The English text can change without renaming the key. - Group keys by feature or screen, such as
checkout.*andprofile.*. Keep shared words likecommon.savein one place. - Use one casing style.
camelCasesegments joined by dots is common. - Keep whole sentences in one message. Do not split a sentence across keys, because word order differs between languages.
Common errors
Several mistakes show up often in real projects:
- Using English text as the key. It looks convenient, but a copy edit then changes the key in every catalog.
- Reusing one key for two meanings. "Open" as a verb and "Open" as a status can need different words in another language. Use two keys.
- Letting catalogs drift. A key added to
enbut not todesilently falls back to English. The next section shows how to see that happen. - Concatenating pieces.
t('hello') + namecannot be reordered by a translator. Use a variable inside the message instead, as covered in a later chapter.
Seeing the fallback in action
The script below lists keys that exist in English but not in German. It flattens each catalog into dotted paths and compares them.
type Catalog = { [key: string]: string | Catalog };
function flatten(catalog: Catalog, prefix = ''): string[] {
return Object.entries(catalog).flatMap(([key, value]) =>
typeof value === 'string'
? [prefix + key]
: flatten(value, `${prefix}${key}.`)
);
}
export function missingKeys(source: Catalog, target: Catalog): string[] {
const present = new Set(flatten(target));
return flatten(source).filter((key) => !present.has(key));
}
const en: Catalog = { nav: { home: 'Home', cart: 'Cart' }, save: 'Save' };
const de: Catalog = { nav: { home: 'Startseite' } };
console.log(missingKeys(en, de)); // ['nav.cart', 'save']A check like this can run in continuous integration. A later chapter covers extraction tools and missing-translation handlers.
Exercise
A teammate names a key Place order and uses it for the checkout button. Later, the product team changes the button text to "Buy now". What problem does the naming cause, and what name would you use instead?
Show answer
The key is tied to the English wording, so the copy change either leaves a misleading key or forces a rename in every catalog and every component that uses it. Name the key by purpose, for example checkout.submit, so the text can change without touching the key.