Skip to content

Commit a7dbb55

Browse files
Copilothuangyiirene
andcommitted
Set up Fumadocs with internationalization support
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent bdc276c commit a7dbb55

24 files changed

Lines changed: 6344 additions & 0 deletions

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# dependencies
2+
/node_modules
3+
/.pnp
4+
.pnp.js
5+
6+
# testing
7+
/coverage
8+
9+
# next.js
10+
/.next/
11+
/out/
12+
13+
# production
14+
/build
15+
16+
# misc
17+
.DS_Store
18+
*.pem
19+
20+
# debug
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
# local env files
26+
.env*.local
27+
28+
# vercel
29+
.vercel
30+
31+
# typescript
32+
*.tsbuildinfo
33+
next-env.d.ts
34+
35+
# fumadocs
36+
.source
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { source } from '@/lib/source';
2+
import type { Metadata } from 'next';
3+
import { DocsPage, DocsBody } from 'fumadocs-ui/page';
4+
import { notFound } from 'next/navigation';
5+
6+
interface PageProps {
7+
params: Promise<{
8+
lang: string;
9+
slug?: string[];
10+
}>;
11+
}
12+
13+
export default async function Page({ params }: PageProps) {
14+
const { lang, slug = [] } = await params;
15+
16+
const page = source.getPage(slug, lang);
17+
18+
if (!page) {
19+
notFound();
20+
}
21+
22+
const MDX = page.data.body as any;
23+
24+
return (
25+
<DocsPage toc={page.data.toc as any} full={page.data.full as any}>
26+
<DocsBody>
27+
<h1>{page.data.title}</h1>
28+
<MDX />
29+
</DocsBody>
30+
</DocsPage>
31+
);
32+
}
33+
34+
export async function generateStaticParams() {
35+
return source.generateParams();
36+
}
37+
38+
export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
39+
const { lang, slug = [] } = await params;
40+
const page = source.getPage(slug, lang);
41+
42+
if (!page) notFound();
43+
44+
return {
45+
title: page.data.title,
46+
description: page.data.description,
47+
};
48+
}

app/[lang]/docs/layout.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { source } from '@/lib/source';
2+
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
3+
import type { ReactNode } from 'react';
4+
import { baseOptions } from '@/app/layout.config';
5+
6+
export default async function Layout({
7+
params,
8+
children,
9+
}: {
10+
params: Promise<{ lang: string }>;
11+
children: ReactNode;
12+
}) {
13+
const { lang } = await params;
14+
15+
return (
16+
<DocsLayout
17+
tree={source.getPageTree(lang)}
18+
{...baseOptions}
19+
>
20+
{children}
21+
</DocsLayout>
22+
);
23+
}

app/[lang]/layout.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { I18nProvider } from 'fumadocs-ui/contexts/i18n';
2+
import { defineI18nUI } from 'fumadocs-ui/i18n';
3+
import type { ReactNode } from 'react';
4+
import { i18n } from '@/lib/i18n';
5+
import { cn } from '@/lib/i18n-ui';
6+
7+
const translations = {
8+
'zh-CN': cn,
9+
};
10+
11+
const i18nConfig = defineI18nUI(i18n, {
12+
translations: {
13+
en: {
14+
displayName: 'English',
15+
},
16+
'zh-CN': {
17+
...cn,
18+
displayName: '简体中文',
19+
},
20+
},
21+
});
22+
23+
export default async function LangLayout({
24+
params,
25+
children,
26+
}: {
27+
params: Promise<{ lang: string }>;
28+
children: ReactNode;
29+
}) {
30+
const { lang } = await params;
31+
const providerProps = i18nConfig.provider(lang);
32+
33+
return <I18nProvider {...providerProps}>{children}</I18nProvider>;
34+
}
35+
36+
export async function generateStaticParams() {
37+
return i18n.languages.map((lang) => ({ lang }));
38+
}

app/[lang]/page.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { redirect } from 'next/navigation';
2+
3+
export default async function LangPage({
4+
params,
5+
}: {
6+
params: Promise<{ lang: string }>;
7+
}) {
8+
const { lang } = await params;
9+
redirect(`/${lang}/docs`);
10+
}

app/layout.config.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { type BaseLayoutProps } from 'fumadocs-ui/layouts/shared';
2+
3+
export const baseOptions: BaseLayoutProps = {
4+
nav: {
5+
title: 'My Docs',
6+
},
7+
links: [
8+
{
9+
text: 'Documentation',
10+
url: '/docs',
11+
active: 'nested-url',
12+
},
13+
],
14+
};

app/layout.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'fumadocs-ui/style.css';
2+
import { RootProvider } from 'fumadocs-ui/provider/next';
3+
import type { ReactNode } from 'react';
4+
5+
export default function RootLayout({ children }: { children: ReactNode }) {
6+
return (
7+
<html lang="en" suppressHydrationWarning>
8+
<body className="flex flex-col min-h-screen">
9+
<RootProvider>{children}</RootProvider>
10+
</body>
11+
</html>
12+
);
13+
}

app/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { redirect } from 'next/navigation';
2+
3+
export default function HomePage() {
4+
redirect('/en/docs');
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Getting Started
3+
description: Learn how to get started with this documentation
4+
---
5+
6+
# Getting Started
7+
8+
This guide will help you get started with our documentation.
9+
10+
## Installation
11+
12+
To use this documentation site locally:
13+
14+
```bash
15+
npm install
16+
npm run dev
17+
```
18+
19+
## Navigation
20+
21+
Use the sidebar to navigate through different sections. The search bar at the top allows you to quickly find specific topics.
22+
23+
## Language Support
24+
25+
Click on the language selector in the navigation bar to switch between English and Chinese.

content/docs/en/index.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Welcome
3+
description: Welcome to the documentation
4+
---
5+
6+
# Welcome to Our Documentation
7+
8+
This is a documentation site built with Fumadocs that supports internationalization.
9+
10+
## Features
11+
12+
- **Multi-language Support**: Switch between English and Chinese
13+
- **Fast Search**: Quick search across all documentation
14+
- **Beautiful UI**: Modern and responsive design
15+
- **MDX Support**: Write docs with MDX for interactive content
16+
17+
## Getting Started
18+
19+
Explore the documentation using the sidebar navigation. You can switch languages using the language selector in the top navigation bar.

0 commit comments

Comments
 (0)