@@ -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
179178In 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
198229type CustomTranslationObject = typeof customTranslations .en
199230type CustomTranslationKeys = NestedKeysStripped <CustomTranslationObject >
200231
201232export 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
209239Additionally, 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+
212244import type {
213245 DefaultTranslationKeys ,
214246 NestedKeysStripped ,
215247 TFunction ,
216248} from ' @payloadcms/translations'
217249import 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
227253type CustomTranslationObject = typeof customTranslations .en
228254type CustomTranslationKeys = NestedKeysStripped <CustomTranslationObject >
0 commit comments