From c15980b32a61f3f133a45ea7b914b47932c817e2 Mon Sep 17 00:00:00 2001 From: javayhu Date: Sat, 29 Mar 2025 12:32:25 +0800 Subject: [PATCH] feat: update documentation and enhance internationalization support - Updated documentation links in content-collections.ts for accuracy. - Modified index.mdx and test.mdx files to improve content structure and clarity. - Added new Chinese documentation file (index.zh.mdx) to support localization. - Enhanced layout.tsx to include translations for English and Chinese. - Updated page.tsx to handle locale parameters for improved routing. - Refactored source.ts to streamline imports and maintain consistency. --- content-collections.ts | 3 ++- content/docs/index.mdx | 8 +++++-- content/docs/index.zh.mdx | 8 +++++++ content/docs/test.mdx | 20 ++++++++++------- src/app/[locale]/docs/[[...slug]]/page.tsx | 19 +++++++++------- src/app/[locale]/docs/layout.tsx | 26 +++++++++++++++++++--- src/lib/docs/source.ts | 2 +- 7 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 content/docs/index.zh.mdx diff --git a/content-collections.ts b/content-collections.ts index 0e51ed3..9b51996 100644 --- a/content-collections.ts +++ b/content-collections.ts @@ -24,7 +24,8 @@ import { /** * Fumadocs documentation - * 1. https://fumadocs.com/docs/configuration + * + * https://fumadocs.vercel.app/docs/headless/content-collections */ const docs = defineCollection({ name: 'docs', diff --git a/content/docs/index.mdx b/content/docs/index.mdx index 986a7fa..b8ee9c9 100644 --- a/content/docs/index.mdx +++ b/content/docs/index.mdx @@ -3,11 +3,15 @@ title: Hello World description: Your first document --- -Welcome to the docs! You can start writing documents in `/content/docs`. +Hey there! -## What is Next? +## Heading + +### Heading + +#### Heading diff --git a/content/docs/index.zh.mdx b/content/docs/index.zh.mdx new file mode 100644 index 0000000..f059ac1 --- /dev/null +++ b/content/docs/index.zh.mdx @@ -0,0 +1,8 @@ +--- +title: 中文 +description: 您的第一個文檔 +--- + +## Hi 中文 + +Fumadocs 對 i18n 有良好的支持 diff --git a/content/docs/test.mdx b/content/docs/test.mdx index d1ee3a8..ec5c7bc 100644 --- a/content/docs/test.mdx +++ b/content/docs/test.mdx @@ -1,17 +1,21 @@ --- -title: Components -description: Components +title: Test Document +description: Your first document --- -## Code Block +Hey there! -```js -console.log('Hello World'); -``` - -## Cards +## Heading + +### Heading + +```js +console.log('Hello World'); +``` + +#### Heading diff --git a/src/app/[locale]/docs/[[...slug]]/page.tsx b/src/app/[locale]/docs/[[...slug]]/page.tsx index ed5fb97..db248b4 100644 --- a/src/app/[locale]/docs/[[...slug]]/page.tsx +++ b/src/app/[locale]/docs/[[...slug]]/page.tsx @@ -9,12 +9,14 @@ import { } from 'fumadocs-ui/page'; import type { Metadata } from 'next'; import { notFound } from 'next/navigation'; +import { Locale } from 'next-intl'; -export default async function Page(props: { - params: Promise<{ slug?: string[] }>; +export default async function Page({ + params, +}: { + params: { slug?: string[]; locale: Locale }; }) { - const params = await props.params; - const page = source.getPage(params.slug); + const page = source.getPage(params.slug, params.locale); if (!page) notFound(); return ( @@ -40,11 +42,12 @@ export function generateStaticParams() { return source.generateParams(); } -export async function generateMetadata(props: { - params: Promise<{ slug?: string[] }>; +export async function generateMetadata({ + params, +}: { + params: { slug?: string[]; locale: Locale }; }) { - const params = await props.params; - const page = source.getPage(params.slug); + const page = source.getPage(params.slug, params.locale); if (!page) notFound(); return { diff --git a/src/app/[locale]/docs/layout.tsx b/src/app/[locale]/docs/layout.tsx index 6a7ad55..0ffad29 100644 --- a/src/app/[locale]/docs/layout.tsx +++ b/src/app/[locale]/docs/layout.tsx @@ -9,7 +9,7 @@ import { I18nProvider } from 'fumadocs-ui/i18n'; import '@/styles/docs.css'; -const cn: Partial = { +const zhTranslations: Partial = { toc: '目录', search: '搜索文档', lastUpdate: '最后更新于', @@ -19,6 +19,22 @@ const cn: Partial = { chooseLanguage: '选择语言', }; +const enTranslations: Partial = { + toc: 'Table of Contents', + search: 'Search docs', + lastUpdate: 'Last updated on', + searchNoResult: 'No results', + previousPage: 'Previous', + nextPage: 'Next', + chooseLanguage: 'Select language', +}; + +// Map of locale to translations +const translations: Record> = { + zh: zhTranslations, + en: enTranslations, +}; + // available languages that will be displayed on UI // make sure `locale` is consistent with your i18n config const locales = [ @@ -28,7 +44,7 @@ const locales = [ }, { name: 'Chinese', - locale: 'cn', + locale: 'zh', }, ]; @@ -42,7 +58,11 @@ export default async function DocsRootLayout({ children, params }: DocsLayoutPro return ( - + {children} diff --git a/src/lib/docs/source.ts b/src/lib/docs/source.ts index c52314f..4eeddae 100644 --- a/src/lib/docs/source.ts +++ b/src/lib/docs/source.ts @@ -1,6 +1,6 @@ +import { createMDXSource } from '@fumadocs/content-collections'; import { allDocs, allMetas } from 'content-collections'; import { loader } from 'fumadocs-core/source'; -import { createMDXSource } from '@fumadocs/content-collections'; import { docsI18nConfig } from './i18n'; export const source = loader({