diff --git a/dev/src/tests/translations.test.ts b/dev/src/tests/translations.test.ts index e5349d2..9f59602 100644 --- a/dev/src/tests/translations.test.ts +++ b/dev/src/tests/translations.test.ts @@ -61,7 +61,7 @@ describe('Translations', () => { }) describe('fn: updateTranslation', () => { - it('updates a Payload article with a translation retrieved from CrowdIn', async () => { + it('updates a Payload article with a `text` field translation retrieved from CrowdIn', async () => { const post = await payload.create({ collection: collections.localized, data: { title: 'Test post' }, @@ -90,4 +90,41 @@ describe('Translations', () => { expect(result.title).toEqual("Testbeitrag") }); }) + + it('updates a Payload article with a `richText` field translation retrieved from CrowdIn', async () => { + const post = await payload.create({ + collection: collections.localized, + data: { + content: { + children: [ + { + text: "Test content" + } + ] + } + }, + }); + const translationsApi = new payloadCrowdInSyncTranslationsApi( + pluginOptions, + payload, + ) + const scope = nock('https://api.crowdin.com') + .persist() + .get('/api/v2/projects/1/translations/builds/1/download') + .reply(200, + "

Testbeitrag

" + ) + const translation = await translationsApi.updateTranslation({ + documentId: post.id, + collection: collections.localized, + dryRun: false, + }) + // retrieve translated post from Payload + const result = await payload.findByID({ + collection: collections.localized, + id: post.id, + locale: 'de_DE', + }); + expect(result.content).toEqual([{"children": [{"text": "{\"title\":\"Testbeitrag\"}"}]}]) + }); }); diff --git a/src/api/payload-crowdin-sync/translations.ts b/src/api/payload-crowdin-sync/translations.ts index 6f914f1..4afed08 100644 --- a/src/api/payload-crowdin-sync/translations.ts +++ b/src/api/payload-crowdin-sync/translations.ts @@ -207,12 +207,13 @@ export class payloadCrowdInSyncTranslationsApi { localizedJsonFields.push('meta.description') } + let docTranslations: { [key: string]: any } = {} // add json fields - const docTranslations = await this.getTranslation({ + docTranslations = await this.getTranslation({ documentId: global ? collectionConfig.slug : doc.id, fieldName: 'fields', locale: locale, - }) + }) || {} // add html fields for (const field of localizedHtmlFields) { docTranslations[field] = await this.getTranslation({