Locales
A site can be written in any number of languages. The first one keeps everything a one-language site has: its files, its addresses, its settings. Every other language gets a folder in content/ and a prefix in the address: the German version of /about might live at /de/uber-uns.
If your site speaks one language, skip this page. Nothing here is on until you add a language, and a site without one builds exactly as before.
Add a language
npx bearminion add locale deUse any lowercase language tag: de, fr, pl, pt-br (see Language codes). The command:
-
declares the languages in
src/lib/content-model.ts, the site’s own language first (langincontent/site.json,enwhen it has none):locales: defineLocales({ default: 'en', list: ['en', 'de'] }), -
copies the home page to
content/de/pages/index.jsonas a draft, and the 404 page next to it; -
copies the theme’s English texts to
src/i18n/de.json, for you to translate (unless the file exists); -
brings the site’s own files up to the ones that publish more than one language (see Upgrading).
Nothing in German is published yet: a language goes live when its home page is published. Run the command again for each language. It names the language it adds (Adding Deutsch (de, German)), so a wrong code shows at once. It stops, before writing anything, when the code is not a language tag or not a language Node knows, is already the site’s language, or would take an address something else has: a page at /de, or a content type called de.
Language codes
A code is a language, optionally followed by a script or a country, all lowercase. Common ones:
| Code | Language | Code | Language |
|---|---|---|---|
en | English | nl | Nederlands (Dutch) |
de | Deutsch (German) | sv | Svenska (Swedish) |
fr | Français (French) | cs | Čeština (Czech) |
es | Español (Spanish) | uk | Українська (Ukrainian) |
it | Italiano (Italian) | ja | 日本語 (Japanese) |
pl | Polski (Polish) | zh-hans | 简体中文 (Simplified Chinese) |
pt | Português (Portuguese) | pt-br | Português (Brasil) |
The full list is the IANA language subtag registry. The command checks the code against the language data Node ships, which covers every language a browser names. For one it does not know, add --force; the switcher then shows the code instead of a name.
Make an existing site multilingual
The whole way from a site in one language to a site in two. The example adds German (de) to an English site; any language tag works the same way.
-
Commit, so git is the undo.
-
Check the site’s own language. It is the Language field on the Site screen,
langincontent/site.json, andenwhen it is not set. That language keeps every address and file the site has now; every language you add goes under a prefix. If the site is not in English, set its real language now, for example:{ "lang": "fr" }Do this before you add a language. Once a second one exists, the field is gone from the editor and the build stops if
langand the content model disagree: changing the main language later means moving every page’s file yourself. -
Install the latest release:
Terminal window npm install bearminion@latest -
Add the language:
Terminal window npx bearminion add locale deIt updates the starter’s files that publish more than one language (one you changed stops it:
--diffshows how), declaresdefineLocales({ default: 'en', list: ['en', 'de'] })insrc/lib/content-model.ts, copies the home page tocontent/de/pages/as a draft, and copies the theme’s English texts tosrc/i18n/de.json. Nothing in German is published yet. -
Add the sitemap line to
astro.config.ts, if the command says it is missing (see The sitemap). A site made from the 1.27 starter has it. -
Translate the home page. Start the site (
npm run dev), open the editor, and pick DE in the language picker above the page. You are on the German home page: translate it, set its status to Published, and save. -
Translate the rest. Open each page you want in German and use Add translation on the Page tab: it copies the page as a German draft, at the address you give it (
/de/uber-unsfor/about). Translate, publish, save. Pages you do not translate stay in the site’s language only; a page can also be German only. -
Translate the site’s texts. With DE picked, the Navigation and Footer screens edit the German menu, buttons and footer; they are saved to
content/site.de.json. The theme’s own labels (Skip to content, Previous, Next, Back to top) are insrc/i18n/de.json: translate that file in your code editor. A site whose own language is not English has no file for it yet: copysrc/i18n/en.jsontosrc/i18n/<its code>.jsonand translate it too, or those labels stay in English on its pages, as they were before.astro devnames the missing file. -
Show the switcher. Turn on Language switcher on the Site screen (in the site’s own language), save, build and deploy.
A site with the hosted editor needs its editor deployment built again after step 4, since its code changed.
Where each language lives
| What | The site’s language | Another language (de) |
|---|---|---|
| Pages, home, 404 | content/pages/<slug>.json at /<slug> | content/de/pages/<slug>.json at /de/<slug> |
| Entries of a type | content/<type>/<slug>.json | content/de/<type>/<slug>.json |
| Singles | content/singles/<id>.json | content/de/singles/<id>.json |
| Site settings | content/site.json | content/site.de.json: only what German changes |
| Theme texts | src/i18n/en.json | src/i18n/de.json |
The site’s own language never moves: its files and addresses stay where they were before you added a second language, so links, bookmarks and search rankings keep working.
Pages in one language or several
A page does not have to exist in every language. Write a page in German only and it is published at /de/... and nowhere else. Leave an English page untranslated and nothing appears under /de/ for it.
Pages that are translations of each other are linked. Linked pages share a translation key in their files, one page per language and content type:
{ "slug": "uber-uns", "title": "Über uns", "translation": "about", "seo": {}, "sections": [] }You rarely write the key by hand. In the editor, open a page and look at Translations on the Page tab:
- Add translation copies the page to the other language as a draft, with the title and address you give it, and links the two.
- Link existing… links the page to one that already exists in the other language.
- Unlink this page from its translations takes it out of the set. The others stay linked to each other.
The home page, the 404 page and singles are linked by their file: content/de/pages/index.json is the German home page. Listing pages of a content type are linked by type.
Renaming a page keeps its links: the key is not the slug.
The language switcher
The switcher is a menu at the end of the header, after its buttons (on phones, next to the Menu button): the open language’s code, and each language by its own name (English, Polski, Deutsch) once opened. It lists every language whose home page is published. From a page with a linked translation it goes to the translation; from any other page, to that language’s home page.
It is off until you turn it on: Language switcher on the Site screen, in the site’s own language. Off hides the switcher only: pages in every language are still built, reachable and in the sitemap. So you can publish a language and show the switcher once it is ready.
The editor has its own language picker above the page. It switches what you edit, the same way: to the open page’s translation, or to the other language’s home page.
Site settings in each language
content/site.de.json holds only what German changes: the site name, the header links and buttons, the footer’s texts, links and columns, the SEO title template and description, the share image, structured data, the logo’s alt text, and the cookie banner’s texts. Everything else (layout, colours, analytics, cookie mode, indexing, the switcher) is one setting for every language. In the editor, settings opened in German save their texts to site.de.json, and a change to a shared setting is refused with a note to make it in the site’s own language. Design is shared outright.
The editor writes only what differs from the site’s own language; going back to the same text removes it again.
Theme texts
Buttons and labels the theme draws itself (Skip to content, Previous, Next, Back to top, the switcher’s label) come from src/i18n/<language>.json. A text a language lacks comes from en.json; astro dev names each missing one once. These are not content: change them in the file, not in the editor.
Not translated yet: the mobile menu’s Menu and Close label is still English in every language.
What gets published
- A draft page is left out of the build in every language, as before.
- A language whose home page is a draft is not published, and the switcher does not offer it.
- A content type’s listing page appears in another language once that language has an entry of the type, or its own listing page.
- The 404 page is the site’s own language’s: a static host serves one. The German 404 file is kept for later.
Search engines
Each page carries <html lang> in its language. Linked pages list each other with hreflang tags, plus x-default on the site’s own language’s page; a page in one language gets none.
The sitemap
astro.config.ts is yours, so the commands never change it. To list each page’s translations in sitemap.xml too, pass the alternates to the sitemap:
import { createSitemapAlternates, createSitemapFilter } from './src/lib/sitemap'
sitemap({ filter: createSitemapFilter(routes), ...createSitemapAlternates(contentModel, routes, content.site.url) })A site made from the 1.27 starter has the line already.
The hosted editor
The hosted editor draws and edits every language. In pull request mode each language’s edits get their own branch and pull request: bearminion/page/de/about, bearminion/site-de. Adding or linking a translation touches pages in two languages, so it goes in one commit, in the bearminion/translations pull request. A translation is listed in the editor once that pull request is merged.
Upgrading
npx bearminion add locale --files-onlyIt brings the starter’s files that publish more than one language up to this release: the route, the content readers, the header and footer, SEO, the switcher, and the theme texts. Files you changed stop it, as with every add. With no language added, the site builds exactly the same pages as before. Then add a language with npx bearminion add locale <code>, and the sitemap line above.
astro dev and astro build say when the model declares languages but the site’s files are older.