Skip to content

Commit

Permalink
feat(Dislate): half ready to go GTranslate API support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico040 committed May 30, 2024
1 parent a779f92 commit b443b3d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 9 deletions.
30 changes: 30 additions & 0 deletions plugins/dislate/src/api/GTranslate.ts
Original file line number Diff line number Diff line change
@@ -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 }


4 changes: 3 additions & 1 deletion plugins/dislate/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import DeepL from "./DeepL"
import GTranslate from "./GTranslate"

export {
DeepL
DeepL,
GTranslate
}
2 changes: 2 additions & 0 deletions plugins/dislate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
12 changes: 10 additions & 2 deletions plugins/dislate/src/patches/ActionSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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",
Expand Down
19 changes: 13 additions & 6 deletions plugins/dislate/src/patches/Commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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: (
Expand Down
34 changes: 34 additions & 0 deletions plugins/dislate/src/settings/TranslatorPage.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<ScrollView style={{ flex: 1 }}>
<FormRow
label="DeepL"
trailing={() => <FormRow.Arrow />}
onPress={() => {
if (settings.translator == 0) return
settings.translator = 0
showToast(`Saved Translator to DeepL`, getAssetIDByName("check"))
}}
/>
<FormRow
label="Google Translate"
trailing={() => <FormRow.Arrow />}
onPress={() => {
if (settings.translator == 1) return
settings.translator = 1
showToast(`Saved Translator to Google Translate`, getAssetIDByName("check"))
}}
/>
</ScrollView>)
}
11 changes: 11 additions & 0 deletions plugins/dislate/src/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,6 +40,16 @@ export default () => {
render: TargetLang,
})}
/>
<FormRow
label={"Translator"}
subLabel={settings.translator ? "Google Translate" : "DeepL"}
leading={<FormRow.Icon source={getAssetIDByName("ic_locale_24px")} />}
trailing={() => <FormRow.Arrow />}
onPress={() => navigation.push("VendettaCustomPage", {
title: "Translator",
render: TranslatorPage,
})}
/>

<Text style={styles.subheaderText} onPress={() => url.openURL("https://github.com/aeongdesu/vdplugins")}>
{`Build: (${manifest.hash.substring(0, 7)})`}
Expand Down
6 changes: 6 additions & 0 deletions plugins/dislate/src/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ export interface DeepLResponse {
message?: string
data?: string
id?: number
}
export interface GTranslateResponse {
src?: string;
sentences?: {
trans?: string;
}[];
}

0 comments on commit b443b3d

Please sign in to comment.