feat(docs): persist browser and manual locale preferences (#123)

* feat(docs): persist browser and manual locale preferences

* test(docs): include localized 404 pages in provider coverage

---------

Co-authored-by: dadachann <[email protected]>
This commit is contained in:
Hyu
2026-09-07 00:15:05 +08:00
committed by GitHub
co-authored by dadachann
parent 070ef2cff9
commit 91daf9ecd6
10 changed files with 208 additions and 9 deletions
+23
View File
@@ -0,0 +1,23 @@
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.resolve(import.meta.dirname, "..");
export async function generateLocaleEntry(publicRoot = path.join(root, "dist/public")) {
const helper = await readFile(path.join(root, "src/locale-preference.mjs"), "utf8");
// Inline a classic script: negotiation runs before rendering or module fetches.
const script = helper.replace(/^export /gm, "") + "\nenterDocs();";
const html = `<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>LangBot Docs</title><script>${script}</script></head>
<body><main><h1>LangBot Docs</h1><noscript><p>Please choose a language / 请选择语言 / 言語を選択してください</p></noscript>
<ul><li><a lang="en" href="/docs/en/insight/guide">English</a></li><li><a lang="zh" href="/docs/zh/insight/guide">简体中文</a></li><li><a lang="ja" href="/docs/ja/insight/guide">日本語</a></li></ul>
</main></body></html>
`;
await writeFile(path.join(publicRoot, "index.html"), html);
return html;
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
await generateLocaleEntry();
console.log("Generated browser-negotiated /docs/ entry (langbot-docs-locale)");
}
+1 -2
View File
@@ -133,7 +133,6 @@ function cloudflarePattern(value, destination = false) {
export function renderCloudflareRedirects(docs) {
const lines = [
"/ /en/insight/guide 302",
"/zh/develop/adapter/discord /zh/develop/adapter/discord/README 308",
"/scripts/README-blog-articles /en/articles 308",
];
@@ -206,7 +205,7 @@ export async function prepareFumapress({ root = ROOT, outRoot = root } = {}) {
documents: documents.length,
fallbackDefaults: 0,
localeOnlyDocuments,
redirects: (docs.redirects?.length ?? 0) + 3,
redirects: (docs.redirects?.length ?? 0) + 2,
locales: [...LOCALES],
openapi,
};