refactor: LibreTranslate and DeepL with separate environment variables for their configuration

This commit is contained in:
P. Reis 2024-10-08 16:53:30 -03:00
parent dbd590228d
commit 20caaa9ebd
4 changed files with 30 additions and 16 deletions

View file

@ -275,13 +275,21 @@ class Conf {
static get translationProvider(): string | undefined { static get translationProvider(): string | undefined {
return Deno.env.get('TRANSLATION_PROVIDER'); return Deno.env.get('TRANSLATION_PROVIDER');
} }
/** Translation provider URL endpoint. */ /** DeepL URL endpoint. */
static get translationProviderEndpoint(): string | undefined { static get deepLendpoint(): string | undefined {
return Deno.env.get('TRANSLATION_PROVIDER_ENDPOINT'); return Deno.env.get('DEEPL_ENDPOINT');
} }
/** Translation provider API KEY. */ /** DeepL API KEY. */
static get translationProviderApiKey(): string | undefined { static get deepLapiKey(): string | undefined {
return Deno.env.get('TRANSLATION_PROVIDER_API_KEY'); return Deno.env.get('DEEPL_API_KEY');
}
/** LibreTranslate URL endpoint. */
static get libreTranslateEndpoint(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_ENDPOINT');
}
/** LibreTranslate API KEY. */
static get libreTranslateApiKey(): string | undefined {
return Deno.env.get('LIBRETRANSLATE_API_KEY');
} }
/** Cache settings. */ /** Cache settings. */
static caches = { static caches = {

View file

@ -6,24 +6,30 @@ import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator
/** Set the translator used for translating posts. */ /** Set the translator used for translating posts. */
export const translatorMiddleware: AppMiddleware = async (c, next) => { export const translatorMiddleware: AppMiddleware = async (c, next) => {
const endpoint = Conf.translationProviderEndpoint; const deepLendpoint = Conf.deepLendpoint;
const apiKey = Conf.translationProviderApiKey; const deepLapiKey = Conf.deepLapiKey;
const libreTranslateEndpoint = Conf.libreTranslateEndpoint;
const libreTranslateApiKey = Conf.libreTranslateApiKey;
const translationProvider = Conf.translationProvider; const translationProvider = Conf.translationProvider;
switch (translationProvider) { switch (translationProvider) {
case 'deepl': case 'deepl':
if (apiKey) { if (deepLapiKey) {
c.set( c.set(
'translator', 'translator',
new DeepLTranslator({ endpoint, apiKey, fetch: fetchWorker }), new DeepLTranslator({ endpoint: deepLendpoint, apiKey: deepLapiKey, fetch: fetchWorker }),
); );
} }
break; break;
case 'libretranslate': case 'libretranslate':
if (apiKey) { if (libreTranslateApiKey) {
c.set( c.set(
'translator', 'translator',
new LibreTranslateTranslator({ endpoint, apiKey, fetch: fetchWorker }), new LibreTranslateTranslator({
endpoint: libreTranslateEndpoint,
apiKey: libreTranslateApiKey,
fetch: fetchWorker,
}),
); );
} }
break; break;

View file

@ -4,8 +4,8 @@ import { Conf } from '@/config.ts';
import { DeepLTranslator } from '@/translators/DeepLTranslator.ts'; import { DeepLTranslator } from '@/translators/DeepLTranslator.ts';
import { getLanguage } from '@/test.ts'; import { getLanguage } from '@/test.ts';
const endpoint = Conf.translationProviderEndpoint; const endpoint = Conf.deepLendpoint;
const apiKey = Conf.translationProviderApiKey; const apiKey = Conf.deepLapiKey;
const translationProvider = Conf.translationProvider; const translationProvider = Conf.translationProvider;
const deepL = 'deepl'; const deepL = 'deepl';

View file

@ -4,8 +4,8 @@ import { Conf } from '@/config.ts';
import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts'; import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts';
import { getLanguage } from '@/test.ts'; import { getLanguage } from '@/test.ts';
const endpoint = Conf.translationProviderEndpoint; const endpoint = Conf.libreTranslateEndpoint;
const apiKey = Conf.translationProviderApiKey; const apiKey = Conf.libreTranslateApiKey;
const translationProvider = Conf.translationProvider; const translationProvider = Conf.translationProvider;
const libreTranslate = 'libretranslate'; const libreTranslate = 'libretranslate';