fix(TranslatePlus): improve layout and structure (#775)
* fix(TranslatePlus): improve layout and structure * Update translator.ts --------- Co-authored-by: thororen1234 <78185467+thororen1234@users.noreply.github.com>
This commit is contained in:
parent
82b8aae3ec
commit
fe007405cf
3 changed files with 17 additions and 7 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
margin-top: 0.5em;
|
margin-top: 0.5em;
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
font-weight: 400;
|
font-weight: 400;
|
||||||
|
display: block;
|
||||||
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
.eq-trans-accessory svg {
|
.eq-trans-accessory svg {
|
||||||
|
|
|
||||||
|
|
@ -28,12 +28,12 @@ export function Accessory({ message }: { message: Message; }) {
|
||||||
if (!translation) return null;
|
if (!translation) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<span className={cl("accessory")}>
|
<div className={cl("accessory")}>
|
||||||
<SmallIcon />
|
<SmallIcon />
|
||||||
{Parser.parse(translation.text)}
|
{Parser.parse(translation.text)}
|
||||||
{" "}
|
{" "}
|
||||||
(translated from {languages[translation.src] ?? translation.src} - <button onClick={() => setTranslation(undefined)} className={cl("dismiss")}>Dismiss</button>)
|
(translated from {languages[translation.src] ?? translation.src} - <button onClick={() => setTranslation(undefined)} className={cl("dismiss")}>Dismiss</button>)
|
||||||
</span>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,12 +84,20 @@ async function translateSitelen(message: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function google(target: string, text: string) {
|
async function google(target: string, text: string) {
|
||||||
const translate = await (await fetch(`https://translate.googleapis.com/translate_a/single?${new URLSearchParams({ client: "gtx", sl: "auto", tl: target, dt: "t", dj: "1", source: "input", q: text })}`)).json();
|
if (!text) return { src: "", text: "" };
|
||||||
|
try {
|
||||||
|
const res = await fetch(`https://translate.googleapis.com/translate_a/single?${new URLSearchParams({ client: "gtx", sl: "auto", tl: target, dt: "t", dj: "1", source: "input", q: text })}`);
|
||||||
|
if (!res.ok) throw new Error(`Request failed with status ${res.status}`);
|
||||||
|
const translate = await res.json();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
src: translate.src,
|
src: translate.src,
|
||||||
text: translate.sentences.map(s => s.trans).filter(Boolean).join("")
|
text: translate.sentences?.map(s => s.trans).filter(Boolean).join("\n")
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[TranslatePlus] Google Translate request failed:", error);
|
||||||
|
return { src: "en", text: "Translation failed due to an error." };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function translate(text: string): Promise<any> {
|
export async function translate(text: string): Promise<any> {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue