Add OneRingTranslator #521

This commit is contained in:
Cohee 2023-09-10 17:27:50 +03:00
parent ec23356c99
commit 70071312d3
3 changed files with 98 additions and 1 deletions

View File

@ -136,7 +136,7 @@ const languageCodes = {
};
const KEY_REQUIRED = ['deepl','libre'];
const LOCAL_URL = ['libre'];
const LOCAL_URL = ['libre', 'oneringtranslator'];
function showKeysButton() {
const providerRequiresKey = KEY_REQUIRED.includes(extension_settings.translate.provider);
@ -144,6 +144,7 @@ function showKeysButton() {
$("#translate_key_button").toggle(providerRequiresKey);
$("#translate_key_button").toggleClass('success', Boolean(secret_state[extension_settings.translate.provider]));
$("#translate_url_button").toggle(providerOptionalUrl);
$("#translate_url_button").toggleClass('success', Boolean(secret_state[extension_settings.translate.provider + "_url"]));
}
function loadSettings() {
@ -184,6 +185,31 @@ async function translateIncomingMessage(messageId) {
updateMessageBlock(messageId, message);
}
async function translateProviderOneRing(text, lang) {
let from_lang = lang == extension_settings.translate.internal_language
? extension_settings.translate.target_language
: extension_settings.translate.internal_language;
const response = await fetch('/api/translate/onering', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ text: text, from_lang: from_lang, to_lang: lang }),
});
if (response.ok) {
const result = await response.text();
return result;
}
throw new Error(response.statusText);
}
/**
* Translates text using the LibreTranslate API
* @param {string} text Text to translate
* @param {string} lang Target language code
* @returns {Promise<string>} Translated text
*/
async function translateProviderLibre(text, lang) {
const response = await fetch('/api/translate/libre', {
method: 'POST',
@ -199,6 +225,12 @@ async function translateProviderLibre(text, lang) {
throw new Error(response.statusText);
}
/**
* Translates text using the Google Translate API
* @param {string} text Text to translate
* @param {string} lang Target language code
* @returns {Promise<string>} Translated text
*/
async function translateProviderGoogle(text, lang) {
const response = await fetch('/api/translate/google', {
method: 'POST',
@ -214,6 +246,12 @@ async function translateProviderGoogle(text, lang) {
throw new Error(response.statusText);
}
/**
* Translates text using the DeepL API
* @param {string} text Text to translate
* @param {string} lang Target language code
* @returns {Promise<string>} Translated text
*/
async function translateProviderDeepl(text, lang) {
if (!secret_state.deepl) {
throw new Error('No DeepL API key');
@ -233,6 +271,12 @@ async function translateProviderDeepl(text, lang) {
throw new Error(response.statusText);
}
/**
* Translates text using the selected translation provider
* @param {string} text Text to translate
* @param {string} lang Target language code
* @returns {Promise<string>} Translated text
*/
async function translate(text, lang) {
try {
if (text == '') {
@ -246,6 +290,8 @@ async function translate(text, lang) {
return await translateProviderGoogle(text, lang);
case 'deepl':
return await translateProviderDeepl(text, lang);
case 'oneringtranslator':
return await translateProviderOneRing(text, lang);
default:
console.error('Unknown translation provider', extension_settings.translate.provider);
return text;
@ -391,6 +437,7 @@ jQuery(() => {
<option value="libre">Libre</option>
<option value="google">Google</option>
<option value="deepl">DeepL</option>
<option value="oneringtranslator">OneRingTranslator</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>
@ -448,6 +495,7 @@ jQuery(() => {
const optionText = $('#translation_provider option:selected').text();
const exampleURLs = {};
exampleURLs['libre'] = 'http://127.0.0.1:5000/translate';
exampleURLs['oneringtranslator'] = 'http://127.0.0.1:4990/translate';
const url = await callPopup(`<h3>${optionText} API URL</h3><i>Example: <tt>` + exampleURLs[extension_settings.translate.provider] + `</tt></i>`, 'input');
if (url == false) {

View File

@ -16,6 +16,7 @@ const SECRET_KEYS = {
SCALE: 'api_key_scale',
AI21: 'api_key_ai21',
SCALE_COOKIE: 'scale_cookie',
ONERING_URL: 'oneringtranslator_url',
}
/**

View File

@ -132,6 +132,54 @@ function registerEndpoints(app, jsonParser) {
return response.sendStatus(500);
}
});
app.post('/api/translate/onering', jsonParser, async (request, response) => {
const url = readSecret(SECRET_KEYS.ONERING_URL);
if (!url) {
console.log('OneRing URL is not configured.');
return response.sendStatus(401);
}
const text = request.body.text;
const from_lang = request.body.from_lang;
const to_lang = request.body.to_lang;
if (!text || !from_lang || !to_lang) {
return response.sendStatus(400);
}
const params = new URLSearchParams();
params.append('text', text);
params.append('from_lang', from_lang);
params.append('to_lang', to_lang);
console.log('Input text: ' + text);
try {
const fetchUrl = new URL(url);
fetchUrl.search = params.toString();
const result = await fetch(fetchUrl, {
method: 'GET',
timeout: 0,
});
if (!result.ok) {
const error = await result.text();
console.log('OneRing error: ', result.statusText, error);
return response.sendStatus(result.status);
}
const data = await result.json();
console.log('Translated text: ' + data.result);
return response.send(data.result);
} catch (error) {
console.log("Translation error: " + error.message);
return response.sendStatus(500);
}
});
}
module.exports = {