Files
payload/packages/plugin-import-export/src/index.ts
Paul 3127d6ad6d fix(plugin-import-export): add translations for all UI elements and fields (#12449)
Converts all text and field labels into variables that can be
translated. Also generated the translations for them

So now the UI here is internationalised


![image](https://github.com/user-attachments/assets/40d7c010-ac58-4cd7-8786-01b3de3cabb7)

I've also moved some of the generic labels into the core package since
those could be re-used elsewhere
2025-05-19 13:19:55 -07:00

94 lines
3.0 KiB
TypeScript

import type { Config, JobsConfig } from 'payload'
import { deepMergeSimple } from 'payload'
import type { PluginDefaultTranslationsObject } from './translations/types.js'
import type { ImportExportPluginConfig } from './types.js'
import { getCreateCollectionExportTask } from './export/getCreateExportCollectionTask.js'
import { getExportCollection } from './getExportCollection.js'
import { translations } from './translations/index.js'
export const importExportPlugin =
(pluginConfig: ImportExportPluginConfig) =>
(config: Config): Config => {
const exportCollection = getExportCollection({ config, pluginConfig })
if (config.collections) {
config.collections.push(exportCollection)
} else {
config.collections = [exportCollection]
}
// inject custom import export provider
config.admin = config.admin || {}
config.admin.components = config.admin.components || {}
config.admin.components.providers = config.admin.components.providers || []
config.admin.components.providers.push(
'@payloadcms/plugin-import-export/rsc#ImportExportProvider',
)
// inject the createExport job into the config
config.jobs =
config.jobs ||
({
tasks: [getCreateCollectionExportTask(config)],
} as unknown as JobsConfig) // cannot type jobs config inside of plugins
let collectionsToUpdate = config.collections
const usePluginCollections = pluginConfig.collections && pluginConfig.collections?.length > 0
if (usePluginCollections) {
collectionsToUpdate = config.collections?.filter((collection) => {
return pluginConfig.collections?.includes(collection.slug)
})
}
collectionsToUpdate.forEach((collection) => {
if (!collection.admin) {
collection.admin = { components: { listMenuItems: [] } }
}
const components = collection.admin.components || {}
if (!components.listMenuItems) {
components.listMenuItems = []
}
if (!components.edit) {
components.edit = {}
}
if (!components.edit.SaveButton) {
components.edit.SaveButton = '@payloadcms/plugin-import-export/rsc#ExportSaveButton'
}
components.listMenuItems.push({
clientProps: {
exportCollectionSlug: exportCollection.slug,
},
path: '@payloadcms/plugin-import-export/rsc#ExportListMenuItem',
})
collection.admin.components = components
})
if (!config.i18n) {
config.i18n = {}
}
// config.i18n.translations = deepMergeSimple(translations, config.i18n?.translations ?? {})
/**
* Merge plugin translations
*/
const simplifiedTranslations = Object.entries(translations).reduce(
(acc, [key, value]) => {
acc[key] = value.translations
return acc
},
{} as Record<string, PluginDefaultTranslationsObject>,
)
config.i18n = {
...config.i18n,
translations: deepMergeSimple(simplifiedTranslations, config.i18n?.translations ?? {}),
}
return config
}