libretranslate-extended
Version:
Extended API wrapper for LibreTranslate with built-in language detection and improvements.
45 lines (44 loc) • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectLanguage = void 0;
// detectLanguage.ts
const contants_1 = require("./contants");
const deepl_1 = require("./providers/deepl");
const ftapi_1 = require("./providers/ftapi");
const libretranslate_1 = require("./providers/libretranslate");
require("dotenv/config");
async function detectLanguage(text, cfg) {
const provider = cfg?.provider ?? contants_1.DEFAULTS.provider;
if (provider === "ftapi") {
// Truco FTAPI: /translate sin 'sl' => autodetecta; leemos "source-language"
const { detectedSourceLang } = await (0, ftapi_1.ftapiTranslate)({ query: text, target: "en" }, { baseUrl: cfg?.baseUrl ?? contants_1.DEFAULTS.baseUrl });
if (!detectedSourceLang)
throw new Error("FTAPI: could not detect language");
return detectedSourceLang;
}
if (provider === "deepl") {
const { detectedSourceLang } = await (0, deepl_1.deeplTranslate)({ query: text, target: "EN" }, { apiKey: cfg?.apiKey ?? process.env.DEEPL_API_KEY });
if (!detectedSourceLang)
throw new Error("DeepL: detection failed");
return detectedSourceLang.toLowerCase();
}
// LibreTranslate: primero /detect; si falla, fallback a /translate con source:"auto"
const baseUrl = cfg?.baseUrl ?? contants_1.DEFAULTS.baseUrl ?? "http://localhost:5000";
try {
const det = await (0, libretranslate_1.libreTranslateDetect)(text, {
baseUrl,
apiKey: cfg?.apiKey ?? contants_1.DEFAULTS.apiKey,
});
return det.language;
}
catch {
const t = await (0, libretranslate_1.libreTranslateTranslate)({ query: text, source: "auto", target: "en" }, { baseUrl, apiKey: cfg?.apiKey ?? contants_1.DEFAULTS.apiKey });
const lang = Array.isArray(t.detectedLanguage)
? t.detectedLanguage[0]?.language
: t.detectedLanguage?.language;
if (!lang)
throw new Error("LibreTranslate: detection failed via /translate fallback");
return lang;
}
}
exports.detectLanguage = detectLanguage;