i18n layout
Locales, content mirroring, UI strings and hreflang in the docs site.
The configuration
docs/lib/i18n.ts:
export const i18n = defineI18n({
defaultLanguage: 'en',
languages: ['en', 'zh'],
hideLocale: 'never',
parser: 'dir',
});hideLocale: 'never'— both locales are always explicit:/en/…,/zh/…. There is no hidden default locale and no redirect; the root/is a styled language chooser page (docs/app/(entry)/page.tsx) that links to/enand/zhwithhreflangmetadata. Because no prefix-less requests are rewritten, no i18n middleware is needed.parser: 'dir'— content is grouped by language folders (content/docs/en/…,content/docs/zh/…) instead of filename suffixes.
The loader and routing
loader({ i18n }) in docs/lib/source.ts builds one page tree per
language. source.getPage(slugs, lang),
source.getPageTree(lang) and source.generateParams() are all
locale-aware. Page URLs are /<locale>/docs/<slugs>.
Content mirroring
content/docs/zh/ must stay structurally identical to content/docs/en/:
same file paths, same meta.json structure, same frontmatter keys. Until a
page is translated it keeps the English text as a placeholder — the build
is green and the structure is stable; translation is an incremental pass
(see How to translate the docs).
Links between pages are relative .mdx paths, which
createRelativeLink resolves inside the reader's current locale.
UI strings
Global UI strings are translated once, in docs/lib/layout.shared.tsx:
i18n.translations().extend(uiTranslations()).add({ zh: { … } }) covers
the search dialog, table of contents, pagination, page actions, theme and
language switchers, and the 404 page. RootProvider receives
i18nProvider(translations, lang) in app/[lang]/layout.tsx, which also
sets the displayName the language switcher shows.
The language switcher
The switcher renders automatically in the docs and landing headers once
more than one locale is registered with the provider. Its default behavior
replaces the locale prefix of the current pathname — /en/docs/user/lint
→ /zh/docs/user/lint — so switching preserves the page.
Metadata and hreflang
- Landing (
app/[lang]/(home)/page.tsx): canonical/{lang}, withalternates.languagespointing at/enand/zhandx-default→ en. - Docs pages (
generateMetadata): canonical is the page's own URL; the languages map swaps the locale prefix of the same path. - The chooser at
/carries its ownalternates(canonical/, languages/enand/zh,x-default→/). <html lang>follows the[lang]segment.
Search and agents
The built-in search indexes every language with no extra config; the
search dialog filters to the current locale. /llms.txt,
/llms-full.txt and the /<lang>/docs/<path>.md endpoints cover both
locales.
Related
- Why explicit prefixes: i18n strategy
- The stack overview: Docs stack