From a88f76319612cdf8bc21d1027f60cf9ba29968b3 Mon Sep 17 00:00:00 2001 From: Carolina Gonzalez Date: Fri, 3 May 2024 13:30:57 -0400 Subject: [PATCH] feat: add same functionality for field level translation --- docs/01-advanced-configuration.md | 2 +- src/configuration/baseFieldLevelConfig.ts | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/01-advanced-configuration.md b/docs/01-advanced-configuration.md index 49a8e5d..1d3e279 100644 --- a/docs/01-advanced-configuration.md +++ b/docs/01-advanced-configuration.md @@ -63,7 +63,7 @@ const customConfig = { //sometimes editors will duplicate a field or document and begin changing assets //THEN import the translation. If this is a typical workflow, please toggle the following option //(it is defaulted to false) - mergeWithTargetLocale: true + mergeWithTargetLocale: true, //adapter, baseLanguage, secretsNamespace, importTranslation, exportForTranslation should likely not be touched unless you very much want to customize your plugin. } satisfies TranslationsTabConfigOptions ``` diff --git a/src/configuration/baseFieldLevelConfig.ts b/src/configuration/baseFieldLevelConfig.ts index 437d530..db5aa79 100644 --- a/src/configuration/baseFieldLevelConfig.ts +++ b/src/configuration/baseFieldLevelConfig.ts @@ -19,6 +19,8 @@ export const fieldLevelPatch = async ( localeId: string, client: SanityClient, baseLanguage: string = 'en', + mergeWithTargetLocale: boolean = false, + // eslint-disable-next-line max-params ): Promise => { let baseDoc: SanityDocument if (translatedFields._rev && translatedFields._id) { @@ -31,7 +33,7 @@ export const fieldLevelPatch = async ( translatedFields, baseDoc, localeId, - baseLanguage, + mergeWithTargetLocale ? baseLanguage : localeId, ) await client.patch(baseDoc._id).set(merged).commit() @@ -63,7 +65,16 @@ export const baseFieldLevelConfig = { return serialized }, importTranslation: (...params: Parameters): Promise => { - const [id, localeId, document, context, baseLanguage = 'en', serializationOptions = {}] = params + const [ + id, + localeId, + document, + context, + baseLanguage = 'en', + serializationOptions = {}, + , + mergeWithTargetLocale, + ] = params const {client} = context const deserializers = { types: { @@ -80,7 +91,7 @@ export const baseFieldLevelConfig = { deserializers, blockDeserializers, ) as SanityDocument - return fieldLevelPatch(id, deserialized, localeId, client, baseLanguage) + return fieldLevelPatch(id, deserialized, localeId, client, baseLanguage, mergeWithTargetLocale) }, adapter: DummyAdapter, secretsNamespace: 'translationService',