Add or remove a language
Everything that knows about languages reads src/i18n/locales.ts: Payload's localization, routing, the language switcher, hreflang, the sitemap and the translation tools. Adding a language starts with one entry there; removing one or changing the default is an edit to the same list. Swedish as an example:
1. The entry #
export const locales = [
{ code: 'en', label: 'English', hreflang: 'en', ogLocale: 'en_US', isDefault: true },
{ code: 'da', label: 'Dansk', hreflang: 'da', ogLocale: 'da_DK', region: 'DK' },
{ code: 'de', label: 'Deutsch', hreflang: 'de', ogLocale: 'de_DE', region: 'DE' },
{ code: 'sv', label: 'Svenska', hreflang: 'sv', ogLocale: 'sv_SE', region: 'SE' },
] as const satisfies readonly LocaleDef[]
Languages explains each field. Restart the development server. The site now answers at /sv/…, the admin's language picker offers Svenska, and every text falls back to English until it is translated.
On PostgreSQL the languages are part of the database schema: create and run a migration (pnpm payload migrate:create, then pnpm payload migrate). MongoDB needs nothing.
2. Interface texts #
Copy src/messages/en.json to src/messages/sv.json and translate the values; keep the keys. Without the file, Swedish visitors read the default language's interface texts (and English's where the default language has no file either: en.json lists every key), and editors can still override any of them in Microcopy.
src/utilities/microcopy.test.ts checks every file in src/messages against en.json, so a missing key fails the test.
3. Words in addresses #
Collection addresses have a word per language in the registry (src/registry/index.ts): segments such as { en: 'news', da: 'nyheder', de: 'neuigkeiten' }, facet words such as topic, and the labels cards show. Add the new language's word to each; without it, its addresses use the default language's word (/sv/news).
Slugs are made from titles by src/utilities/slugify.ts, which spells out Nordic and other Latin letters with accents (ø becomes oe) and drops anything else. For a language in another script, add its letters to TRANSLIT, or editors type the slugs themselves.
4. Translation #
The AI translation needs nothing for a new language: it names languages with the browser's own language names. One optional tuning: if the language writes compounds as one word, as German and the Nordic languages do, add it to LANGUAGE_TRAITS in src/utilities/translate/languages.ts, so the glossary check finds a term inside a longer word.
Then, in the admin, editors can:
- give the language its own notes and examples in the Translation style guide, and its decisions in the Glossary;
- translate the site with ✨ Translate or "Translate the whole site" in Translation status;
- import existing translations under Import translations.
5. The demo and the tests #
The demo seed writes English and Danish, and German where it has recorded translations; a new language starts empty there, for demo visitors to translate into. The tests need nothing: the integration and end-to-end tests run with the site's own languages, named by their part (the default language and the others, see Testing).
Remove a language #
Delete its entry from locales. That is all the code needs:
- Its words can stay. The registry's words are kept per language code, for languages a site does not have too; the kit ships English, Danish and German ones, and a language not in the list is never read.
src/messages/<code>.jsoncan stay or go. - Its addresses are gone:
/de/…is not found, and the sitemap,hreflangand the language switcher leave the language out. Point old addresses elsewhere in Redirects if they had visitors. - On MongoDB its content stays in the documents, unread, and is there again if the language comes back.
- On PostgreSQL the languages are part of the database schema: create a migration (
pnpm payload migrate:create remove-german), commit it and run it. The migration refuses while the database holds content in that language (invalid input value for enum _locales): a new site never has any, but on a site with content in the language, that content has to be deleted first.
Change the default language #
Move isDefault: true to another entry; exactly one has it. Any language can be the default. It is the language that:
- has no address prefix: with Danish the default,
/om-osis Danish and English moves to/en/about; - is the source of every translation, and the fallback for anything not translated;
- gives internal paths their words. A document's internal path (
buildHref) and a link to a "Site page" are in the default language:/newswith English the default,/nyhederwith Danish. The "Site page" options insrc/payload-types.tsfollow, so runpnpm generate:types(as after any change to the list).
Choose the default before there is content. Changing it on a site with content is more than an edit: what editors wrote stays in the language they wrote it in, so the new default language starts from what it has, translations were made from the old one, and saved "Site page" links name the old default's words. On PostgreSQL the migration refuses while they do.
The demo (pnpm seed:demo) is written in English and needs English as the default language: with another default it refuses and changes nothing, and the tests that run it skip. The kit's own CI checks every change with Danish the default and German removed, as well as with the list it ships with.