Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(translate): Auto Translate Received Messages #2618

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Next Next commit
feat(translate): Auto Translate Received Messages
  • Loading branch information
MrDiamondDog committed Feb 12, 2024
commit d294f087247e549ed76d04c199e54f5c0de60607
4 changes: 2 additions & 2 deletions src/plugins/translate/TranslateIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function TranslateIcon({ height = 24, width = 24, className }: { height?:
}

export const TranslateChatBarIcon: ChatBarButton = ({ isMainChat }) => {
const { autoTranslate } = settings.use(["autoTranslate"]);
const { autoTranslate, autoTranslateReceived } = settings.use(["autoTranslate", "autoTranslateReceived"]);

if (!isMainChat) return null;

Expand Down Expand Up @@ -81,7 +81,7 @@ export const TranslateChatBarIcon: ChatBarButton = ({ isMainChat }) => {
"aria-haspopup": "dialog"
}}
>
<TranslateIcon className={cl({ "auto-translate": autoTranslate, "chat-button": true })} />
<TranslateIcon className={cl({ "auto-translate": autoTranslate || autoTranslateReceived, "chat-button": true })} />
</ChatBarButton>
);
};
13 changes: 7 additions & 6 deletions src/plugins/translate/TranslateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ function LanguageSelect({ settingsKey, includeAuto }: { settingsKey: typeof Lang
);
}

function AutoTranslateToggle() {
const value = settings.use(["autoTranslate"]).autoTranslate;
function TranslateToggle({ setting, label }: { setting: "autoTranslate" | "autoTranslateReceived", label: string; }) {
const value = settings.use([setting])[setting];

return (
<Switch
value={value}
onChange={v => settings.store.autoTranslate = v}
note={settings.def.autoTranslate.description}
onChange={v => settings.store[setting] = v}
note={settings.def[setting].description}
hideBorder
>
Auto Translate
{label}
</Switch>
);
}
Expand All @@ -94,7 +94,8 @@ export function TranslateModal({ rootProps }: { rootProps: ModalProps; }) {

<Forms.FormDivider className={Margins.bottom16} />

<AutoTranslateToggle />
<TranslateToggle label="Auto Translate" setting="autoTranslate" />
<TranslateToggle label="Auto Translate Received" setting="autoTranslateReceived" />
</ModalContent>
</ModalRoot>
);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/translate/TranslationAccessory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Languages } from "./languages";
import { TranslateIcon } from "./TranslateIcon";
import { cl, TranslationValue } from "./utils";

const TranslationSetters = new Map<string, (v: TranslationValue) => void>();
export const TranslationSetters = new Map<string, (v: TranslationValue) => void>();

export function handleTranslate(messageId: string, data: TranslationValue) {
TranslationSetters.get(messageId)!(data);
Expand Down
18 changes: 16 additions & 2 deletions src/plugins/translate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { addPreSendListener, removePreSendListener } from "@api/MessageEvents";
import { addButton, removeButton } from "@api/MessagePopover";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { ChannelStore, Menu } from "@webpack/common";
import { ChannelStore, Menu, UserStore } from "@webpack/common";
import { Util } from "Vencord";

import { settings } from "./settings";
import { TranslateChatBarIcon, TranslateIcon } from "./TranslateIcon";
Expand Down Expand Up @@ -54,7 +55,7 @@ const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) =>
export default definePlugin({
name: "Translate",
description: "Translate messages with Google Translate",
authors: [Devs.Ven],
authors: [Devs.Ven, Devs.MrDiamond],
dependencies: ["MessageAccessoriesAPI", "MessagePopoverAPI", "MessageEventsAPI", "ChatInputButtonAPI"],
settings,
// not used, just here in case some other plugin wants it or w/e
Expand Down Expand Up @@ -96,4 +97,17 @@ export default definePlugin({
removeButton("vc-translate");
removeAccessory("vc-translation");
},

flux: {
MESSAGE_CREATE: async event => {
if (!settings.store.autoTranslateReceived) return;
if (event.channelId !== Util.getCurrentChannel().id) return;
if (event.message.author.id === UserStore.getCurrentUser().id) return;

console.log(event.message);

const trans = await translate("received", event.message.content);
handleTranslate(event.message.id, trans);
}
}
});
5 changes: 5 additions & 0 deletions src/plugins/translate/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export const settings = definePluginSettings({
type: OptionType.BOOLEAN,
description: "Automatically translate your messages before sending. You can also shift/right click the translate button to toggle this",
default: false
},
autoTranslateReceived: {
type: OptionType.BOOLEAN,
description: "Automatically translate received messages",
default: false
}
}).withPrivateSettings<{
showAutoTranslateAlert: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({
coolelectronics: {
name: "coolelectronics",
id: 696392247205298207n,
},
MrDiamond: {
name: "MrDiamond",
id: 523338295644782592n
}
} satisfies Record<string, Dev>);

Expand Down
Loading