artemis.js/src/utils/gtrans.ts
2025-02-17 23:25:32 +01:00

61 lines
1.2 KiB
TypeScript

import ky from "ky";
import { languageCodeToName, lazy } from "./functions";
import { readFileSync } from "node:fs";
import type { TranslateResult } from "../types/translate";
type TranslationResponse = {
src: string;
sentences: {
trans: string;
}[];
};
const client = ky.create({
prefixUrl: "https://translate.googleapis.com/translate_a",
});
const languageCodes = lazy(
() =>
JSON.parse(readFileSync("data/gtrans-langcodes.json", "utf8")) as string[]
);
export const getLanguages = lazy(() =>
languageCodes().map(code => ({
code,
name: languageCodeToName(code),
}))
);
export function isLanguage(code: string) {
return languageCodes().includes(code);
}
export async function translate(
text: string,
source = "auto",
target = "en"
): Promise<TranslateResult> {
const res = await client.get("single", {
searchParams: {
sl: source,
tl: target,
q: text,
client: "gtx",
dt: "t",
dj: "1",
source: "input",
},
});
const { sentences, src } = await res.json<TranslationResponse>();
return {
translatedText: sentences
.map(s => s?.trans)
.filter(Boolean)
.join(""),
detectedSourceLang: src,
model: "google",
};
}