mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-07 21:53:04 -04:00
Translate: use newer google api with better results
This commit is contained in:
parent
77bed92c57
commit
1fa6181f7e
1 changed files with 12 additions and 26 deletions
|
@ -29,11 +29,8 @@ export const cl = classNameFactory("vc-trans-");
|
||||||
const Native = VencordNative.pluginHelpers.Translate as PluginNative<typeof import("./native")>;
|
const Native = VencordNative.pluginHelpers.Translate as PluginNative<typeof import("./native")>;
|
||||||
|
|
||||||
interface GoogleData {
|
interface GoogleData {
|
||||||
src: string;
|
translation: string;
|
||||||
sentences: {
|
sourceLanguage: string;
|
||||||
// 🏳️⚧️
|
|
||||||
trans: string;
|
|
||||||
}[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DeeplData {
|
interface DeeplData {
|
||||||
|
@ -77,21 +74,13 @@ export async function translate(kind: "received" | "sent", text: string): Promis
|
||||||
}
|
}
|
||||||
|
|
||||||
async function googleTranslate(text: string, sourceLang: string, targetLang: string): Promise<TranslationValue> {
|
async function googleTranslate(text: string, sourceLang: string, targetLang: string): Promise<TranslationValue> {
|
||||||
const url = "https://translate.googleapis.com/translate_a/single?" + new URLSearchParams({
|
const url = "https://translate-pa.googleapis.com/v1/translate?" + new URLSearchParams({
|
||||||
// see https://stackoverflow.com/a/29537590 for more params
|
"params.client": "gtx",
|
||||||
// holy shidd nvidia
|
"dataTypes": "TRANSLATION",
|
||||||
client: "gtx",
|
"key": "AIzaSyDLEeFI5OtFBwYBIoK_jj5m32rZK5CkCXA", // some google API key
|
||||||
// source language
|
"query.sourceLanguage": sourceLang,
|
||||||
sl: sourceLang,
|
"query.targetLanguage": targetLang,
|
||||||
// target language
|
"query.text": text,
|
||||||
tl: targetLang,
|
|
||||||
// what to return, t = translation probably
|
|
||||||
dt: "t",
|
|
||||||
// Send json object response instead of weird array
|
|
||||||
dj: "1",
|
|
||||||
source: "input",
|
|
||||||
// query, duh
|
|
||||||
q: text
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await fetch(url);
|
const res = await fetch(url);
|
||||||
|
@ -101,14 +90,11 @@ async function googleTranslate(text: string, sourceLang: string, targetLang: str
|
||||||
+ `\n${res.status} ${res.statusText}`
|
+ `\n${res.status} ${res.statusText}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const { src, sentences }: GoogleData = await res.json();
|
const { sourceLanguage, translation }: GoogleData = await res.json();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sourceLanguage: GoogleLanguages[src] ?? src,
|
sourceLanguage: GoogleLanguages[sourceLanguage] ?? sourceLanguage,
|
||||||
text: sentences.
|
text: translation
|
||||||
map(s => s?.trans).
|
|
||||||
filter(Boolean).
|
|
||||||
join("")
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue