From 17f3dafd6be91b0d4b3c1b95fbf2dc7e3d9c8cf8 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Mon, 19 Sep 2022 22:20:50 +0200 Subject: [PATCH] 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;