From 1a40218b9b665bad81675145262c9286440a0c89 Mon Sep 17 00:00:00 2001 From: naskya Date: Tue, 1 Aug 2023 02:21:56 +0900 Subject: [PATCH] feat: ability to set translation target language --- README.md | 1 + locales/en-US.yml | 1 + locales/ja-JP.yml | 1 + packages/client/src/components/MkNote.vue | 9 +++++++-- packages/client/src/components/MkNoteSub.vue | 9 +++++++-- packages/client/src/pages/settings/general.vue | 16 ++++++++++++++++ packages/client/src/scripts/get-note-menu.ts | 5 ++++- 7 files changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index be54d22bd..c2afe25f0 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ ## 主要な変更点 +- UI 用の言語とは別に、投稿翻訳に使用する言語を設定可能に - 投稿言語を自動検出して外国語の投稿に翻訳ボタンを表示する設定を追加 - モバイル表示の下部のウィジェットボタンを再読み込みボタンに変更可能に - スマートフォンでウィジェットは使わないけど再読み込みはたくさんする人はいそう diff --git a/locales/en-US.yml b/locales/en-US.yml index f6d152a2b..5a09c7246 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -1143,6 +1143,7 @@ delete2faConfirm: "This will irreversibly delete 2FA on this account. Proceed?" deletePasskeysConfirm: "This will irreversibly delete all passkeys and security keys on this account. Proceed?" inputNotMatch: "Input does not match" detectPostLanguage: "Automatically detect the language and show a translate button for non-English posts" +languageForTranslation: "Language used for post translation" _sensitiveMediaDetection: description: "Reduces the effort of server moderation through automatically recognizing diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index e8cf2d1cf..fda02a912 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -996,6 +996,7 @@ replaceChatButtonWithAccountButton: "画面下部のチャットのボタンを replaceWidgetsButtonWithReloadButton: "画面下部のウィジェットのボタンを再読み込みボタンに変更する" addRe: "閲覧注意の投稿への返信で、注釈の先頭に\"re:\"を追加する" detectPostLanguage: "投稿の言語を自動検出し、外国語の投稿に翻訳ボタンを表示する" +languageForTranslation: "投稿翻訳に使用する言語" _sensitiveMediaDetection: description: "機械学習を使って自動でセンシティブなメディアを検出し、モデレーションに役立てられます。サーバーの負荷が少し増えます。" diff --git a/packages/client/src/components/MkNote.vue b/packages/client/src/components/MkNote.vue index f0a287c08..c7461ed75 100644 --- a/packages/client/src/components/MkNote.vue +++ b/packages/client/src/components/MkNote.vue @@ -374,7 +374,9 @@ const isForeignLanguage = (() => { const text = purifyMFM(appearNote.text).trim(); if (!text) return false; const uiLanguage = ( - localStorage.getItem("lang") || navigator.language + localStorage.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language ).slice(0, 2); return detectLanguage(text) !== uiLanguage; })(); @@ -383,7 +385,10 @@ const translate = async () => { translating.value = true; translation.value = await os.api("notes/translate", { noteId: appearNote.id, - targetLang: localStorage.getItem("lang") || navigator.language, + targetLang: + localStorage.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language, }); translating.value = false; }; diff --git a/packages/client/src/components/MkNoteSub.vue b/packages/client/src/components/MkNoteSub.vue index 1929a623f..502d23b25 100644 --- a/packages/client/src/components/MkNoteSub.vue +++ b/packages/client/src/components/MkNoteSub.vue @@ -290,7 +290,9 @@ const isForeignLanguage = (() => { const text = purifyMFM(appearNote.text).trim(); if (!text) return false; const uiLanguage = ( - localStorage.getItem("lang") || navigator.language + localStorage.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language ).slice(0, 2); return detectLanguage(text) !== uiLanguage; })(); @@ -299,7 +301,10 @@ const translate = async () => { translating.value = true; translation.value = await os.api("notes/translate", { noteId: appearNote.id, - targetLang: localStorage.getItem("lang") || navigator.language, + targetLang: + localStorage.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language, }); translating.value = false; }; diff --git a/packages/client/src/pages/settings/general.vue b/packages/client/src/pages/settings/general.vue index 34da3adf0..0bd2a046b 100644 --- a/packages/client/src/pages/settings/general.vue +++ b/packages/client/src/pages/settings/general.vue @@ -17,6 +17,16 @@ + + + + + @@ -314,6 +324,7 @@ import { definePageMetadata } from "@/scripts/page-metadata"; import { deviceKind } from "@/scripts/device-kind"; const lang = ref(localStorage.getItem("lang")); +const translateLang = ref(localStorage.getItem("translateLang")); const fontSize = ref(localStorage.getItem("fontSize")); const useSystemFont = ref(localStorage.getItem("useSystemFont") != null); @@ -431,6 +442,10 @@ watch(lang, () => { localStorage.removeItem("locale"); }); +watch(translateLang, () => { + localStorage.setItem("translateLang", translateLang.value as string); +}); + watch(fontSize, () => { if (fontSize.value == null) { localStorage.removeItem("fontSize"); @@ -450,6 +465,7 @@ watch(useSystemFont, () => { watch( [ lang, + translateLang, fontSize, useSystemFont, enableInfiniteScroll, diff --git a/packages/client/src/scripts/get-note-menu.ts b/packages/client/src/scripts/get-note-menu.ts index a5565a9ba..04ae9739b 100644 --- a/packages/client/src/scripts/get-note-menu.ts +++ b/packages/client/src/scripts/get-note-menu.ts @@ -241,7 +241,10 @@ export function getNoteMenu(props: { props.translating.value = true; const res = await os.api("notes/translate", { noteId: appearNote.id, - targetLang: localStorage.getItem("lang") || navigator.language, + targetLang: + localStorege.getItem("translateLang") || + localStorage.getItem("lang") || + navigator.language, }); props.translating.value = false; props.translation.value = res;