LibreTranslate

This commit is contained in:
Bahamut 2023-08-26 19:53:57 +03:00 committed by Cohee
parent 7931cd9b17
commit de905875c4
2 changed files with 80 additions and 4 deletions

View File

@ -135,12 +135,15 @@ const languageCodes = {
'Zulu': 'zu',
};
const KEY_REQUIRED = ['deepl'];
const KEY_REQUIRED = ['deepl','libre'];
const LOCAL_URL = ['libre'];
function showKeyButton() {
function showKeysButton() {
const providerRequiresKey = KEY_REQUIRED.includes(extension_settings.translate.provider);
const providerOptionalUrl = LOCAL_URL.includes(extension_settings.translate.provider);
$("#translate_key_button").toggle(providerRequiresKey);
$("#translate_key_button").toggleClass('success', Boolean(secret_state[extension_settings.translate.provider]));
$("#translate_url_button").toggle(providerOptionalUrl);
}
function loadSettings() {
@ -153,7 +156,7 @@ function loadSettings() {
$(`#translation_provider option[value="${extension_settings.translate.provider}"]`).attr('selected', true);
$(`#translation_target_language option[value="${extension_settings.translate.target_language}"]`).attr('selected', true);
$(`#translation_auto_mode option[value="${extension_settings.translate.auto_mode}"]`).attr('selected', true);
showKeyButton();
showKeysButton();
}
async function translateImpersonate(text) {
@ -181,6 +184,22 @@ async function translateIncomingMessage(messageId) {
updateMessageBlock(messageId, message);
}
async function translateProviderLibre(text, lang) {
const response = await fetch('/libre_translate', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ text: text, lang: lang }),
});
if (response.ok) {
const result = await response.text();
return result;
}
throw new Error(response.statusText);
}
async function translateProviderGoogle(text, lang) {
const response = await fetch('/google_translate', {
method: 'POST',
@ -222,6 +241,8 @@ async function translate(text, lang) {
}
switch (extension_settings.translate.provider) {
case 'libre':
return await translateProviderLibre(text, lang);
case 'google':
return await translateProviderGoogle(text, lang);
case 'deepl':
@ -368,10 +389,12 @@ jQuery(() => {
<label for="translation_provider">Provider</label>
<div class="flex-container gap5px flexnowrap marginBot5">
<select id="translation_provider" name="provider" class="margin0">
<option value="libre">Libre</option>
<option value="google">Google</option>
<option value="deepl">DeepL</option>
<select>
<div id="translate_key_button" class="menu_button fa-solid fa-key margin0"></div>
<div id="translate_url_button" class="menu_button fa-solid fa-link margin0"></div>
</div>
<label for="translation_target_language">Target Language</label>
<select id="translation_target_language" name="target_language"></select>
@ -403,7 +426,7 @@ jQuery(() => {
});
$('#translation_provider').on('change', (event) => {
extension_settings.translate.provider = event.target.value;
showKeyButton();
showKeysButton();
saveSettingsDebounced();
});
$('#translation_target_language').on('change', (event) => {
@ -422,6 +445,17 @@ jQuery(() => {
await writeSecret(extension_settings.translate.provider, key);
toastr.success('API Key saved');
});
$('#translate_url_button').on('click', async () => {
const optionText = $('#translation_provider option:selected').text();
const url = await callPopup(`<h3>${optionText} API URL</h3>`, 'input');
if (url == false) {
return;
}
await writeSecret(extension_settings.translate.provider + "_url", url);
toastr.success('API URL saved');
});
loadSettings();

View File

@ -4096,6 +4096,8 @@ const SECRET_KEYS = {
NOVEL: 'api_key_novel',
CLAUDE: 'api_key_claude',
DEEPL: 'deepl',
LIBRE: 'libre',
LIBRE_URL: 'libre_url',
OPENROUTER: 'api_key_openrouter',
SCALE: 'api_key_scale',
AI21: 'api_key_ai21',
@ -4324,6 +4326,46 @@ app.post('/horde_generateimage', jsonParser, async (request, response) => {
}
});
app.post('/libre_translate', jsonParser, async (request, response) => {
const key = readSecret(SECRET_KEYS.LIBRE);
const url = readSecret(SECRET_KEYS.LIBRE_URL);
const text = request.body.text;
const lang = request.body.lang;
if (!text || !lang) {
return response.sendStatus(400);
}
console.log('Input text: ' + text);
try {
const result = await fetch(url, {
method: "POST",
body: JSON.stringify({
q: text,
source: "auto",
target: lang,
format: "text",
api_key: key
}),
headers: { "Content-Type": "application/json" }
});
if (!result.ok) {
return response.sendStatus(result.status);
}
const json = await result.json();
console.log('Translated text: ' + json.translatedText);
return response.send(json.translatedText);
} catch (error) {
console.log("Translation error: " + error.message);
return response.sendStatus(500);
}
});
app.post('/google_translate', jsonParser, async (request, response) => {
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');