--- name: localization description: > Implement or extend localization (i18n) for a static, SSR or client-rendered website, or a native app sharing its links, using one canonical pattern — locale-prefixed URLs, locale inferred from a saved choice then the browser/OS languages, a language toggle in the UI, hreflang/SEO, and typed translated strings. Use whenever the user adds a language, translates pages or UI copy, builds or moves a language switcher/toggle, touches Accept-Language or navigator.languages detection, locale redirects or edge middleware, hreflang/og:locale/html lang, per-language routes, slugs, RSS or sitemaps — even if they only say "make this page work in Spanish" or "add an English version". --- # Localization One pattern for static sites, SSR apps, client-rendered SPAs and the native apps that share their links. The **core** (URL scheme, precedence, persistence, toggle behavior) must behave identically in every app. Only the **adapters** (where detection runs, how the page renders its locale) change per stack. - Read `references/core-module.md` when creating or diffing `src/i18n/locale.ts`. - Read `references/stack-adapters.md` when wiring detection, rendering or the toggle. - On a site with theme switching, follow `light-dark-mode` for the theme boot script and toggle; this skill only places the language toggle next to it. ## Phase 0 — Understand the target 1. **Existing i18n:** grep for an i18n library (`i18next`, `react-intl`, `next-intl`, `vue-i18n`, Astro `i18n` config), `[lang]`/`[locale]` route folders, `Accept-Language` or `navigator.languages` reads, locale cookies or localStorage keys, a language switcher, and hreflang tags. Note what each one controls. 2. **Rendering and host:** static, SSR or client-rendered SPA, and whether the host runs middleware. This picks the detection adapter (see *Where detection runs*). 3. **Locales:** the default locale, the other locales, and whether content uses shared or per-language slugs. 4. **Sibling apps:** other apps on the same registrable domain (blog subdomain, native app) that must share the `locale` cookie and the `/{locale}` link shape. 5. **Deviations** (existing i18n): list every place the app breaks the rules below (`?lang=`, 301 redirects, a localStorage-only choice, hand-written prefixes, flags, a toggle in the header). Fix the ones you touch, replace the old mechanism instead of running two, and carry the rest into the report. ## Phase 1 — Implement Apply *The model* and *Rules, and why* throughout, in this order: 1. **Core module:** see *Core module* and *URL and link arrangement*. 2. **Detection:** see *Where detection runs*. 3. **Toggle:** see *Language toggle*. 4. **Strings and formatting:** see *Strings and formatting*. 5. **Per-language slugs** (only when content uses them): see *Content with per-language slugs*. 6. **SEO and metadata:** see *SEO and metadata checklist*. 7. **Testing:** see *Testing*. When adding a locale to an app that already localizes, follow *Adding a locale*. For a native app sharing the links, also apply *Native apps*. ### The model ``` request /about?utm=x │ ├─ path has a locale prefix (/es/…)? ── yes ─► render that locale. Never redirect away. ├─ exempt path (API, assets, dev pages, per-language-slug content)? ── yes ─► render as-is │ ├─ saved choice: cookie `locale` ─┐ ├─ else browser/OS: Accept-Language | navigator.languages ├─► target locale └─ else DEFAULT_LOCALE ─┘ │ └─ target ≠ default ─► 307 / location.replace → /es/about?utm=x ``` ### Rules, and why 1. **The URL is the source of truth for what a page renders.** Detection only decides where an *unprefixed* first visit lands. A shared `/es/...` link must open in Spanish for everyone, and crawlers (no cookie, no Accept-Language) must be able to reach every language. 2. **Default locale unprefixed, every other locale under `/{locale}`.** `/about` = English, `/es/about` = Spanish. One deploy, one cookie scope, shareable and crawlable URLs. No `?lang=` (poorly indexed, lost on copy) and no same-URL content negotiation (breaks caching and sharing). 3. **Precedence: URL → saved choice → browser/OS languages → default.** An explicit choice always beats inference, and inference beats a guess. 4. **Negotiate in the user's priority order.** Walk the language list (Accept-Language sorted by `q`, or `navigator.languages`) and take the first entry whose base language you support. `sv, es;q=0.8` → `es`; `en-US, es` → `en`. Looking at only the first entry, or at *any* entry, both get real users wrong. 5. **Redirect only unprefixed → prefixed, temporarily, preserving `?query#hash`.** Use 307 (edge) or `location.replace` (client), never 301: the target depends on who's asking, and a cached permanent redirect traps people in the wrong language. Never let a CDN cache the redirect. 6. **Persist the choice in one cookie, `locale`.** Edge code can read cookies; localStorage is invisible to the edge and scoped to a single origin. Set `Domain` to the registrable domain so sibling apps (e.g. site + blog subdomain) share one choice. 7. **Any link that switches language persists the choice on `click`.** Not `mousedown`: keyboard activation never fires it. 8. **Never hand-write locale prefixes.** All internal links go through `localizePath()`, and the toggle target comes from `switchLocalePath()`. Duplicated regexes in components drift apart. 9. **Everything locale-specific derives from `pageLocale(pathname)`:** ``, `og:locale`, hreflang, feeds, date formats, form payloads, 404 page. ### URL and link arrangement | Resource | Default (`en`) | Other (`es`) | |---|---|---| | Home | `/` | `/es` | | Page with a shared slug | `/about` | `/es/about` | | Content with per-language slugs | `/posts/what-is-an-algorithm` | `/es/posts/que-es-un-algoritmo` | | Feed | `/rss.xml` | `/es/rss.xml` | | 404 | `/404` | `/es/404` | | API, assets, dev-only pages | `/api/*`, `/assets/*`, `/tokens` | not localized | | Cross-app link | `https://blog.example.com/` | `https://blog.example.com/es` | The default locale is never prefixed (`/en/about` is a 404, not an alias). Pick one trailing-slash form per app (e.g. `/es` on an SPA, `/es/` on static directory output) and use it consistently in canonical, hreflang and the toggle. If the host serves `/es/`, change the root case in `localizePath`. ### Core module `src/i18n/locale.ts` holds the config (`LOCALES`, `DEFAULT_LOCALE`, `LOCALE_COOKIE`, `COOKIE_DOMAIN`, `EXEMPT_PATHS`, `LANGUAGE_NAMES`, `OG_LOCALES`) and the pure functions `isLocale`, `localePrefix`, `pageLocale`, `stripLocale`, `localizePath`, `switchLocalePath`, `parseAcceptLanguage`, `negotiateLocale`, `resolveLocaleRedirect`, `readCookie`, `alternateLinks`. `src/i18n/persistLocale.ts` is the browser-only cookie writer. Copy both from `references/core-module.md` as-is and change only the config block. Never re-implement any of this logic inside an adapter or component. ### Where detection runs Detection must finish before first paint, or Spanish visitors see an English flash. - **Preferred: at the edge or on the server.** Whenever the host runs middleware (Vercel Middleware, Netlify Edge Functions, Cloudflare Workers) or the app renders on the server (SSR), redirect there with a 307, `Cache-Control: private, no-store` and `Vary: Cookie, Accept-Language`, before any HTML is sent. - **Fallback: blocking boot script.** On a purely static host, a blocking `