返回 CodeWhale
content-locales.ts
根目录 / web / lib / i18n / content-locales.ts
1 import { defaultLocale, locales } from "./config";
2 import { hasComputerUseTranslation } from "./dictionaries";
3
4 /**
5 * Routes whose page-body translation coverage differs from English/Chinese.
6 *
7 * `/docs/guide` is not listed: its French/German/Catalan/Hindi/Turkish/
8 * Italian/Polish/Arabic dictionaries translate only the overview and headings,
9 * while the getting-started steps that make up the body ship in English and
10 * Chinese, so those variants canonicalize to English like any other partial
11 * page.
12 */
13 const ROUTE_CONTENT_LOCALES: Readonly<Record<string, readonly string[]>> = {
14 "/": locales,
15 "/install": ["en"],
16 "/features": ["en"],
17 "/docs/runtime-reference": ["en"],
18 "/docs/workflow-tutorial": ["en"],
19 "/docs/plugin-authoring": ["en"],
20 "/computer-use": locales.filter(hasComputerUseTranslation),
21 };
22
23 /** Most first-party page bodies currently ship in English and Chinese. */
24 const DEFAULT_CONTENT_LOCALES = ["en", "zh"] as const;
25
26 function normalizedPath(path: string): string {
27 if (path === "" || path === "/") return "/";
28 return `/${path.replace(/^\/+|\/+$/g, "")}`;
29 }
30
31 /** Locales with a genuine page-body translation for an indexable route. */
32 export function contentLocalesForPath(path: string): readonly string[] {
33 const normalized = normalizedPath(path);
34 if (normalized.startsWith("/ratatui/")) return ["en"];
35 return ROUTE_CONTENT_LOCALES[normalized] ?? DEFAULT_CONTENT_LOCALES;
36 }
37
38 /**
39 * Canonical locale for a rendered route.
40 *
41 * Partial locale routes stay accessible in the product, but an English-body
42 * fallback points crawlers at the English source instead of presenting a
43 * duplicate as a translated page.
44 */
45 export function canonicalLocaleForPath(path: string, locale: string): string {
46 return contentLocalesForPath(path).includes(locale) ? locale : defaultLocale;
47 }
48
48 lines TYPESCRIPT