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.
This commit is contained in:
Johann150 2022-09-19 22:20:50 +02:00
parent d5d8affc33
commit 17f3dafd6b
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -168,9 +168,17 @@ export function getNoteMenu(props: {
async function translate(): Promise<void> {
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;