mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
feat: create translatorMiddleware
This commit is contained in:
parent
a2d8478e1a
commit
8e58b1a7d4
1 changed files with 33 additions and 0 deletions
33
src/middleware/translatorMiddleware.ts
Normal file
33
src/middleware/translatorMiddleware.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { AppMiddleware } from '@/app.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { fetchWorker } from '@/workers/fetch.ts';
|
||||
import { DeepLTranslator } from '@/translators/DeepLTranslator.ts';
|
||||
import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts';
|
||||
|
||||
/** Set the translator used for translating posts. */
|
||||
export const translatorMiddleware: AppMiddleware = async (c, next) => {
|
||||
const endpoint = Conf.translationProviderEndpoint;
|
||||
const apiKey = Conf.translationProviderApiKey;
|
||||
const translationProvider = Conf.translationProvider;
|
||||
|
||||
switch (translationProvider) {
|
||||
case 'DeepL'.toLowerCase():
|
||||
if (apiKey) {
|
||||
c.set(
|
||||
'translator',
|
||||
new DeepLTranslator({ endpoint, apiKey, fetch: fetchWorker }),
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'Libretranslate'.toLowerCase():
|
||||
if (apiKey) {
|
||||
c.set(
|
||||
'translator',
|
||||
new LibreTranslateTranslator({ endpoint, apiKey, fetch: fetchWorker }),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
await next();
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue