Created
October 31, 2025 12:13
-
-
Save mmikhan/fd5b949b4bf8030cbe7ea9a7bdada857 to your computer and use it in GitHub Desktop.
Payload Seed multiple locales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { localization } from '@/i18n/localization' | |
| import type { Category } from '@/payload-types' | |
| import type { RequiredDataFromCollectionSlug, TypedLocale } from 'payload' | |
| type Data = { | |
| title?: string | |
| slug?: string | |
| icon?: string | |
| } | |
| type Props = { | |
| parent?: Category | |
| locales: TypedLocale[] | |
| data: Record<TypedLocale, Data> | |
| } | |
| type CategorySeed = RequiredDataFromCollectionSlug<'categories'> & { locale: TypedLocale } | |
| const seed = ({ parent, locales, data }: Props): CategorySeed[] => { | |
| return locales.map((locale) => ({ | |
| title: data[locale]?.title ?? '', | |
| icon: data[locale]?.icon ?? '', | |
| slug: data[locale]?.slug ?? '', | |
| parent, | |
| locale, | |
| })) | |
| } | |
| export const electronicsCategory = seed({ | |
| locales: localization.locales.map((l) => l.code) as TypedLocale[], | |
| data: { | |
| en: { title: 'Electronics', slug: 'electronics', icon: 'tv' }, | |
| nl: { title: 'Elektronica', slug: 'electronics', icon: 'tv' }, | |
| bn: { title: 'ইলেকট্রনিক্স', slug: 'electronics', icon: 'tv' }, | |
| }, | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const payloadReq = await createLocalReq({ user }, payload) | |
| await Promise.all( | |
| electronicsCategory.map((category) => { | |
| return payload.create({ | |
| collection: 'categories', | |
| data: category, | |
| req: payloadReq, | |
| depth: 0, | |
| locale: category.locale, | |
| }) | |
| }), | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment