Localization
Serve a second language with Astro's i18n option, a localized docs tree and localized pages, and see what Publier does not translate or link for you.
Publier has one documentation route, /docs/[...slug], and no locale-aware routing of its own. A second language is a folder of pages plus Astro’s own i18n option. This page shows a setup that Publier’s own end-to-end tests build and check, and lists what you still have to do by hand.
The examples use English as the default language and Chinese (zh) as the second one. Other locale codes should work the same way, but the tests only ran with zh.
Set up a second language
1. Turn on Astro i18n
Add i18n to astro.config.ts. docsShell() stays where it is, first in the integration list:
import { defineConfig } from 'astro/config';
export default defineConfig({ i18n: { defaultLocale: 'en', locales: ['en', 'zh'] }, // integrations, site, and the rest of your config stay as they are});With Astro’s default routing, pages in the default locale keep their normal URLs (/, /docs/quick-start) and every other locale gets its code as a URL prefix (/zh/). The zh segment in /docs/zh/quick-start comes from the docs folder instead (step 3). Astro’s guide describes the other routing options; the tests only used the defaults above.
2. Put localized marketing pages under src/pages/<locale>/
Astro requires the folder name to match an entry in locales exactly. A page there uses BaseLayout like any other custom page:
---import BaseLayout from '@publier/shell/layouts/base-layout.astro';---<BaseLayout title="中文首页" description="第二语言首页"> <main><h1>中文首页</h1><p>这是第二语言页面。</p></main></BaseLayout>BaseLayout sets <html lang> to its lang prop, and when you pass none, to Astro.currentLocale, and when that is empty, to en. With i18n on, /zh/ therefore renders <html lang="zh">. Astro’s own reference says Astro.currentLocale is “the locale computed from the current URL”, and that without a /[locale]/ prefix it defaults to i18n.defaultLocale.
Pass lang yourself when you want a more specific tag than the locale code. An explicit lang wins over the locale:
---import BaseLayout from '@publier/shell/layouts/base-layout.astro';---<BaseLayout title="快速开始" lang="zh-CN" description="安装并运行 Publier 站点。"> <main id="main-content" data-pagefind-body> <h1>快速开始</h1> <p>安装并运行 Publier 站点。</p> </main></BaseLayout>data-pagefind-body puts the page in the search index; see Search.
3. Put the translated docs in docs/<locale>/ and give them a sidebar
The docs route serves every file under src/content/docs/, so a second-language tree is another folder:
├── index.mdx → /docs├── quick-start.mdx → /docs/quick-start└── zh/ ├── index.mdx → /docs/zh └── quick-start.mdx → /docs/zh/quick-startWithout more configuration, these pages share one sidebar with everything else. Add the folder to docs.tabSections so it gets its own:
docs: tabSections: [zh]Give each section a landing page (zh/index.mdx above). The reader of /docs/zh/quick-start then sees only the zh tree in the sidebar, and the English sidebar no longer lists the zh pages. Global settings explains how tabSections chooses the owning section for a page, and what a section without a landing page loses.
With i18n on, /docs/zh/quick-start is rendered with <html lang="zh">. The docs route passes no lang of its own, so BaseLayout falls back to the page’s Astro locale. This was checked with the zh locale and a zh folder directly under docs/; a different folder layout was not tried.
Without Astro i18n
Astro i18n is optional, but the docs tree then cannot carry its own language tag:
- Custom pages work if every one passes an explicit
langtoBaseLayout. A page that omits it is labelleden, whatever its content. - A
docs/<locale>/tree is labelleden. The docs route has nolangprop to set, so it falls back toen. The sidebar still works throughtabSections; only the language tag is wrong.
The tag also decides which search index a page lands in; see below.
Search
Publier’s search is Pagefind. Per Pagefind’s multilingual docs, indexing “will look for a lang attribute on your html element” and then “run independently for each detected language”. A reader on a page tagged zh searches only pages tagged zh.
In practice:
- A localized docs tree with the right
lang(Astroi18non) gets its own index. Withouti18n, it is indexed together with the English pages. - Pages under
/docs/are indexed automatically. Your own.astropages are indexed only if they carrydata-pagefind-body, as in the example above. Search explains the rule. - A test build with one page tagged
lang="zh-CN"produced Pagefind indexes forenandzh-cn.
What Publier does not do
- Built-in labels stay English. The tests check the docs navigation’s accessible name on a
docs/zhpage; it is stillDocumentation navigation. The docs route always uses thedocsnavigation variant, so a translated docs tree shows the same header as the English one. Custom pages can choose a variant with thenavprop; see Navigation. - No
hreflangalternates. Pages emit nohreflanglinks. Search engines are not told that/docs/zh/quick-startis the translation of/docs/quick-start. - No link between a page and its translation. There is no language switcher, and nothing maps a page to its counterpart. If you want one, link the pages yourself.
- No locale-aware docs route. Each language’s docs tree is plain content under one route.
The @astrojs/sitemap integration has its own i18n option for localizing the sitemap. It was not tested with Publier; check Astro’s documentation before relying on it.
Requirements
BaseLayout’s locale handling and lang prop are part of the current release, and the behaviour described here was checked against its framework source.