From b443b3d9ed9b7a8f4c7dec4e31e20485343ccac7 Mon Sep 17 00:00:00 2001
From: Rico040 <93081680+Rico040@users.noreply.github.com>
Date: Thu, 30 May 2024 22:17:25 +1000
Subject: [PATCH] feat(Dislate): half ready to go GTranslate API support
---
plugins/dislate/src/api/GTranslate.ts | 30 ++++++++++++++++
plugins/dislate/src/api/index.ts | 4 ++-
plugins/dislate/src/index.ts | 2 ++
plugins/dislate/src/patches/ActionSheet.tsx | 12 +++++--
plugins/dislate/src/patches/Commands.tsx | 19 +++++++----
.../dislate/src/settings/TranslatorPage.tsx | 34 +++++++++++++++++++
plugins/dislate/src/settings/index.tsx | 11 ++++++
plugins/dislate/src/type.d.ts | 6 ++++
8 files changed, 109 insertions(+), 9 deletions(-)
create mode 100644 plugins/dislate/src/api/GTranslate.ts
create mode 100644 plugins/dislate/src/settings/TranslatorPage.tsx
diff --git a/plugins/dislate/src/api/GTranslate.ts b/plugins/dislate/src/api/GTranslate.ts
new file mode 100644
index 0000000..2b30423
--- /dev/null
+++ b/plugins/dislate/src/api/GTranslate.ts
@@ -0,0 +1,30 @@
+// "inspired" by https://github.com/Vendicated/Vencord/blob/main/src/plugins/translate/utils.ts
+import { GTranslateResponse } from "../type"
+
+const translate = async (text: string, source_lang: string = "auto", target_lang: string, original: boolean = false) => {
+ try {
+ if (original) return { source_lang, text }
+
+ const API_URL = "https://translate.googleapis.com/translate_a/single?" + new URLSearchParams({
+ client: "gtx",
+ sl: source_lang,
+ tl: target_lang,
+ dt: "t",
+ dj: "1",
+ source: "input",
+ q: text
+ });
+
+ const data: GTranslateResponse = await (await fetch(API_URL)).json()
+
+ console.log(data)
+
+ return { source_lang, text: data.sentences[0].trans }
+ } catch (e) {
+ throw Error(`Failed to fetch from Google Translate: ${e}`)
+ }
+}
+
+export default { translate }
+
+
diff --git a/plugins/dislate/src/api/index.ts b/plugins/dislate/src/api/index.ts
index e67f382..f989145 100644
--- a/plugins/dislate/src/api/index.ts
+++ b/plugins/dislate/src/api/index.ts
@@ -1,5 +1,7 @@
import DeepL from "./DeepL"
+import GTranslate from "./GTranslate"
export {
- DeepL
+ DeepL,
+ GTranslate
}
\ No newline at end of file
diff --git a/plugins/dislate/src/index.ts b/plugins/dislate/src/index.ts
index 1acc998..af10d39 100644
--- a/plugins/dislate/src/index.ts
+++ b/plugins/dislate/src/index.ts
@@ -6,9 +6,11 @@ import Settings from "./settings"
export const settings: {
source_lang?: string
target_lang?: string
+ translator?: number
} = storage
settings.target_lang ??= "EN"
+settings.translator ??= 0
let patches = []
diff --git a/plugins/dislate/src/patches/ActionSheet.tsx b/plugins/dislate/src/patches/ActionSheet.tsx
index 8df5667..cc4761c 100644
--- a/plugins/dislate/src/patches/ActionSheet.tsx
+++ b/plugins/dislate/src/patches/ActionSheet.tsx
@@ -7,7 +7,7 @@ import { Forms } from "@vendetta/ui/components"
import { findInReactTree } from "@vendetta/utils"
import { settings } from ".."
-import { DeepL } from "../api"
+import { DeepL, GTranslate } from "../api"
import { showToast } from "@vendetta/ui/toasts"
import { logger } from "@vendetta"
@@ -55,7 +55,15 @@ export default () => before("openLazy", LazyActionSheet, ([component, key, msg])
const target_lang = settings.target_lang
const isTranslated = translateType === "Translate"
- const translate = await DeepL.translate(originalMessage.content, undefined, target_lang, !isTranslated)
+ var translate
+ switch(settings.translator) {
+ case 0:
+ console.log("Translating with DeepL: ", originalMessage.content)
+ translate = await DeepL.translate(originalMessage.content, null, target_lang, !isTranslated)
+ case 1:
+ console.log("Translating with GTranslate: ", originalMessage.content)
+ translate = await GTranslate.translate(originalMessage.content, null, target_lang, !isTranslated)
+ }
FluxDispatcher.dispatch({
type: "MESSAGE_UPDATE",
diff --git a/plugins/dislate/src/patches/Commands.tsx b/plugins/dislate/src/patches/Commands.tsx
index 9d3cfe2..25f5d2b 100644
--- a/plugins/dislate/src/patches/Commands.tsx
+++ b/plugins/dislate/src/patches/Commands.tsx
@@ -6,12 +6,13 @@ import { getAssetIDByName } from "@vendetta/ui/assets"
import { Codeblock } from "@vendetta/ui/components"
import { showConfirmationAlert } from "@vendetta/ui/alerts"
import { findByProps } from "@vendetta/metro"
+import { settings } from ".."
-import lang from "../lang"
-import { DeepL } from "../api"
+import Lang from "../lang"
+import { DeepL, GTranslate } from "../api"
const ClydeUtils = findByProps("sendBotMessage")
-const langOptions = Object.entries(lang).map(([key, value]) => ({
+const langOptions = Object.entries(Lang).map(([key, value]) => ({
name: key,
displayName: key,
value: value
@@ -20,8 +21,8 @@ const langOptions = Object.entries(lang).map(([key, value]) => ({
export default () => registerCommand({
name: "translate",
displayName: "translate",
- description: "Send a message using Dislate in any language chosen, using the DeepL Translate API.",
- displayDescription: "Send a message using Dislate in any language chosen, using the DeepL Translate API.",
+ description: "Send a message using Dislate in any language chosen.",
+ displayDescription: "Send a message using Dislate in any language chosen.",
applicationId: "-1",
type: ApplicationCommandType.CHAT as number,
inputType: ApplicationCommandInputType.BUILT_IN_TEXT as number,
@@ -48,7 +49,13 @@ export default () => registerCommand({
async execute(args, ctx) {
const [text, lang] = args
try {
- const content = await DeepL.translate(text.value, null, lang.value)
+ var content
+ switch(settings.translator) {
+ case 0:
+ content = await DeepL.translate(text.value, null, lang.value)
+ case 1:
+ content = await GTranslate.translate(text.value, null, lang.value)
+ }
return await new Promise((resolve): void => showConfirmationAlert({
title: "Are you sure you want to send it?",
content: (
diff --git a/plugins/dislate/src/settings/TranslatorPage.tsx b/plugins/dislate/src/settings/TranslatorPage.tsx
new file mode 100644
index 0000000..05187c0
--- /dev/null
+++ b/plugins/dislate/src/settings/TranslatorPage.tsx
@@ -0,0 +1,34 @@
+import { getAssetIDByName } from "@vendetta/ui/assets"
+import { React, ReactNative } from "@vendetta/metro/common"
+import { Forms } from "@vendetta/ui/components"
+import { showToast } from "@vendetta/ui/toasts"
+import { useProxy } from "@vendetta/storage"
+import { settings } from ".."
+
+const { FormRow } = Forms
+const { ScrollView } = ReactNative
+
+export default () => {
+ useProxy(settings)
+ return (
+
+ }
+ onPress={() => {
+ if (settings.translator == 0) return
+ settings.translator = 0
+ showToast(`Saved Translator to DeepL`, getAssetIDByName("check"))
+ }}
+ />
+ }
+ onPress={() => {
+ if (settings.translator == 1) return
+ settings.translator = 1
+ showToast(`Saved Translator to Google Translate`, getAssetIDByName("check"))
+ }}
+ />
+ )
+}
\ No newline at end of file
diff --git a/plugins/dislate/src/settings/index.tsx b/plugins/dislate/src/settings/index.tsx
index 5e50b52..8e82973 100644
--- a/plugins/dislate/src/settings/index.tsx
+++ b/plugins/dislate/src/settings/index.tsx
@@ -7,6 +7,7 @@ import { useProxy } from "@vendetta/storage"
import { settings } from ".."
import TargetLang from "./TargetLang"
+import TranslatorPage from "./TranslatorPage"
const { ScrollView, Text } = ReactNative
const { FormRow } = Forms
@@ -39,6 +40,16 @@ export default () => {
render: TargetLang,
})}
/>
+ }
+ trailing={() => }
+ onPress={() => navigation.push("VendettaCustomPage", {
+ title: "Translator",
+ render: TranslatorPage,
+ })}
+ />
url.openURL("https://github.com/aeongdesu/vdplugins")}>
{`Build: (${manifest.hash.substring(0, 7)})`}
diff --git a/plugins/dislate/src/type.d.ts b/plugins/dislate/src/type.d.ts
index 0a7e9d1..d9eddf1 100644
--- a/plugins/dislate/src/type.d.ts
+++ b/plugins/dislate/src/type.d.ts
@@ -4,4 +4,10 @@ export interface DeepLResponse {
message?: string
data?: string
id?: number
+}
+export interface GTranslateResponse {
+ src?: string;
+ sentences?: {
+ trans?: string;
+ }[];
}
\ No newline at end of file