@@ -49,24 +49,6 @@ export const source = loader({
49
49
});
50
50
```
51
51
52
- Update the usages to your source to include a locale code:
53
-
54
- ``` ts
55
- import { source } from ' @/lib/source' ;
56
-
57
- // get page tree
58
- source .pageTree [params .lang ];
59
-
60
- // get page
61
- source .getPage (params .slug , params .lang );
62
-
63
- // get pages
64
- source .getPages (params .lang );
65
- ```
66
-
67
- Note that without providing a locale code, it uses your default locale instead.
68
- You can see [ Source API] ( /docs/headless/source-api ) for other usages.
69
-
70
52
### Middleware
71
53
72
54
Create a middleware that redirects users to appropriate locale.
@@ -83,28 +65,57 @@ Create a middleware that redirects users to appropriate locale.
83
65
84
66
See [ Middleware] ( /docs/headless/internationalization#middleware ) for customisable options.
85
67
86
- ### Root Layout
68
+ ### Routing
87
69
88
70
Create a dynamic route ` /app/[lang] ` , and move all special files from ` /app ` to
89
71
the folder.
90
72
91
- A ` I18nProvider ` is needed for localization. Wrap the root provider inside your I18n provider.
73
+ A ` I18nProvider ` is needed for localization.
74
+ Wrap the root provider inside your I18n provider, and provide available languages & translations to it.
75
+
76
+ Note that only English translations are provided by default.
92
77
93
78
``` tsx
94
79
import { RootProvider } from ' fumadocs-ui/provider' ;
95
- import { I18nProvider } from ' fumadocs-ui/i18n' ;
80
+ import { I18nProvider , type Translations } from ' fumadocs-ui/i18n' ;
81
+
82
+ const cn: Translations = {
83
+ search: ' Translated Content' ,
84
+ // other props
85
+ };
96
86
97
- export default function RootLayout({
87
+ // available languages that will be displayed on UI
88
+ // make sure `locale` is consistent with your i18n config
89
+ const locales = [
90
+ {
91
+ name: ' English' ,
92
+ locale: ' en' ,
93
+ },
94
+ {
95
+ name: ' Chinese' ,
96
+ locale: ' cn' ,
97
+ },
98
+ ];
99
+
100
+ export default async function RootLayout({
98
101
params ,
99
102
children ,
100
103
}: {
101
- params: { lang: string };
104
+ params: Promise < { lang: string }> ;
102
105
children: React .ReactNode ;
103
106
}) {
104
107
return (
105
- <html lang = { params .lang } >
108
+ <html lang = { ( await params ) .lang } >
106
109
<body >
107
- <I18nProvider locale = { params .lang } >
110
+ <I18nProvider
111
+ locale = { (await params ).lang }
112
+ locales = { locales }
113
+ translations = {
114
+ {
115
+ cn ,
116
+ }[(await params ).lang ]
117
+ }
118
+ >
108
119
<RootProvider >{ children } </RootProvider >
109
120
</I18nProvider >
110
121
</body >
@@ -113,47 +124,61 @@ export default function RootLayout({
113
124
}
114
125
```
115
126
116
- ### Writing Documents
127
+ ### Source
117
128
118
- see [ Page Conventions ] ( /docs/ui/page-conventions#internationalization ) to learn how to organize your documents.
129
+ Update the usages to your source to include a locale code:
119
130
120
- ### Search
131
+ ``` ts
132
+ import { source } from ' @/lib/source' ;
121
133
122
- Configure i18n on your search solution.
134
+ // get page tree
135
+ source .pageTree [params .lang ];
123
136
124
- You don't need further changes if you're using the ` createFromSource ` shortcut.
137
+ // get page
138
+ source .getPage (params .slug , params .lang );
125
139
126
- For the built-in Orama search, see [ Search I18n] ( /docs/headless/search/orama#internationalization ) .
140
+ // get pages
141
+ source .getPages (params .lang );
142
+ ```
127
143
128
- ### Adding Translations
144
+ like:
129
145
130
- We only provide English translation by default, you have to pass your translations to the provider.
146
+ ``` tsx title="app/[lang]/layout.tsx"
147
+ import { source } from ' @/lib/source' ;
148
+ import { DocsLayout } from ' fumadocs-ui/docs' ;
149
+ import type { ReactNode } from ' react' ;
131
150
132
- ``` tsx
133
- import { I18nProvider } from ' fumadocs-ui/i18n' ;
134
-
135
- <I18nProvider
136
- locales = { [
137
- {
138
- name: ' English' ,
139
- locale: ' en' ,
140
- },
141
- {
142
- name: ' Chinese' ,
143
- locale: ' cn' ,
144
- },
145
- ]}
146
- translations = {
147
- {
148
- cn: {
149
- search: ' Translated Content' ,
150
- },
151
- }[locale ]
152
- }
153
- // other props
154
- />;
151
+ export default async function Layout({
152
+ params ,
153
+ children ,
154
+ }: {
155
+ params: Promise <{ lang: string }>;
156
+ children: ReactNode ;
157
+ }) {
158
+ const pageTree = source .pageTree [(await params ).lang ];
159
+
160
+ return <DocsLayout pageTree = { pageTree } >{ children } </DocsLayout >;
161
+ }
155
162
```
156
163
164
+ Note that without providing a locale code, it uses your default locale instead.
165
+ You can see [ Source API] ( /docs/headless/source-api ) for other usages.
166
+
167
+ ### Writing Documents
168
+
169
+ see [ Page Conventions] ( /docs/ui/page-conventions#internationalization ) to learn how to organize your documents.
170
+
171
+ ### Search
172
+
173
+ Configure i18n on your search solution.
174
+
175
+ - Built-in Search (Orama):
176
+ - For ` createFromSource ` and most languages, no further changes are needed.
177
+ - For special languages like Chinese & Japanese, they require additional config.
178
+ See [ Orama I18n] ( /docs/headless/search/orama#internationalization ) guide.
179
+ - Cloud Solutions (e.g. Algolia):
180
+ - They usually have official support for multilingual.
181
+
157
182
### Add Language Switch
158
183
159
184
To allow users changing their language, enable ` i18n ` on your layouts.
0 commit comments