--- name: astro-i18n-setup description: > Turn a new-website scaffold into a turnkey multi-language site: Astro i18n routing (clean default locale + prefixed others), self-referencing hreflang + x-default in the Base layout, i18n sitemap alternates, a language switcher, and the test-harness changes that keep the green-from-commit-1 gate honest across locales. Run at scaffold time when new-website Q4 = "two+ languages at launch", OR later to add a second language to a single-locale site whose default stays unprefixed (phased rollout). Trigger phrases: "multi-language site", "add i18n", "DE+EN site", "internationalization", "hreflang setup", "two languages at launch". --- # Astro i18n setup (scaffold-time, opt-in) Wires turnkey internationalization into the `new-website` Astro overlay. **Opt-in:** the default scaffold stays single-locale. Run it either **at scaffold time** for a 2+-language launch (new-website §1 Q4), **or later** when you add a second language to an existing single-locale site — see **Phased rollout** below. Safe vs unsafe: because the default locale stays **unprefixed** (`/`, not `/en/`), adding a *secondary* prefixed locale (`/de/…`) is purely additive — it never moves an existing URL. What DOES break links/SEO is moving the **default locale itself** off `/` (e.g. retrofitting `/` → `/en/`); this skill never does that, so keep `prefixDefaultLocale: false`. A full re-prefixing of the default is a migration, not this skill. Routing model (house default): **clean default locale, prefixed others** — `/` and `/about` are the default language (marked `x-default`); other languages get a prefix (`/de/`, `/de/about`). `prefixDefaultLocale: false`. This keeps the default-locale URLs identical to a single-locale build, so nothing regresses. Deep SEO rules (reciprocity, x-default, canonical-vs-hreflang, sitemap structure, "translate the whole page not just chrome") live in `seo-audit/references/international-seo.md` — read it, don't re-derive it. ## Phased rollout (primary language first, translate later) You don't have to translate everything before launch. Because the default locale is unprefixed, you can build and ship the **primary language first**, then add the second locale when the translations are ready — without moving a single existing URL: 0. **Phase 0 — vendor this skill NOW.** A declared-multilingual-phased site gets `astro-i18n-setup` copied into its skills dir at scaffold time (new-website §3's conditional copy list) even while running single-locale — Phase 2 below is executed months later by whoever holds the handoff, and the instruction can't resolve if the skill isn't in the project. 1. **Phase 1 — primary only.** Build the site in the default language and ship it. Keep `LOCALES = [DEFAULT_LOCALE]` (one entry). `tests/i18n.spec.ts` **self-skips** while there's a single locale (`LOCALES.length < 2`), so the full QA gate is green with no half-built hreflang. You can skip this skill entirely for now, or apply it with a single-locale `LOCALES` to pre-wire the layout. 2. **Phase 2 — add the locale.** When translating, apply this skill's changes: add the new locale to `astro.config` `i18n.locales`, the `sitemap({ i18n })` map, and `LOCALES`; create the `src/pages//…` pages. hreflang, `x-default`, sitemap alternates and `i18n.spec.ts` now enforce both locales — and the default-language URLs are exactly what they were in Phase 1. The one rule: commit to an **unprefixed default** from day 1 (`prefixDefaultLocale: false`). That's the only decision that's expensive to change later; the translations themselves are always additive. ## Localized 404 The starter's `src/pages/404.astro` serves every unmatched URL, whatever its locale prefix. Translate its strings to the DEFAULT locale's language; a per-locale 404 (`src/pages/de/404.astro`) is optional and needs no routing changes — Cloudflare Pages serves `/de/404` for unmatched `/de/*` paths when it exists (on a Worker the starter's `not_found_handling: "404-page"` serves the nearest `404.html` up the folder tree, from Cloudflare's docs: not checked on a build; a Preview link on a Worker with no 404 file serves a bare "Not found"). Keep any 404 noindex and out of PAGES (@astrojs/sitemap already excludes status-code pages from the sitemap by itself). ## Partial translation (some routes are not translated) Not every route needs every locale. A German company running a DE+EN site whose blog stays German-only, or an English-first site adding one German landing page, does not have to translate everything to keep the i18n gate on. Mark such a route in `src/config.ts`'s `ROUTES` registry (§2) with an explicit `locales` list; a route with no `locales` field exists in every locale (unchanged default): ```ts export const ROUTES: readonly RouteSpec[] = [ { path: '/' }, { path: '/privacy' }, { path: '/blog/some-post', locales: ['de'] }, // German-only, no EN variant ]; ``` One registry drives everything: `tests/_helpers.ts` PAGES (the EN variant is never expected to exist), Base.astro's head hreflang (the route emits one `de` alternate + a self-referencing `x-default` — never a link to a never-built page), the sitemap `serialize` hook (§1 — no advertised-but-missing alternate), and `tests/i18n.spec.ts`'s completeness/x-default/reciprocity/thin-translation checks (all per-route via `routeLocales()`; a route without a default-locale variant gets a deterministic x-default — its first listed locale — on every variant). Google's guidance is explicit that hreflang belongs only on pages that actually have variants (international-seo.md) — this is that rule, made enforceable. ## Light path: twin pages (no prefixed routing) For a handful of translated pages on an otherwise single-language site, the full routing setup above is more machinery than the job needs. The default kit `Base.astro` ships an optional `alternates` prop instead — no astro.config changes, no locale prefixes, no `LOCALES`: Give each version of a page its own localized slug (see "Localized slugs" in `seo-audit/references/international-seo.md`) — e.g. `/ai-events-munich` (EN) and `/ai-treffen-muenchen` (DE) — and pass EVERY version the full cluster, including a self-referencing entry and `x-default`. Two languages are the example; a third or fourth version adds one entry to every version's cluster: ```astro ``` `tests/seo.spec.ts`'s twin-page reciprocity check (in the default suite) enforces that the versions agree: a self-referencing entry equal to the canonical, no dead same-site targets, and A→B implies B→A. Forgetting the cluster on one twin — the exact bug that shipped live on a real site built from this kit (an English event page with no `alternates`, so its German twin's hreflang went unreciprocated, which makes Google ignore the pair) — now fails the build. Also add a visible link to each other version, e.g. `Deutsch`, near the nav/footer: `alternates` only talks to crawlers, not visitors. Pick the heavy path (everything above this section) when most of the site is translated and you want prefixed routing + a language switcher; pick this light path for a few one-off translated pages. Don't mix both on the same SITE: a localized-slug twin can't be expressed in ROUTES (non-default locales map to `/prefix` paths there), so on a heavy site a light-path page fails i18n.spec's completeness check by design. ## Keeping language versions in step Applies to both paths: a page that lists versions in other languages (hreflang) is one page in several languages, and an edit to it is an edit to every version. - **A change to any version goes into every version, in the same pull request.** Not only a change to the main language: sections, facts, numbers, images, dates. The pull request names every version it touched. - **What may differ:** wording and idiom, how a number or date is written, examples chosen for the reader, links to sources in the reader's language, translated anchor ids. - **Before and after an edit, run `npx playwright test tests/language-versions.spec.ts`.** Run it both times, so you can say which differences the edit made and which were there already. Fix a failure. Tell the owner each `⚠ language versions:` warning in chat, one set at a time, in plain words ("The English and German versions of /x now have different numbers: 100 and 150 only on the English page, 80 and 120 only on the German one. Is that meant?"), and ask whether it is meant. Not meant: make the same change on every version. Meant: offer to mark the sentence `data-versions-exempt="why"`, so the owner is not asked again; if the owner declines, ask again next time. List each set, its numbers and the owner's answer in the pull request description. A warning in the run's output is not telling the owner. - **What the test reads, and what it cannot.** It compares every version of a set, any number of them, on counts: it fails a version with more or fewer headings at a level, or more or fewer images, and it warns on numbers that appear a different number of times. It also fails when the versions of a set do not list the same versions. A change that keeps the counts passes: a section replaced by another of the same shape, an image swapped for another, a number moved to another sentence, a reworded sentence with no number. The edit itself must cover those. It reads the body without `nav`, `header`, `footer`, `script`, `style`, `template` and `noscript`. It compares versions on this site only: an alternate on another domain is left out, so check that one by hand. - **`data-versions-exempt="why"`** on a plain HTML element (a `

`, a `

`) for text one language needs, such as a phone line for German callers. The reason is required: an empty one fails the test. The element and everything in it is left out of the comparison. Known false alarms in the number warning, listed though the versions agree: the same number written "1000" in one version and "1.000" in another, a 12-hour time against a 24-hour time, a number written as a word in one language. Write them alike, or mark them. - **Adding a version:** add it to the cluster of every existing version. The test fails when the versions of a set disagree about the set: the new version names the others and one of them does not name it. It cannot see a new version that names only itself and that no other version names (A = {A, B}, B = {A, B}, C = {C} passes: C is in no set). ## What it changes ### 1. `astro.config.mjs` — i18n routing + sitemap alternates Add the `i18n` block (default + launch locales, `prefixDefaultLocale: false`) and give `sitemap()` the matching `i18n` map so it emits `` hreflang alternates — plus a `serialize` hook that filters each entry's alternates down to the locales the route actually exists in (sparse routes: routes given an explicit `locales` list in §2's ROUTES registry — see "Partial translation" above). The hook imports the shared registry from `src/config.ts` (Vite loads the Astro config, so the TS import works). Verbatim config: `references/heavy-path-code.md` §1. Fully-translated sites (no `locales` overrides in ROUTES): the hook is a no-op. ### 2. `src/config.ts` — replace the single locale, add the route registry Replace `SITE.locale` with `LOCALES` / `DEFAULT_LOCALE` / `LOCALE_LABELS`, and add the per-route `ROUTES` registry — THE single registry that Base.astro (head hreflang), astro.config.mjs (sitemap alternates) and the tests all read, so they can't drift apart; a route with NO `locales` field exists in every locale. The block also carries fail-loud import-time validation of the registry (duplicate path, unknown or repeated locale) and the `pathLocale()` / `neutralPath()` / `routeLocales()` helpers everything else imports — apply it whole. Verbatim code: `references/heavy-path-code.md` §2. Remove `SITE.locale`. In `Base.astro`'s Props destructure, `lang = SITE.locale,` becomes `lang: langProp,`: the layout derives the page's language from `Astro.currentLocale` (step 3), and an explicit `lang` prop still wins. Keeping the old default breaks the build; deleting the line leaves `lang` undefined for `inLanguage` and `og:locale`. `src/components/EmailLink.astro` reads `SITE.locale` too: its import of `SITE` becomes `DEFAULT_LOCALE`, and its default `lang = SITE.locale` becomes `lang = Astro.currentLocale ?? DEFAULT_LOCALE`, so a `/de` page's email hint reads "[punkt]". Everything else reads `DEFAULT_LOCALE` / `Astro.currentLocale`. ### 3. `src/layouts/Base.astro` — locale-aware (VERIFIED build output) Add the imports + derive locale/alternates, set `` from the current locale, and emit self-referencing hreflang + `x-default`. Verbatim code, with the hard-won caveats preserved as inline comments: `references/heavy-path-code.md` §3. Build and confirm the output (reciprocal hreflang, and clean trailing slashes under `trailingSlash: 'never'`). The starter's CHROME chrome-strings lookup keys on `SITE.locale`, which §2 removed — rekey it on `Astro.currentLocale ?? DEFAULT_LOCALE` (per-page chrome language IS correct on a locale-routed site, unlike the single-locale starter). Then point each entry at the pages that exist. The default language keeps its unprefixed paths; every other language's entry gets its prefix, because its pages live under `src/pages//`. On the usual site, English first and German added, that is the `de` entry: `privacyHref: '/de/privacy'` and `imprintHref: '/de/impressum'` (its single-locale values, `/datenschutz` and `/impressum`, are 404s there). With German as the default (§5), `de` gets `/privacy` and `/impressum` (ROUTES has no `/datenschutz`), and `en` gets `/en/privacy` and `/en/legal-notice`. The template's LIGHT-path pieces are superseded on a heavy site: **delete the `{alternates.map(…)}` render line** (the hreflang cluster from `references/heavy-path-code.md` §3 replaces it) — the `alternates` prop and `altHref` helper then sit unused; remove them too or leave them, but never feed both emission paths on one page. The starter's `Astro.locals.lang = lang;` line becomes `Astro.locals.lang = currentLocale;` (in the §3 code), so a contact form on the page speaks the page's language. Keep the `ogLocale` line (config.ts's shared `ogLocaleFor` maps `lang → og:locale`); optionally add `og:locale:alternate` for the non-current locales. `inLanguage` in the WebPage/WebSite schema read `lang`, which §3's code sets to the current locale. ### 4. `src/components/LanguageSwitcher.astro` (new) Copy the ready component from `references/LanguageSwitcher.astro` into `src/components/` and render it on every page: the starter has no header or nav, so put `` inside the `