Skip to content

Commit

Permalink
feat: add same functionality for field level translation
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed May 3, 2024
1 parent 5e47491 commit a88f763
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/01-advanced-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
17 changes: 14 additions & 3 deletions src/configuration/baseFieldLevelConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
let baseDoc: SanityDocument
if (translatedFields._rev && translatedFields._id) {
Expand All @@ -31,7 +33,7 @@ export const fieldLevelPatch = async (
translatedFields,
baseDoc,
localeId,
baseLanguage,
mergeWithTargetLocale ? baseLanguage : localeId,
)

await client.patch(baseDoc._id).set(merged).commit()
Expand Down Expand Up @@ -63,7 +65,16 @@ export const baseFieldLevelConfig = {
return serialized
},
importTranslation: (...params: Parameters<ImportTranslation>): Promise<void> => {
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: {
Expand All @@ -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',
Expand Down

0 comments on commit a88f763

Please sign in to comment.