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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ For this plugin you can configure global settings and configure the plugin per f
- **Auto apply to fields (switch)**: When enabled this will automatically apply the plugin to all `structured-text`, `rich-text`, `string`, `text` and `seo` fields.
By changing the following setting you can choose on which fields this plugin will be applied.

- **Show "Translate to all locales" button (switch)**: When enabled this will show a `Translate to all locales` button on for all English text to translate the field into all available languages.

- **Field where this plugin is enabled (multi select)**: You can choose to which fields the plugin will be applied.

> Options of `Field where this plugin is enabled`:
Expand Down
16 changes: 16 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,
defaultShowTranslate,
defaultDeeplPreserveFormatting,
fieldsOptions,
translationServiceOptions,
Expand Down Expand Up @@ -85,6 +86,21 @@ export default function ConfigScreen({ ctx }: Props) {
}}
/>

<SwitchField
name="showTranslateAll"
id="showTranslateAll"
label='Show "translate to all locales" button'
hint='If disabled it will not show the "Translate to all locales" button.'
value={pluginParameters?.showTranslateAll ?? defaultShowTranslate}
onChange={(newValue) => {
ctx.updatePluginParameters({
...pluginParameters,
showTranslateAll: newValue,
})
ctx.notice('Settings updated successfully!')
}}
/>

<SelectField
name="translationService"
id="translationService"
Expand Down
38 changes: 24 additions & 14 deletions src/entrypoints/FieldAddon/FieldAddon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import {
} from '../../lib/types'
import {
deeplFormalityLevelOptions,
defaultShowTranslate,
defaultDeeplPreserveFormatting,
translationFormats,
translationServiceOptions,
} from '../../lib/constants'
import { fieldHasFieldValue } from '../../lib/helpers'
import { fieldHasFieldValue, getFullLocaleText } from '../../lib/helpers'

type Props = {
ctx: RenderFieldExtensionCtx
Expand All @@ -47,6 +48,11 @@ export default function FieldAddon({ ctx }: Props) {
ctx.plugin.attributes.parameters
const pluginParameters: Parameters = ctx.parameters

const showTranslateAll =
pluginParameters?.showTranslateAll ??
pluginGlobalParameters?.showTranslateAll ??
defaultShowTranslate

const translationService =
pluginParameters?.translationService ||
pluginGlobalParameters?.translationService ||
Expand Down Expand Up @@ -227,7 +233,9 @@ export default function FieldAddon({ ctx }: Props) {
}
} else {
setHasError(
`Please add content to the default field (${fromLocale || locales[0]})`,
`Please add content to the ${
fromLocale ? getFullLocaleText(fromLocale) : ''
} field`,
)
}
}
Expand Down Expand Up @@ -276,32 +284,34 @@ export default function FieldAddon({ ctx }: Props) {
}
disabled={isTranslating}
>
Copy and translate from {locales[0]}
Copy and translate from {getFullLocaleText(locales[0])}
</Button>
</Form>
</Canvas>
)
}

if (!showTranslateAll) {
ctx.setHeight(0)
return null
}

const otherLocales = locales.filter((locale) => locale !== currentLocale)
const buttonText =
locales.length > 2
? 'Translate to all locales'
: `Translate to ${getFullLocaleText(otherLocales[0])}`

return (
<Canvas ctx={ctx}>
<Form
onSubmit={() =>
translateField(locales.filter((locale) => locale !== currentLocale))
}
>
<Form onSubmit={() => translateField(otherLocales)}>
<Button
buttonSize="xxs"
type="submit"
rightIcon={isTranslating ? <Spinner size={24} /> : null}
disabled={isTranslating}
>
{locales.length > 2
? `Translate to all locales (
${locales.filter((locale) => locale !== currentLocale).join(', ')})`
: `Translate to ${locales.filter(
(locale) => locale !== currentLocale,
)}`}
{buttonText}
</Button>
</Form>
</Canvas>
Expand Down
23 changes: 23 additions & 0 deletions src/entrypoints/FieldAddonConfigScreen/FieldAddonConfigScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

import {
deeplFormalityLevelOptions,
defaultShowTranslate,
defaultDeeplPreserveFormatting,
translationServiceOptions,
} from '../../lib/constants'
Expand All @@ -33,6 +34,11 @@ export default function ConfigScreen({ ctx }: Props) {
const pluginGlobalParameters: GlobalParameters =
ctx.plugin.attributes.parameters

const selectedShowTranslateAll =
pluginParameters?.showTranslateAll ??
pluginGlobalParameters?.showTranslateAll ??
defaultShowTranslate

const selectedTranslationService =
pluginParameters?.translationService ||
pluginGlobalParameters?.translationService ||
Expand Down Expand Up @@ -61,6 +67,21 @@ export default function ConfigScreen({ ctx }: Props) {
<Canvas ctx={ctx}>
<Form>
<FieldGroup>
<SwitchField
name="showTranslateAll"
id="showTranslateAll"
label='Show "translate to all locales" button'
hint='If disabled it will not show the "Translate to all locales" button.'
value={selectedShowTranslateAll}
onChange={(newValue) => {
ctx.setParameters({
...pluginParameters,
showTranslateAll: newValue,
})
ctx.notice('Settings updated successfully!')
}}
/>

<SelectField
name="translationService"
id="translationService"
Expand All @@ -75,6 +96,8 @@ export default function ConfigScreen({ ctx }: Props) {
...pluginParameters,
translationService: newValue,
})

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

Expand Down
1 change: 1 addition & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SettingOption,
} from './types'

export const defaultShowTranslate = true
export const defaultDeeplPreserveFormatting = false

export const fieldsOptions: SettingOption<DatoFieldType>[] = [
Expand Down
21 changes: 21 additions & 0 deletions src/lib/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isJsonString,
structuredTextValueToDast,
fieldHasFieldValue,
getFullLocaleText,
} from './helpers'
import {
structuredTextDast,
Expand Down Expand Up @@ -152,3 +153,23 @@ describe('fieldHasFieldValue', () => {
expect(fieldHasFieldValue([], extendedDatoCmsCtx)).toBeFalsy()
})
})

describe('getFullLocaleText', () => {
it('should return full locale text for a given locale', () => {
const locale = 'en'
const result = 'English'
expect(getFullLocaleText(locale)).toBe(result)
})

it('should return full locale text for a different locale', () => {
const locale = 'fr'
const result = 'French'
expect(getFullLocaleText(locale)).toBe(result)
})

it('should return locale if locale is not supported', () => {
const locale = 'abc'
const result = 'abc'
expect(getFullLocaleText(locale)).toBe(result)
})
})
7 changes: 7 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,10 @@ export function fieldHasFieldValue(
}
}
}

export function getFullLocaleText(locale: string): string {
const fullLocaleText = new Intl.DisplayNames(['en'], { type: 'language' }).of(
locale,
)
return fullLocaleText || locale
}
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export enum DeeplFormalityLevel {
}

export type Parameters = {
showTranslateAll?: boolean
translationService?: SettingOption<TranslationService>
model?: SettingOption<string>
temperature?: number
Expand Down