Skip to content

Commit aad11c9

Browse files
committed
clean code
1 parent 9ea226b commit aad11c9

8 files changed

Lines changed: 32 additions & 69 deletions

File tree

examples/i18n/app/[lang]/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'fumadocs-ui/style.css';
22
import { RootProvider } from 'fumadocs-ui/provider/next';
33
import { Inter } from 'next/font/google';
4-
import { i18nProps } from '@/lib/layout.shared';
4+
import { translations } from '@/lib/layout.shared';
55
import { i18nProvider } from 'fumadocs-ui/i18n';
66

77
const inter = Inter({
@@ -19,7 +19,7 @@ export default async function Layout({ params, children }: LayoutProps<'/[lang]'
1919
minHeight: '100vh',
2020
}}
2121
>
22-
<RootProvider i18n={i18nProvider(i18nProps(lang))}>{children}</RootProvider>
22+
<RootProvider i18n={i18nProvider(translations, lang)}>{children}</RootProvider>
2323
</body>
2424
</html>
2525
);

examples/i18n/lib/layout.shared.tsx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
import type { BaseLayoutProps } from 'fumadocs-ui/layouts/shared';
2-
import type { I18nProviderProps } from 'fumadocs-ui/contexts/i18n';
32
import { i18n } from '@/lib/i18n';
4-
import { translations as zhTW } from '@fumadocs/language/zh-tw';
3+
import { zhTW } from '@fumadocs/language/zh-tw';
4+
import { uiTranslations } from 'fumadocs-ui/i18n';
55

6-
export function i18nProps(locale: string): I18nProviderProps {
7-
const locales = i18n.languages.map((code) => ({
8-
locale: code,
9-
name: code === 'cn' ? 'Chinese' : 'English',
10-
}));
11-
12-
if (locale === 'cn') {
13-
return {
14-
locale,
15-
locales,
16-
translations: {
17-
...zhTW,
18-
'Search(search dialog input placeholder)': 'Translated Content',
19-
},
20-
};
21-
}
22-
23-
return { locale, locales };
24-
}
6+
export const translations = i18n.translations().extend(uiTranslations()).preset('cn', zhTW());
257

268
export function baseOptions(locale: string): BaseLayoutProps {
279
return {

examples/tanstack-start-i18n/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"types:check": "fumadocs-mdx && tsc --noEmit"
1111
},
1212
"dependencies": {
13+
"@fumadocs/language": "workspace:*",
1314
"@orama/tokenizers": "^3.1.18",
1415
"@tanstack/react-router": "1.170.11",
1516
"@tanstack/react-router-devtools": "1.167.0",

examples/tanstack-start-i18n/src/routes/__root.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { createRootRoute, HeadContent, Outlet, Scripts, useParams } from '@tanst
22
import * as React from 'react';
33
import appCss from '@/styles/app.css?url';
44
import { RootProvider } from 'fumadocs-ui/provider/tanstack';
5-
import { i18nProvider } from 'fumadocs-ui/i18n';
5+
import { i18nProvider, uiTranslations } from 'fumadocs-ui/i18n';
66
import { i18n } from '@/lib/i18n';
7+
import { zhTW } from '@fumadocs/language/zh-tw';
78

89
export const Route = createRootRoute({
910
head: () => ({
@@ -32,24 +33,15 @@ function RootComponent() {
3233
);
3334
}
3435

35-
function i18nProps(locale: string) {
36-
const locales = i18n.languages.map((code) => ({
37-
locale: code,
38-
name: code === 'cn' ? 'Chinese' : 'English',
39-
}));
40-
41-
if (locale === 'cn') {
42-
return {
43-
locale,
44-
locales,
45-
translations: {
46-
'Search(search dialog input placeholder)': 'Translated Content',
47-
},
48-
};
49-
}
50-
51-
return { locale, locales };
52-
}
36+
const translations = i18n
37+
.translations()
38+
.extend(uiTranslations())
39+
.preset('cn', zhTW())
40+
.add({
41+
cn: {
42+
'Search(search trigger)': 'Translated Content',
43+
},
44+
});
5345

5446
function RootDocument({ children }: { children: React.ReactNode }) {
5547
const { lang = i18n.defaultLanguage } = useParams({ strict: false });
@@ -60,7 +52,7 @@ function RootDocument({ children }: { children: React.ReactNode }) {
6052
<HeadContent />
6153
</head>
6254
<body className="flex flex-col min-h-screen">
63-
<RootProvider i18n={i18nProvider(i18nProps(lang))}>{children}</RootProvider>
55+
<RootProvider i18n={i18nProvider(translations, lang)}>{children}</RootProvider>
6456
<Scripts />
6557
</body>
6658
</html>

examples/waku-i18n/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"types:check": "fumadocs-mdx && tsc --noEmit"
1111
},
1212
"dependencies": {
13+
"@fumadocs/language": "workspace:*",
1314
"@orama/tokenizers": "^3.1.18",
1415
"fumadocs-core": "workspace:*",
1516
"fumadocs-mdx": "workspace:*",
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
'use client';
22
import type { ReactNode } from 'react';
33
import { RootProvider } from 'fumadocs-ui/provider/waku';
4-
import { i18nProvider } from 'fumadocs-ui/i18n';
4+
import { i18nProvider, uiTranslations } from 'fumadocs-ui/i18n';
55
import { i18n } from '@/lib/i18n';
66
import { useRouter } from 'waku/router/client';
7+
import { zhTW } from '@fumadocs/language/zh-tw';
78

8-
function i18nProps(locale: string) {
9-
const locales = i18n.languages.map((code) => ({
10-
locale: code,
11-
name: code === 'cn' ? 'Chinese' : 'English',
12-
}));
13-
14-
if (locale === 'cn') {
15-
return {
16-
locale,
17-
locales,
18-
translations: {
19-
'Search(search dialog input placeholder)': 'Search (Translated)',
20-
},
21-
};
22-
}
23-
24-
return { locale, locales };
25-
}
9+
const translations = i18n.translations().extend(uiTranslations()).preset('cn', zhTW());
2610

2711
export function Provider({ children }: { children: ReactNode }) {
2812
const router = useRouter();
13+
const lang = router.path.split('/')[1] ?? i18n.defaultLanguage;
2914

30-
return (
31-
<RootProvider i18n={i18nProvider(i18nProps(router.path.split('/')[1] ?? i18n.defaultLanguage))}>
32-
{children}
33-
</RootProvider>
34-
);
15+
return <RootProvider i18n={i18nProvider(translations, lang)}>{children}</RootProvider>;
3516
}

packages/core/src/i18n/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface TranslationPreset<Keys extends string = string> {
4848
value: Partial<Record<Keys, string>>;
4949
}
5050

51-
export interface TranslationsAPI<Languages extends string = string, Keys extends string = never> {
51+
export interface TranslationsAPI<Languages extends string = string, Keys extends string = string> {
5252
/** for type inference only, always `undefined` in runtime */
5353
$inferLanguages: Languages;
5454
/** for type inference only, always `undefined` in runtime */
@@ -63,7 +63,7 @@ export interface TranslationsAPI<Languages extends string = string, Keys extends
6363
/** register allowed translation keys */
6464
extend<const NewKeys extends string>(
6565
extension: TranslationExtension<NewKeys>,
66-
): TranslationsAPI<Languages, Keys | NewKeys[number]>;
66+
): TranslationsAPI<Languages, Keys | NewKeys>;
6767
/** add translations */
6868
add: (overrides: {
6969
[Lang in Languages]?: Partial<Record<Keys, string>>;

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)