Skip to content

Commit 9e1a6bc

Browse files
committed
更新国际化支持,移除冗余语言配置,添加语言重定向组件
1 parent 15fff92 commit 9e1a6bc

6 files changed

Lines changed: 87 additions & 24 deletions

File tree

objectdocs.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
"i18n": {
99
"enabled": true,
1010
"defaultLanguage": "en",
11-
"languages": [
12-
{ "code": "en", "name": "English" },
13-
{ "code": "cn", "name": "简体中文" }
11+
"languages": ["en", "cn"
1412
]
1513
},
1614
"branding": {
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import 'fumadocs-ui/style.css';
12
import { RootProvider } from 'fumadocs-ui/provider/next';
23
import { defineI18nUI } from 'fumadocs-ui/i18n';
3-
import type { ReactNode } from 'react';
44
import { i18n } from '@/lib/i18n';
5-
import { cn } from '@/lib/i18n-ui';
6-
import { siteConfig } from '@/lib/site-config';
75

86

97
const { provider } = defineI18nUI(i18n, {
@@ -13,23 +11,30 @@ const { provider } = defineI18nUI(i18n, {
1311
},
1412
cn: {
1513
displayName: 'Chinese',
14+
toc: '目錄',
15+
search: '搜尋文檔',
16+
lastUpdate: '最後更新於',
17+
searchNoResult: '沒有結果',
18+
previousPage: '上一頁',
19+
nextPage: '下一頁',
20+
chooseLanguage: '選擇語言',
1621
},
1722
},
1823
});
1924

20-
export default async function LangLayout({
21-
params,
22-
children,
23-
}: {
24-
params: Promise<{ lang: string }>;
25-
children: ReactNode;
26-
}) {
25+
export default async function Layout({ params, children }: LayoutProps<'/[lang]'>) {
2726
const { lang } = await params;
28-
29-
return <RootProvider i18n={provider(lang)}>{children}</RootProvider>;
30-
31-
}
32-
33-
export async function generateStaticParams() {
34-
return i18n.languages.map((lang) => ({ lang }));
27+
return (
28+
<html lang={lang} csuppressHydrationWarning>
29+
<body
30+
style={{
31+
display: 'flex',
32+
flexDirection: 'column',
33+
minHeight: '100vh',
34+
}}
35+
>
36+
<RootProvider i18n={provider(lang)}>{children}</RootProvider>
37+
</body>
38+
</html>
39+
);
3540
}

packages/site/app/[lang]/page.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { redirect, notFound } from 'next/navigation';
2+
13

24
export default async function LangPage({
35
params,
46
}: {
57
params: Promise<{ lang: string }>;
68
}) {
7-
return <></>
8-
;
9-
}
9+
const { lang } = await params;
10+
11+
redirect(`/${lang}/docs`);
12+
}

packages/site/app/page.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { i18n } from '@/lib/i18n';
2+
import { LanguageRedirect } from '@/components/language-redirect';
3+
4+
export default function HomePage() {
5+
return (
6+
<LanguageRedirect
7+
availableLanguages={i18n.languages as string[]}
8+
defaultLanguage={i18n.defaultLanguage}
9+
/>
10+
);
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use client';
2+
3+
import { useRouter } from 'next/navigation';
4+
import { useEffect } from 'react';
5+
6+
interface ClientRedirectProps {
7+
availableLanguages: string[];
8+
defaultLanguage: string;
9+
}
10+
11+
export function LanguageRedirect({ availableLanguages, defaultLanguage }: ClientRedirectProps) {
12+
const router = useRouter();
13+
14+
useEffect(() => {
15+
let preferredLang = defaultLanguage;
16+
17+
// Try to detect user language from browser
18+
if (typeof navigator !== 'undefined') {
19+
const userLangs = navigator.languages || [navigator.language];
20+
21+
for (const lang of userLangs) {
22+
if (!lang) continue;
23+
24+
// Exact match
25+
if (availableLanguages.includes(lang)) {
26+
preferredLang = lang;
27+
break;
28+
}
29+
30+
// Prefix match (e.g. "zh" for "zh-CN")
31+
const match = availableLanguages.find(l =>
32+
l.split('-')[0].toLowerCase() === lang.split('-')[0].toLowerCase()
33+
);
34+
if (match) {
35+
preferredLang = match;
36+
break;
37+
}
38+
}
39+
}
40+
41+
router.replace(`/${preferredLang}/docs`);
42+
}, [availableLanguages, defaultLanguage, router]);
43+
44+
return null;
45+
}

packages/site/lib/i18n.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { defineI18n } from 'fumadocs-core/i18n';
33

44
export const i18n = defineI18n({
55
defaultLanguage: siteConfig.i18n.defaultLanguage,
6-
languages: siteConfig.i18n.languages
6+
languages: siteConfig.i18n.languages,
7+
parser: 'dir',
78
});

0 commit comments

Comments
 (0)