From 63dbc38806a0aaa3c0f2bec336f8b73c5beeac04 Mon Sep 17 00:00:00 2001 From: 2m411H <540351143@qq.com> Date: Thu, 7 Sep 2023 16:02:08 +0800 Subject: [PATCH 1/3] feat: not translate tags or mention --- composables/masto/translate.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/composables/masto/translate.ts b/composables/masto/translate.ts index af788b4c97..53f4f2e7bd 100644 --- a/composables/masto/translate.ts +++ b/composables/masto/translate.ts @@ -65,11 +65,13 @@ export async function translateText(text: string, from: string | null | undefine error: '', text: '', }) + const regex = /(]*>)(.*?)(<\/a>)/g + const filteredText = text.replace(regex, '$1$3') try { const response = await ($fetch as any)(config.public.translateApi, { method: 'POST', body: { - q: text, + q: filteredText, source: from ?? 'auto', target: to, format: 'html', @@ -77,7 +79,10 @@ export async function translateText(text: string, from: string | null | undefine }, }) as TranslationResponse status.success = true - status.text = response.translatedText + status.text = response.translatedText.replace(regex, (match) => { + const res = regex.exec(text) + return res ? res[0] : match + }) } catch (err) { // TODO: improve type From 81eefb6538a17d317fc943de476e41982aeefb0b Mon Sep 17 00:00:00 2001 From: 2m411H <540351143@qq.com> Date: Fri, 8 Sep 2023 15:55:23 +0800 Subject: [PATCH 2/3] replace all links --- composables/masto/translate.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composables/masto/translate.ts b/composables/masto/translate.ts index 53f4f2e7bd..de8a9730c7 100644 --- a/composables/masto/translate.ts +++ b/composables/masto/translate.ts @@ -65,13 +65,12 @@ export async function translateText(text: string, from: string | null | undefine error: '', text: '', }) - const regex = /(]*>)(.*?)(<\/a>)/g - const filteredText = text.replace(regex, '$1$3') + const regex = /]*>.*?<\/a>/g try { const response = await ($fetch as any)(config.public.translateApi, { method: 'POST', body: { - q: filteredText, + q: text, source: from ?? 'auto', target: to, format: 'html', @@ -79,9 +78,10 @@ export async function translateText(text: string, from: string | null | undefine }, }) as TranslationResponse status.success = true + // reaplace the translated links with the original status.text = response.translatedText.replace(regex, (match) => { - const res = regex.exec(text) - return res ? res[0] : match + const tagLink = regex.exec(text) + return tagLink ? tagLink[0] : match }) } catch (err) { From 3f4549ad4aac69593fc990b833433539262f1ce2 Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 13 Oct 2023 09:10:56 +0200 Subject: [PATCH 3/3] chore: typo --- composables/masto/translate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composables/masto/translate.ts b/composables/masto/translate.ts index de8a9730c7..969388aa53 100644 --- a/composables/masto/translate.ts +++ b/composables/masto/translate.ts @@ -78,7 +78,7 @@ export async function translateText(text: string, from: string | null | undefine }, }) as TranslationResponse status.success = true - // reaplace the translated links with the original + // replace the translated links with the original status.text = response.translatedText.replace(regex, (match) => { const tagLink = regex.exec(text) return tagLink ? tagLink[0] : match