From b48b7f4c164cea25391e4c5ae3cf15fd39797704 Mon Sep 17 00:00:00 2001 From: Niklas Wolf Date: Sun, 29 Jan 2023 13:18:49 +0100 Subject: [PATCH] feat: allow disabling translation for specific languages (#1371) --- components/settings/SettingsTranslations.vue | 63 ++++++++++++++++++++ components/status/StatusTranslation.vue | 2 +- composables/masto/translate.ts | 45 +++++++++++++- composables/settings/definition.ts | 2 + locales/de-DE.json | 11 +++- locales/en.json | 9 ++- pages/settings/language/index.vue | 4 ++ pages/settings/preferences/index.vue | 6 -- 8 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 components/settings/SettingsTranslations.vue diff --git a/components/settings/SettingsTranslations.vue b/components/settings/SettingsTranslations.vue new file mode 100644 index 0000000000..729a850bf2 --- /dev/null +++ b/components/settings/SettingsTranslations.vue @@ -0,0 +1,63 @@ + + + diff --git a/components/status/StatusTranslation.vue b/components/status/StatusTranslation.vue index daf00a7e53..67f2c84a71 100644 --- a/components/status/StatusTranslation.vue +++ b/components/status/StatusTranslation.vue @@ -12,7 +12,7 @@ const { } = useTranslation(status, getLanguageCode()) const preferenceHideTranslation = usePreferences('hideTranslation') -const showButton = computed(() => !preferenceHideTranslation.value && isTranslationEnabled && status.language !== getLanguageCode()) +const showButton = computed(() => !preferenceHideTranslation.value && isTranslationEnabled) let translating = $ref(false) const toggleTranslation = async () => { diff --git a/composables/masto/translate.ts b/composables/masto/translate.ts index f87ee31442..fc623df242 100644 --- a/composables/masto/translate.ts +++ b/composables/masto/translate.ts @@ -8,6 +8,40 @@ export interface TranslationResponse { } } +// @see https://github.com/LibreTranslate/LibreTranslate/tree/main/libretranslate/locales +export const supportedTranslationCodes = [ + 'ar', + 'az', + 'cs', + 'da', + 'de', + 'el', + 'en', + 'eo', + 'es', + 'fa', + 'fi', + 'fr', + 'ga', + 'he', + 'hi', + 'hu', + 'id', + 'it', + 'ja', + 'ko', + 'nl', + 'pl', + 'pt', + 'ru', + 'sk', + 'sv', + 'tr', + 'uk', + 'vi', + 'zh', +] as const + export const getLanguageCode = () => { let code = 'en' const getCode = (code: string) => code.replace(/-.*$/, '') @@ -63,9 +97,16 @@ export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEd translations.set(status, reactive({ visible: false, text: '', success: false, error: '' })) const translation = translations.get(status)! + const userSettings = useUserSettings() + + const shouldTranslate = 'language' in status && status.language && status.language !== to + && supportedTranslationCodes.includes(to as any) + && supportedTranslationCodes.includes(status.language as any) + && !userSettings.value.disabledTranslationLanguages.includes(status.language) + const enabled = /*! !useRuntimeConfig().public.translateApi && */ shouldTranslate async function toggle() { - if (!('language' in status)) + if (!shouldTranslate) return if (!translation.text) { @@ -79,7 +120,7 @@ export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEd } return { - enabled: !!useRuntimeConfig().public.translateApi, + enabled, toggle, translation, } diff --git a/composables/settings/definition.ts b/composables/settings/definition.ts index 36d0e20435..c3940ee143 100644 --- a/composables/settings/definition.ts +++ b/composables/settings/definition.ts @@ -26,6 +26,7 @@ export interface UserSettings { colorMode?: ColorMode fontSize: FontSize language: string + disabledTranslationLanguages: string[] zenMode: boolean themeColors?: ThemeColors } @@ -56,6 +57,7 @@ export function getDefaultUserSettings(locales: string[]): UserSettings { return { language: getDefaultLanguage(locales), fontSize: DEFAULT_FONT_SIZE, + disabledTranslationLanguages: [], zenMode: false, preferences: {}, } diff --git a/locales/de-DE.json b/locales/de-DE.json index 569522f5d2..5647453965 100644 --- a/locales/de-DE.json +++ b/locales/de-DE.json @@ -270,7 +270,14 @@ }, "language": { "display_language": "Anzeigesprache", - "label": "Sprache" + "label": "Sprache", + "translations": { + "add": "Hinzufügen", + "choose_language": "Sprache wählen", + "heading": "Übersetzungen", + "hide_specific": "Bestimmte Übersetzungen ausblenden", + "remove": "Entfernen" + } }, "notifications": { "label": "Benachrichtigungen", @@ -328,7 +335,7 @@ "hide_boost_count": "Boost-Zähler ausblenden", "hide_favorite_count": "Favoritenzahl ausblenden", "hide_follower_count": "Anzahl der Follower ausblenden", - "hide_translation": "Übersetzungen ausblenden", + "hide_translation": "Übersetzungen komplett ausblenden", "label": "Einstellungen", "title": "Experimentelle Funktionen", "user_picker": "Benutzerauswahl", diff --git a/locales/en.json b/locales/en.json index 196da4b273..2072472b38 100644 --- a/locales/en.json +++ b/locales/en.json @@ -316,7 +316,14 @@ }, "language": { "display_language": "Display Language", - "label": "Language" + "label": "Language", + "translations": { + "add": "Add", + "choose_language": "Choose language", + "heading": "Translations", + "hide_specific": "Hide specific translations", + "remove": "Remove" + } }, "notifications": { "label": "Notifications", diff --git a/pages/settings/language/index.vue b/pages/settings/language/index.vue index 52112f3e5f..1047871199 100644 --- a/pages/settings/language/index.vue +++ b/pages/settings/language/index.vue @@ -18,6 +18,10 @@ useHeadFixed({

{{ $t('settings.language.display_language') }}

+

+ {{ $t('settings.language.translations.heading') }} +

+ diff --git a/pages/settings/preferences/index.vue b/pages/settings/preferences/index.vue index 373cc26b8b..dde8f45299 100644 --- a/pages/settings/preferences/index.vue +++ b/pages/settings/preferences/index.vue @@ -39,12 +39,6 @@ const userSettings = useUserSettings() > {{ $t('settings.preferences.hide_follower_count') }} - - {{ $t('settings.preferences.hide_translation') }} -