diff --git a/README.md b/README.md index 8922cda..71c5a02 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/src/entrypoints/ConfigScreen/ConfigScreen.tsx b/src/entrypoints/ConfigScreen/ConfigScreen.tsx index e6f9467..f8fb47f 100644 --- a/src/entrypoints/ConfigScreen/ConfigScreen.tsx +++ b/src/entrypoints/ConfigScreen/ConfigScreen.tsx @@ -9,6 +9,7 @@ import { import { deeplFormalityLevelOptions, + defaultShowTranslate, defaultDeeplPreserveFormatting, fieldsOptions, translationServiceOptions, @@ -85,6 +86,21 @@ export default function ConfigScreen({ ctx }: Props) { }} /> + { + ctx.updatePluginParameters({ + ...pluginParameters, + showTranslateAll: newValue, + }) + ctx.notice('Settings updated successfully!') + }} + /> + - Copy and translate from {locales[0]} + Copy and translate from {getFullLocaleText(locales[0])} ) } + 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 ( -
- translateField(locales.filter((locale) => locale !== currentLocale)) - } - > + translateField(otherLocales)}>
diff --git a/src/entrypoints/FieldAddonConfigScreen/FieldAddonConfigScreen.tsx b/src/entrypoints/FieldAddonConfigScreen/FieldAddonConfigScreen.tsx index 3bc7986..132ca10 100644 --- a/src/entrypoints/FieldAddonConfigScreen/FieldAddonConfigScreen.tsx +++ b/src/entrypoints/FieldAddonConfigScreen/FieldAddonConfigScreen.tsx @@ -9,6 +9,7 @@ import { import { deeplFormalityLevelOptions, + defaultShowTranslate, defaultDeeplPreserveFormatting, translationServiceOptions, } from '../../lib/constants' @@ -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 || @@ -61,6 +67,21 @@ export default function ConfigScreen({ ctx }: Props) {
+ { + ctx.setParameters({ + ...pluginParameters, + showTranslateAll: newValue, + }) + ctx.notice('Settings updated successfully!') + }} + /> + diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 1cae9ab..abc2dd1 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -7,6 +7,7 @@ import { SettingOption, } from './types' +export const defaultShowTranslate = true export const defaultDeeplPreserveFormatting = false export const fieldsOptions: SettingOption[] = [ diff --git a/src/lib/helpers.test.ts b/src/lib/helpers.test.ts index 3e90fb9..56868ad 100644 --- a/src/lib/helpers.test.ts +++ b/src/lib/helpers.test.ts @@ -4,6 +4,7 @@ import { isJsonString, structuredTextValueToDast, fieldHasFieldValue, + getFullLocaleText, } from './helpers' import { structuredTextDast, @@ -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) + }) +}) diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 2c4abba..1fadb8c 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -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 +} diff --git a/src/lib/types.ts b/src/lib/types.ts index 3b7a05d..852371c 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -65,6 +65,7 @@ export enum DeeplFormalityLevel { } export type Parameters = { + showTranslateAll?: boolean translationService?: SettingOption model?: SettingOption temperature?: number