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 {
return Deno.env.get('TRANSLATION_PROVIDER');
}
/** Translation provider URL endpoint. */
static get translationProviderEndpoint(): string | undefined {
return Deno.env.get('TRANSLATION_PROVIDER_ENDPOINT');
/** DeepL URL endpoint. */
static get deepLendpoint(): string | undefined {
return Deno.env.get('DEEPL_ENDPOINT');
}
/** Translation provider API KEY. */
static get translationProviderApiKey(): string | undefined {
return Deno.env.get('TRANSLATION_PROVIDER_API_KEY');
/** DeepL API KEY. */
static get deepLapiKey(): string | undefined {
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. */
static caches = {

View file

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

View file

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

View file

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