Files

93 lines
3.2 KiB
TypeScript

import { getPayload } from 'payload'
import configPromise from '../src/payload.config'
async function run() {
const payload = await getPayload({ config: configPromise })
const translations = {
pl: {
title: 'Formularz kontaktowy',
confirmationMessage: 'Dziękujemy za wiadomość!',
emailSubject: 'Nowe zapytanie ze strony',
emailMessage: 'Ktoś wysłał zapytanie ze strony.',
fields: [
{ name: 'name', label: 'Imię i nazwisko' },
{ name: 'email', label: 'Adres e-mail' },
{ name: 'service', label: 'Usługa', options: [{ value: 'konsultacje', label: 'Konsultacje' }] },
{ name: 'message', label: 'Wiadomość' },
{ name: 'consent', label: 'Wyrażam zgodę na przetwarzanie danych' }
]
},
de: {
title: 'Kontaktformular',
confirmationMessage: 'Vielen Dank für Ihre Nachricht!',
emailSubject: 'Neue Anfrage über die Website',
emailMessage: 'Jemand hat eine Anfrage über die Website gesendet.',
fields: [
{ name: 'name', label: 'Vor- und Nachname' },
{ name: 'email', label: 'E-Mail Adresse' },
{ name: 'service', label: 'Dienstleistung', options: [{ value: 'konsultacje', label: 'Beratung' }] },
{ name: 'message', label: 'Nachricht' },
{ name: 'consent', label: 'Ich stimme der Datenverarbeitung zu' }
]
}
}
for (const locale of ['pl', 'de'] as const) {
const t = translations[locale]
// First, fetch the form in this locale to get its fields layout
const forms = await payload.find({ collection: 'forms', locale, limit: 1 })
if (forms.docs.length > 0) {
const form = forms.docs[0]
const updatedFields = form.fields?.map((f:any) => {
const tf = t.fields.find(tf => tf.name === f.name)
if (tf) {
f.label = tf.label
if (f.blockType === 'select' && f.options) {
f.options = f.options.map((o:any) => {
const to = tf.options?.find(to => to.value === o.value)
if (to) o.label = to.label
return o
})
}
}
return f
})
const updatedEmails = form.emails?.map((e:any) => {
e.subject = t.emailSubject
e.message = {
root: {
type: 'root',
children: [{ type: 'paragraph', children: [{ type: 'text', text: t.emailMessage, version: 1 }], direction: 'ltr', format: '', indent: 0, version: 1 }],
direction: 'ltr', format: '', indent: 0, version: 1
}
}
return e
})
await payload.update({
collection: 'forms',
id: form.id,
locale,
data: {
title: t.title,
fields: updatedFields,
emails: updatedEmails,
confirmationMessage: {
root: {
type: 'root',
children: [{ type: 'paragraph', children: [{ type: 'text', text: t.confirmationMessage, version: 1 }], direction: 'ltr', format: '', indent: 0, version: 1 }],
direction: 'ltr', format: '', indent: 0, version: 1
}
}
}
})
console.log(`Updated form translations for locale: ${locale}`)
}
}
process.exit(0)
}
run()