Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/entrypoints/ConfigScreen/ConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

import {
deeplFormalityLevelOptions,
defaultDeeplPreserveFormatting,
fieldsOptions,
translationServiceOptions,
} from '../../lib/constants'
Expand Down Expand Up @@ -145,6 +146,27 @@ export default function ConfigScreen({ ctx }: Props) {
/>
)}

{isDeepl && (
<SwitchField
name="deeplPreserveFormatting"
id="deeplPreserveFormatting"
label="Preserve formatting"
hint="Sets whether the translation engine should respect the original formatting, even if it would usually correct some aspects."
value={
pluginParameters?.deeplPreserveFormatting ??
defaultDeeplPreserveFormatting
}
onChange={(newValue) => {
ctx.updatePluginParameters({
...pluginParameters,
deeplPreserveFormatting: newValue,
})

ctx.notice('Settings updated successfully!')
}}
/>
)}

{isDeepl && (
<GlossaryIdField
value={pluginParameters?.deeplGlossaryId || ''}
Expand Down
7 changes: 7 additions & 0 deletions src/entrypoints/FieldAddon/FieldAddon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../../lib/types'
import {
deeplFormalityLevelOptions,
defaultDeeplPreserveFormatting,
translationFormats,
translationServiceOptions,
} from '../../lib/constants'
Expand Down Expand Up @@ -88,6 +89,11 @@ export default function FieldAddon({ ctx }: Props) {
const deeplGlossaryId =
pluginParameters.deeplGlossaryId || pluginGlobalParameters.deeplGlossaryId

const deeplPreserveFormatting =
pluginParameters.deeplPreserveFormatting ??
pluginGlobalParameters.deeplPreserveFormatting ??
defaultDeeplPreserveFormatting

const deeplFormalityLevelValue =
pluginParameters.deeplFormalityLevel?.value ||
pluginGlobalParameters.deeplFormalityLevel?.value ||
Expand Down Expand Up @@ -141,6 +147,7 @@ export default function FieldAddon({ ctx }: Props) {
deeplOptions: {
glossaryId: deeplGlossaryId,
formality: deeplFormalityLevelValue,
preserveFormatting: deeplPreserveFormatting,
},
openAIOptions: {
model: modelValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { RenderManualFieldExtensionConfigScreenCtx } from 'datocms-plugin-sdk'
import { Canvas, Form, SelectField, FieldGroup } from 'datocms-react-ui'
import {
Canvas,
Form,
SelectField,
FieldGroup,
SwitchField,
} from 'datocms-react-ui'

import {
deeplFormalityLevelOptions,
defaultDeeplPreserveFormatting,
translationServiceOptions,
} from '../../lib/constants'
import {
Expand Down Expand Up @@ -36,6 +43,11 @@ export default function ConfigScreen({ ctx }: Props) {
pluginGlobalParameters?.deeplFormalityLevel ||
deeplFormalityLevelOptions[0]

const selectedPreserveFormatting =
pluginParameters?.deeplPreserveFormatting ??
pluginGlobalParameters?.deeplPreserveFormatting ??
defaultDeeplPreserveFormatting

const excludedKeys =
pluginParameters?.excludedKeys || pluginGlobalParameters?.excludedKeys || ''

Expand Down Expand Up @@ -112,6 +124,24 @@ export default function ConfigScreen({ ctx }: Props) {
/>
)}

{isDeepl && (
<SwitchField
name="deeplPreserveFormatting"
id="deeplPreserveFormatting"
label="Preserve formatting"
hint="Sets whether the translation engine should respect the original formatting, even if it would usually correct some aspects."
onChange={(newValue) => {
ctx.setParameters({
...pluginParameters,
deeplPreserveFormatting: newValue,
})

ctx.notice('Settings updated successfully!')
}}
value={selectedPreserveFormatting}
/>
)}

{isDeepl && (
<GlossaryIdField
value={pluginParameters?.deeplGlossaryId || ''}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
SettingOption,
} from './types'

export const defaultDeeplPreserveFormatting = false

export const fieldsOptions: SettingOption<DatoFieldType>[] = [
{ label: 'String fields', value: DatoFieldType.stringField },
{ label: 'Text fields', value: DatoFieldType.textField },
Expand Down
12 changes: 11 additions & 1 deletion src/lib/translation-services/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ export default async function translate(
const params = new URLSearchParams()

params.set('target_lang', options.toLocale)
params.set('tag_handling', options.format === 'html' ? 'html' : 'xml')

if (options.format === 'html') {
params.set('tag_handling', 'html')
} else if (!options.deeplOptions?.preserveFormatting) {
params.set('tag_handling', 'xml')
}

params.set('text', string)

if (options.fromLocale) {
Expand All @@ -22,6 +28,10 @@ export default async function translate(
params.set('glossary_id', options.deeplOptions.glossaryId)
}

if (options.deeplOptions?.preserveFormatting) {
params.set('preserve_formatting', '1')
}

if (
options.deeplOptions?.formality &&
options.deeplOptions.formality !== DeeplFormalityLevel.default
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type Parameters = {
topP?: number
prompt?: string
deeplGlossaryId?: string
deeplPreserveFormatting?: boolean
deeplFormalityLevel?: SettingOption<DeeplFormalityLevel>
excludedKeys?: string
[TranslationServiceKey.yandexKey]?: string
Expand Down Expand Up @@ -100,6 +101,7 @@ export type TranslationOptions = {
deeplOptions?: {
glossaryId?: string
formality?: DeeplFormalityLevel
preserveFormatting?: boolean
}
openAIOptions: {
model: string
Expand Down