返回提示词目录第 1381 页

搭建应用多语言路由与消息配置

为应用接入多语言支持,完成请求级语言解析、路由与消息配置,并验证并发隔离。

完整提示词

<context>
SvelteKit has no built-in i18n, and the server side is where setups break. Libraries built around a global writable store for the current locale are fine in a browser but on the server that store is shared by every concurrent request, so one user can get another user's language; the locale must be resolved per request in `hooks.server` and kept in `event.locals` or the library's request-scoped mechanism. `app.html` has a fixed `<html lang>`, so it needs a placeholder that `transformPageChunk` replaces per request. Duplicating route folders per language is unmaintainable; the `reroute` hook (or an optional `[[lang]]` segment) maps `/de/about` to the same route. Prerendered pages only exist for URLs the crawler reaches, so other locales need explicit `entries` or links to them. Compiled-message libraries such as Paraglide JS turn each message into a typed function that is tree-shaken per page, while runtime libraries such as svelte-i18n load JSON and format at runtime; the choice affects bundle size, type safety and how translators' files look.
</context>

<task>
Set up i18n in this SvelteKit project:

{{project}}

Locales: {{locales}}.
{{#library}}Library: {{library}}.{{/library}}

1. Run the build, `svelte-check` and tests and record the baseline. Stop and report if they fail.
2. Read `svelte.config`, `hooks.server`, `hooks` (universal), `app.html`, root layouts and any i18n code. Record which routes are prerendered.
3. Library choice: use the installed library, or the one given. If none, recommend one in two sentences (compiled typed messages versus runtime JSON) and use it. Check its current documentation for the setup API; it has changed between major versions.
4. Locale resolution: in `hooks.server`, resolve the locale per request from the URL prefix, then a cookie, then `Accept-Language`, store it in `event.locals` (typed in `app.d.ts`) or the library's request context, and replace an `%lang%` placeholder in `app.html` through `transformPageChunk`, setting `dir` too for right-to-left locales. Never keep the server-side locale in module-level state.
5. Routing: use the `reroute` hook or an optional locale segment so one route tree serves every locale; add a helper that builds localized hrefs, and a language switcher that keeps the current path. Redirect only the bare root by preference, never deep links.
6. Prerendering: make every locale's prerendered pages reachable through `prerender.entries` or crawlable links, and confirm they appear in the build output.
7. Messages: one catalog per locale, loaded or compiled so each page gets only its messages; plurals in the library's ICU or plural syntax for each locale's CLDR categories; dates and numbers through `Intl` with the resolved locale.
8. SEO: in the root layout's `svelte:head`, add hreflang alternates and a canonical link per locale.
9. Convert a small representative slice (layout, one page, one component with a plural) to prove the setup and list remaining hard-coded strings for later.
10. Run the build, `svelte-check` and tests again; request each locale's root and one deep link, check `html lang`, and fire two concurrent requests with different locales against the server build to confirm there is no cross-request leak.
</task>

<constraints>
- No module-level mutable locale on the server.
- Do not duplicate route folders per language.
- Do not invent translations; other locales start untranslated or fall back.
- No route that was prerendered in the baseline may stop being prerendered without a stated reason.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Baseline
Build, checks, tests and prerendered routes before changes.

## Library choice
The library, version and reason.

## Locale resolution
`hooks.server`, `app.d.ts`, `app.html` changes.

## Routes and prerendering
Reroute or segment, href helper, switcher, prerender entries.

## Messages
Catalog layout, plural examples and formatting.

## Verification
Commands and requests with real results, including the concurrency check.

## Follow-ups
Remaining strings and decisions.
</output_format>

可替换内容

  • {{project}}
  • {{locales}}
  • {{#library}}
  • {{library}}
  • {{/library}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}
  • [[lang]

分类与标签

编程开发、应用国际化、多语言路由、服务端渲染、并发隔离

前往 AI 图片创作