From d5d8affc339158f9ada976bef5f6d3be6b2785e9 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Sat, 17 Sep 2022 15:01:31 -0400 Subject: [PATCH 1/2] backend: allow for source lang to be overridden in note/translate This adds a new optional `sourceLang` parameter to the `notes/translate` endpoint. If not set, the old behaviour is used, else this sets the `source_lang` parameter to the DeepL API call which makes it use the source language specified instead of using autodetection. Changelog: Changed Ref: https://akkoma.dev/FoundKeyGang/FoundKey/issues/33 --- .../server/api/endpoints/notes/translate.ts | 75 ++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/notes/translate.ts b/packages/backend/src/server/api/endpoints/notes/translate.ts index c42bf8c65..a5eca5e99 100644 --- a/packages/backend/src/server/api/endpoints/notes/translate.ts +++ b/packages/backend/src/server/api/endpoints/notes/translate.ts @@ -27,11 +27,79 @@ export const meta = { }, } as const; +// List of permitted languages from https://www.deepl.com/docs-api/translate-text/translate-text/ export const paramDef = { type: 'object', properties: { noteId: { type: 'string', format: 'misskey:id' }, - targetLang: { type: 'string' }, + sourceLang: { + type: 'string', + enum: [ + 'BG', + 'CS', + 'DA', + 'DE', + 'EL', + 'EN', + 'ES', + 'ET', + 'FI', + 'FR', + 'HU', + 'ID', + 'IT', + 'JA', + 'LT', + 'LV', + 'NL', + 'PL', + 'PT', + 'RO', + 'RU', + 'SK', + 'SL', + 'SV', + 'TR', + 'UK', + 'ZH', + ], + }, + targetLang: { + type: 'string', + enum: [ + 'BG', + 'CS', + 'DA', + 'DE', + 'EL', + 'EN', + 'EN-GB', + 'EN-US', + 'ES', + 'ET', + 'FI', + 'FR', + 'HU', + 'ID', + 'IT', + 'JA', + 'LT', + 'LV', + 'NL', + 'PL', + 'PT', + 'PT-BR', + 'PT-PT', + 'RO', + 'RU', + 'SK', + 'SL', + 'SV', + 'TR', + 'UK', + 'ZH', + ], + }, }, required: ['noteId', 'targetLang'], } as const; @@ -53,13 +121,14 @@ export default define(meta, paramDef, async (ps, user) => { return 204; // TODO: 良い感じのエラー返す } - let targetLang = ps.targetLang; - if (targetLang.includes('-')) targetLang = targetLang.split('-')[0]; + const sourceLang = ps.sourceLang; + const targetLang = ps.targetLang; const params = new URLSearchParams(); params.append('auth_key', instance.deeplAuthKey); params.append('text', note.text); params.append('target_lang', targetLang); + if (sourceLang) params.append('source_lang', sourceLang); const endpoint = instance.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate'; From 17f3dafd6be91b0d4b3c1b95fbf2dc7e3d9c8cf8 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Mon, 19 Sep 2022 22:20:50 +0200 Subject: [PATCH 2/2] client: bring targetLang into correct format Now that stricter API validation has been added, it will be necessary to modify the target language in the client so the API will not fail with a validation error. --- packages/client/src/scripts/get-note-menu.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/client/src/scripts/get-note-menu.ts b/packages/client/src/scripts/get-note-menu.ts index e68b6cb7f..a46cdb038 100644 --- a/packages/client/src/scripts/get-note-menu.ts +++ b/packages/client/src/scripts/get-note-menu.ts @@ -168,9 +168,17 @@ export function getNoteMenu(props: { async function translate(): Promise { if (props.translation.value != null) return; props.translating.value = true; + + let targetLang = localStorage.getItem('lang') || navigator.language; + targetLang = targetLang.toUpperCase(); + if (!['EN-GB', 'EN-US', 'PT-BR', 'PT-PT'].íncludes(targetLang)) { + // only the language code without country code is allowed + targetLang = targetLang.split('-', 1)[0]; + } + const res = await os.api('notes/translate', { noteId: appearNote.id, - targetLang: localStorage.getItem('lang') || navigator.language, + targetLang, }); props.translating.value = false; props.translation.value = res;