Skip to content

Commit 769c94b

Browse files
chore: clarifies i18n docs (#8619)
Fixes #8562 Removes `debug` option from i18n docs - never existed. Corrects hallucinations in the docs and lays out exactly how custom translations should be used when you want to use them in custom components.
1 parent f507530 commit 769c94b

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

docs/configuration/i18n.mdx

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ The following options are available:
5151
| Option | Description |
5252
| --------------------- | --------------------------------|
5353
| **`fallbackLanguage`** | The language to fall back to if the user's preferred language is not supported. Default is `'en'`. |
54-
| **`debug`** | Whether to log debug information to the console. Default is `false`. |
5554
| **`translations`** | An object containing the translations. The keys are the language codes and the values are the translations. |
5655
| **`supportedLanguages`** | An object containing the supported languages. The keys are the language codes and the values are the translations. |
5756

@@ -178,51 +177,78 @@ Anywhere in your Payload app that you have access to the `req` object, you can a
178177

179178
In order to use custom translations in your project, you need to provide the types for the translations.
180179

181-
Here is an example of how you can define the types for the custom translations in a [Custom Component](../admin/components):
180+
Here we create a shareable translations object. We will import this in both our custom components and in our Payload config.
182181

183182
```ts
184-
'use client'
185-
import type { NestedKeysStripped } from '@payloadcms/translations'
186-
import type React from 'react'
183+
// <rootDir>/custom-translations.ts
187184

188-
import { useTranslation } from '@payloadcms/ui/providers/Translation'
185+
import type { Config } from 'payload'
189186

190-
const customTranslations = {
187+
export customTranslations: Config['i18n']['translations'] = {
191188
en: {
192189
general: {
193-
test: 'Custom Translation',
190+
myCustomKey: 'My custom english translation',
194191
},
192+
fields: {
193+
addLabel: 'Add!',
194+
}
195195
},
196196
}
197+
```
198+
199+
We import the shared translations object into our Payload config so they are available for use:
200+
201+
```ts
202+
// <rootDir>/payload.config.ts
203+
204+
import { buildConfig } from 'payload'
205+
206+
import { customTranslations } from './custom-translations'
207+
208+
export default buildConfig({
209+
//...
210+
i18n: {
211+
translations: customTranslations,
212+
},
213+
//...
214+
})
215+
```
216+
217+
We import the shared translations and use it to create types to use in a [Custom Component](../admin/components):
218+
219+
```ts
220+
// <rootDir>/components/MyComponent.tsx
221+
222+
'use client'
223+
import type { NestedKeysStripped } from '@payloadcms/translations'
224+
import type React from 'react'
225+
import { useTranslation } from '@payloadcms/ui/providers/Translation'
226+
227+
import { customTranslations } from '../custom-translations'
197228

198229
type CustomTranslationObject = typeof customTranslations.en
199230
type CustomTranslationKeys = NestedKeysStripped<CustomTranslationObject>
200231

201232
export const MyComponent: React.FC = () => {
202233
const { i18n, t } = useTranslation<CustomTranslationObject, CustomTranslationKeys>() // These generics merge your custom translations with the default client translations
203234

204-
return t('general:test')
235+
return t('general:myCustomKey')
205236
}
206-
207237
```
208238

209239
Additionally, Payload exposes the `t` function in various places, for example in labels. Here is how you would type those:
210240

211241
```ts
242+
// <rootDir>/fields/myField.ts
243+
212244
import type {
213245
DefaultTranslationKeys,
214246
NestedKeysStripped,
215247
TFunction,
216248
} from '@payloadcms/translations'
217249
import type { Field } from 'payload'
218250

219-
const customTranslations = {
220-
en: {
221-
general: {
222-
test: 'Custom Translation',
223-
},
224-
},
225-
}
251+
import { customTranslations } from '../custom-translations'
226252

227253
type CustomTranslationObject = typeof customTranslations.en
228254
type CustomTranslationKeys = NestedKeysStripped<CustomTranslationObject>

0 commit comments

Comments
 (0)