add: libretranslate

This commit is contained in:
Marie 2025-03-06 01:03:02 +01:00
parent c5440c20c6
commit 40599190f7
No known key found for this signature in database
GPG key ID: 7ADF6C9CD9A28555
8 changed files with 190 additions and 59 deletions

View file

@ -459,6 +459,14 @@ export const meta = {
type: 'string',
optional: false, nullable: true,
},
libreTranslateURL: {
type: 'string',
optional: false, nullable: true,
},
libreTranslateKey: {
type: 'string',
optional: false, nullable: true,
},
defaultDarkTheme: {
type: 'string',
optional: false, nullable: true,
@ -652,7 +660,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
defaultLike: instance.defaultLike,
enableEmail: instance.enableEmail,
enableServiceWorker: instance.enableServiceWorker,
translatorAvailable: instance.deeplAuthKey != null,
translatorAvailable: instance.deeplAuthKey != null || instance.libreTranslateURL != null || instance.deeplFreeMode && instance.deeplFreeInstance != null,
cacheRemoteFiles: instance.cacheRemoteFiles,
cacheRemoteSensitiveFiles: instance.cacheRemoteSensitiveFiles,
pinnedUsers: instance.pinnedUsers,
@ -700,6 +708,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
deeplIsPro: instance.deeplIsPro,
deeplFreeMode: instance.deeplFreeMode,
deeplFreeInstance: instance.deeplFreeInstance,
libreTranslateURL: instance.libreTranslateURL,
libreTranslateKey: instance.libreTranslateKey,
enableIpLogging: instance.enableIpLogging,
enableActiveEmailValidation: instance.enableActiveEmailValidation,
enableVerifymailApi: instance.enableVerifymailApi,

View file

@ -107,6 +107,8 @@ export const paramDef = {
deeplIsPro: { type: 'boolean' },
deeplFreeMode: { type: 'boolean' },
deeplFreeInstance: { type: 'string', nullable: true },
libreTranslateURL: { type: 'string', nullable: true },
libreTranslateKey: { type: 'string', nullable: true },
enableEmail: { type: 'boolean' },
email: { type: 'string', nullable: true },
smtpSecure: { type: 'boolean' },
@ -577,6 +579,22 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
}
if (ps.libreTranslateURL !== undefined) {
if (ps.libreTranslateURL === '') {
set.libreTranslateURL = null;
} else {
set.libreTranslateURL = ps.libreTranslateURL;
}
}
if (ps.libreTranslateKey !== undefined) {
if (ps.libreTranslateKey === '') {
set.libreTranslateKey = null;
} else {
set.libreTranslateKey = ps.libreTranslateKey;
}
}
if (ps.enableIpLogging !== undefined) {
set.enableIpLogging = ps.enableIpLogging;
}

View file

@ -93,11 +93,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
return;
}
if (this.serverSettings.deeplAuthKey == null && !this.serverSettings.deeplFreeMode) {
if (this.serverSettings.deeplAuthKey == null && !this.serverSettings.deeplFreeMode && !this.serverSettings.libreTranslateURL) {
throw new ApiError(meta.errors.unavailable);
}
if (this.serverSettings.deeplFreeMode && !this.serverSettings.deeplFreeInstance) {
if (this.serverSettings.deeplFreeMode && !this.serverSettings.deeplFreeInstance && !this.serverSettings.libreTranslateURL) {
throw new ApiError(meta.errors.unavailable);
}
@ -105,40 +105,76 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
const params = new URLSearchParams();
if (this.serverSettings.deeplAuthKey) params.append('auth_key', this.serverSettings.deeplAuthKey);
params.append('text', note.text);
params.append('target_lang', targetLang);
const endpoint = this.serverSettings.deeplFreeMode && this.serverSettings.deeplFreeInstance ? this.serverSettings.deeplFreeInstance : this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
// DeepL/DeepLX handling
if (this.serverSettings.deeplAuthKey != null || this.serverSettings.deeplFreeMode) {
if (this.serverSettings.deeplAuthKey) params.append('auth_key', this.serverSettings.deeplAuthKey);
params.append('text', note.text);
params.append('target_lang', targetLang);
const endpoint = this.serverSettings.deeplFreeMode && this.serverSettings.deeplFreeInstance ? this.serverSettings.deeplFreeInstance : this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
const res = await this.httpRequestService.send(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json, */*',
},
body: params.toString(),
});
if (this.serverSettings.deeplAuthKey) {
const json = (await res.json()) as {
translations: {
detected_source_language: string;
text: string;
}[];
};
return {
sourceLang: json.translations[0].detected_source_language,
text: json.translations[0].text,
};
} else {
const json = (await res.json()) as {
code: number,
message: string,
data: string,
source_lang: string,
target_lang: string,
alternatives: string[],
};
const languageNames = new Intl.DisplayNames(['en'], {
type: 'language',
});
return {
sourceLang: languageNames.of(json.source_lang),
text: json.data,
};
}
}
// LibreTranslate handling
if (this.serverSettings.libreTranslateURL) {
const res = await this.httpRequestService.send(this.serverSettings.libreTranslateURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json, */*',
},
body: JSON.stringify({
q: note.text,
source: 'auto',
target: targetLang,
format: 'text',
api_key: this.serverSettings.libreTranslateKey ?? '',
}),
});
const res = await this.httpRequestService.send(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json, */*',
},
body: params.toString(),
});
if (this.serverSettings.deeplAuthKey) {
const json = (await res.json()) as {
translations: {
detected_source_language: string;
text: string;
}[];
};
return {
sourceLang: json.translations[0].detected_source_language,
text: json.translations[0].text,
};
} else {
const json = (await res.json()) as {
code: number,
message: string,
data: string,
source_lang: string,
target_lang: string,
alternatives: string[],
detectedLanguage: { [key: string]: string | number },
translatedText: string,
};
const languageNames = new Intl.DisplayNames(['en'], {
@ -146,8 +182,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
});
return {
sourceLang: languageNames.of(json.source_lang),
text: json.data,
sourceLang: languageNames.of(json.detectedLanguage.language as string),
text: json.translatedText,
};
}
});