From d745bd05837cd69fd96879c158ab76689084fcde Mon Sep 17 00:00:00 2001 From: Alex <49969959+alexzhang1030@users.noreply.github.com> Date: Mon, 16 Jan 2023 17:55:00 +0800 Subject: [PATCH] feat: improve posts translation logic (#1211) --- components/status/StatusActionsMore.vue | 8 ----- components/status/StatusBody.vue | 5 ++- components/status/StatusContent.vue | 1 + components/status/StatusTranslation.vue | 41 ++++++++++++++++++++++ composables/masto/translate.ts | 45 +++++++++++++++++-------- nuxt.config.ts | 1 + 6 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 components/status/StatusTranslation.vue diff --git a/components/status/StatusActionsMore.vue b/components/status/StatusActionsMore.vue index 69fd870c..5a95fdff 100644 --- a/components/status/StatusActionsMore.vue +++ b/components/status/StatusActionsMore.vue @@ -206,14 +206,6 @@ const showFavoritedAndBoostedBy = () => { /> - - + +import type { mastodon } from 'masto' + +const { status } = defineProps<{ + status: mastodon.v1.Status +}>() + +const { + toggle: _toggleTranslation, + translation, + enabled: isTranslationEnabled, +} = useTranslation(status) + +let translating = $ref(false) +const toggleTranslation = async () => { + translating = true + try { + await _toggleTranslation() + } + finally { + translating = false + } +} + + + + + diff --git a/composables/masto/translate.ts b/composables/masto/translate.ts index 0f88fc08..d3567570 100644 --- a/composables/masto/translate.ts +++ b/composables/masto/translate.ts @@ -11,24 +11,37 @@ export interface TranslationResponse { export const languageCode = process.server ? 'en' : navigator.language.replace(/-.*$/, '') export async function translateText(text: string, from?: string | null, to?: string) { const config = useRuntimeConfig() - const { translatedText } = await $fetch(config.public.translateApi, { - method: 'POST', - body: { - q: text, - source: from ?? 'auto', - target: to ?? languageCode, - format: 'html', - api_key: '', - }, + const status = $ref({ + success: false, + error: '', + text: '', }) - return translatedText + try { + const response = await $fetch(config.public.translateApi, { + method: 'POST', + body: { + q: text, + source: from ?? 'auto', + target: to ?? languageCode, + format: 'html', + api_key: '', + }, + }) + status.success = true + status.text = response.translatedText + } + catch (err) { + // TODO: improve type + status.error = (err as { data: { error: string } }).data.error + } + return status } -const translations = new WeakMap() +const translations = new WeakMap() export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEdit) { if (!translations.has(status)) - translations.set(status, reactive({ visible: false, text: '' })) + translations.set(status, reactive({ visible: false, text: '', success: false, error: '' })) const translation = translations.get(status)! @@ -36,8 +49,12 @@ export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEd if (!('language' in status)) return - if (!translation.text) - translation.text = await translateText(status.content, status.language) + if (!translation.text) { + const { success, text, error } = await translateText(status.content, status.language) + translation.error = error + translation.text = text + translation.success = success + } translation.visible = !translation.visible } diff --git a/nuxt.config.ts b/nuxt.config.ts index 55a09dfa..ccebc9e4 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -92,6 +92,7 @@ export default defineNuxtConfig({ env: '', // set in build-env module buildInfo: {} as BuildInfo, // set in build-env module pwaEnabled: !isDevelopment || process.env.VITE_DEV_PWA === 'true', + // We use LibreTranslate(https://github.com/LibreTranslate/LibreTranslate) as our default translation server #76 translateApi: '', defaultServer: 'mas.to', },