astro-i18n-setup Skill
把新网站脚手架变成开箱即用的多语言站点:Astro i18n 路由(默认语言无前缀 + 其他语言带前缀)、Base 布局中的自引用 hreflang + x-default、i18n sitemap 备用链接、语言切换器,以及让测试从首次提交就跨语言保持通过的测试工具改动。在新网站 Q4 = "上线时支持两种及以上语言" 时于脚手架阶段运行,或稍后为默认语言无前缀的单语言站点添加第二种语言(分阶段上线)。触发语句:"多语言站点"、"添加 i18n"、"德语+英语站点"、"国际化"、"hreflang 设置"
安装方式:把技能目录放入 ~/.claude/skills/(Claude Code)或在 claude.ai 设置中启用;也可复制右侧安装命令一键添加。
技能指令原文(SKILL.md)
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:
- 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.
- 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.
- 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. 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):
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 paired 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 BOTH pages the full cluster, including a
self-referencing entry and x-default:
<!-- /ai-events-munich (EN page) -->
<Base title="…" description="…" alternates={[
{ hreflang: 'en', href: '/ai-events-munich' },
{ hreflang: 'de', href: '/ai-treffen-muenchen' },
{ hreflang: 'x-default', href: '/ai-events-munich' },
]}>
<!-- /ai-treffen-muenchen (DE page) -->
<Base lang="de" title="…" description="…" alternates={[
{ hreflang: 'de', href: '/ai-treffen-muenchen' },
{ hreflang: 'en', href: '/ai-events-munich' },
{ hreflang: 'x-default', href: '/ai-events-munich' },
]}>
tests/seo.spec.ts's twin-page reciprocity check (in the default suite) enforces
that both halves 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
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.
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