返回 CodeWhale
fallback.ts
根目录 / web / lib / i18n / fallback.ts
1 import { defaultLocale } from "./config";
2 import { contentLocalesForPath } from "./content-locales";
3 import { EN_CHROME, EN_DOCS_GUIDE, EN_DOCS_SHELL, getChrome, getDocsGuide, getDocsShell } from "./dictionaries";
4
5 export interface FallbackAttrs {
6 lang: "en";
7 dir: "ltr";
8 }
9
10 export const ENGLISH_ATTRS: FallbackAttrs = { lang: "en", dir: "ltr" };
11
12 export const DOCS_TOPICS = [
13 "account",
14 "auth",
15 "claude-code",
16 "computers",
17 "configuration",
18 "constitution",
19 "first-time",
20 "fleet",
21 "guide",
22 "hooks",
23 "mcp",
24 "modes",
25 "pet",
26 "plugin-authoring",
27 "pod",
28 "review",
29 "runtime-api",
30 "runtime-reference",
31 "sandbox",
32 "skills",
33 "subagents",
34 "tools",
35 "troubleshooting",
36 "trust",
37 "vocabulary",
38 "web",
39 "work",
40 "workflow-tutorial",
41 ] as const;
42
43 function normalize(path: string): string {
44 return path === "" || path === "/" ? "/" : `/${path.replace(/^\/+|\/+$/g, "")}`;
45 }
46
47 export function bodyFallsBackToEnglish(path: string, locale: string): boolean {
48 if (locale === defaultLocale) return false;
49 return !contentLocalesForPath(normalize(path)).includes(locale);
50 }
51
52 export function fallbackAttrs(path: string, locale: string): FallbackAttrs | undefined {
53 return bodyFallsBackToEnglish(path, locale) ? ENGLISH_ATTRS : undefined;
54 }
55
56 export function bodyDateLocale(path: string, locale: string): string {
57 return bodyFallsBackToEnglish(path, locale) ? EN_CHROME.dateLocale : getChrome(locale).dateLocale;
58 }
59
60 export function docsChromeFallsBackToEnglish(locale: string): boolean {
61 return locale !== defaultLocale && getDocsShell(locale) === EN_DOCS_SHELL;
62 }
63
64 export function docsEnglishTopics(locale: string): string[] {
65 if (locale === defaultLocale) return [];
66 return ["", ...DOCS_TOPICS].filter((topic) => {
67 if (topic === "guide" && getDocsGuide(locale) !== EN_DOCS_GUIDE) return false;
68 return bodyFallsBackToEnglish(topic ? `/docs/${topic}` : "/docs", locale);
69 });
70 }
71
71 lines TYPESCRIPT