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: FoundKeyGang/FoundKey#33
This commit is contained in:
Norm 2022-09-17 15:01:31 -04:00 committed by Gitea
parent d8a8306603
commit d5d8affc33

View file

@ -27,11 +27,79 @@ export const meta = {
}, },
} as const; } as const;
// List of permitted languages from https://www.deepl.com/docs-api/translate-text/translate-text/
export const paramDef = { export const paramDef = {
type: 'object', type: 'object',
properties: { properties: {
noteId: { type: 'string', format: 'misskey:id' }, 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'], required: ['noteId', 'targetLang'],
} as const; } as const;
@ -53,13 +121,14 @@ export default define(meta, paramDef, async (ps, user) => {
return 204; // TODO: 良い感じのエラー返す return 204; // TODO: 良い感じのエラー返す
} }
let targetLang = ps.targetLang; const sourceLang = ps.sourceLang;
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0]; const targetLang = ps.targetLang;
const params = new URLSearchParams(); const params = new URLSearchParams();
params.append('auth_key', instance.deeplAuthKey); params.append('auth_key', instance.deeplAuthKey);
params.append('text', note.text); params.append('text', note.text);
params.append('target_lang', targetLang); 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'; const endpoint = instance.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';