This commit is contained in:
Mirco Wittrien 2021-06-01 16:17:59 +02:00
parent ca076ac8a4
commit 47ee057edf
2 changed files with 145 additions and 94 deletions

View File

@ -4852,6 +4852,15 @@ module.exports = (_ => {
InternalComponents.LibraryComponents.ChannelTextAreaButton = reactInitialized && class BDFDB_ChannelTextAreaButton extends LibraryModules.React.Component { InternalComponents.LibraryComponents.ChannelTextAreaButton = reactInitialized && class BDFDB_ChannelTextAreaButton extends LibraryModules.React.Component {
render() { render() {
const inner = BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN.textareabuttonwrapper,
children: BDFDB.ReactUtils.createElement(InternalComponents.LibraryComponents.SvgIcon, {
name: this.props.iconName,
iconSVG: this.props.iconSVG,
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.textareaicon, this.props.iconClassName, this.props.pulse && BDFDB.disCN.textareaiconpulse),
nativeClass: this.props.nativeClass
})
});
return BDFDB.ReactUtils.createElement(InternalComponents.LibraryComponents.Button, { return BDFDB.ReactUtils.createElement(InternalComponents.LibraryComponents.Button, {
look: InternalComponents.LibraryComponents.Button.Looks.BLANK, look: InternalComponents.LibraryComponents.Button.Looks.BLANK,
size: InternalComponents.LibraryComponents.Button.Sizes.NONE, size: InternalComponents.LibraryComponents.Button.Sizes.NONE,
@ -4863,15 +4872,7 @@ module.exports = (_ => {
onContextMenu: this.props.onContextMenu, onContextMenu: this.props.onContextMenu,
onMouseEnter: this.props.onMouseEnter, onMouseEnter: this.props.onMouseEnter,
onMouseLeave: this.props.onMouseLeave, onMouseLeave: this.props.onMouseLeave,
children: BDFDB.ReactUtils.createElement("div", { children: this.props.tooltip && this.props.tooltip.text ? BDFDB.ReactUtils.createElement(InternalComponents.LibraryComponents.TooltipContainer, Object.assign({}, this.props.tooltip, {children: inner})) : inner
className: BDFDB.disCN.textareabuttonwrapper,
children: BDFDB.ReactUtils.createElement(InternalComponents.LibraryComponents.SvgIcon, {
name: this.props.iconName,
iconSVG: this.props.iconSVG,
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.textareaicon, this.props.iconClassName, this.props.pulse && BDFDB.disCN.textareaiconpulse),
nativeClass: this.props.nativeClass
})
})
}); });
} }
}; };

View File

@ -2,7 +2,7 @@
* @name GoogleTranslateOption * @name GoogleTranslateOption
* @author DevilBro * @author DevilBro
* @authorId 278543574059057154 * @authorId 278543574059057154
* @version 2.2.5 * @version 2.2.6
* @description Allows you to translate Messages and your outgoing Message within Discord * @description Allows you to translate Messages and your outgoing Message within Discord
* @invite Jx3TjNS * @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien * @donate https://www.paypal.me/MircoWittrien
@ -17,12 +17,13 @@ module.exports = (_ => {
"info": { "info": {
"name": "GoogleTranslateOption", "name": "GoogleTranslateOption",
"author": "DevilBro", "author": "DevilBro",
"version": "2.2.5", "version": "2.2.6",
"description": "Allows you to translate Messages and your outgoing Message within Discord" "description": "Allows you to translate Messages and your outgoing Message within Discord"
}, },
"changeLog": { "changeLog": {
"improved": { "added": {
"Per Channel Outgoing Translation State": "Instead of toggling the outgoing translation for every chat it now remembers the state for each channel, can be disabled to the way it worked before in the plugin settings" "Show Original Message": "Added an option that, similar to send original message, allows you to translate someone elses message and have the plugin display the translation and the original message",
"Per Channel Languages": "You can now press the lock icon in the language settings, this will save any further changes for the languages in a separate save, allowing you to set up a separate language config for each channel"
} }
} }
}; };
@ -74,11 +75,16 @@ module.exports = (_ => {
const TranslateButtonComponent = class TranslateButton extends BdApi.React.Component { const TranslateButtonComponent = class TranslateButton extends BdApi.React.Component {
render() { render() {
const enabled = _this.isTranslationEnabled(this.props.channelId);
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ChannelTextAreaButton, { return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ChannelTextAreaButton, {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN._googletranslateoptiontranslatebutton, _this.isTranslationEnabled(this.props.channelId) && BDFDB.disCN._googletranslateoptiontranslating, BDFDB.disCN.textareapickerbutton), className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN._googletranslateoptiontranslatebutton, _this.isTranslationEnabled(this.props.channelId) && BDFDB.disCN._googletranslateoptiontranslating, BDFDB.disCN.textareapickerbutton),
isActive: this.props.isActive, isActive: this.props.isActive,
iconSVG: translateIcon, iconSVG: translateIcon,
nativeClass: true, nativeClass: true,
tooltip: enabled && {
text: (_ => `${BDFDB.LanguageUtils.getName(languages[_this.getLanguageChoice(languageTypes.INPUT, messageTypes.SENT, this.props.channelId)])}${BDFDB.LanguageUtils.getName(languages[_this.getLanguageChoice(languageTypes.OUTPUT, messageTypes.SENT, this.props.channelId)])}`),
tooltipConfig: {style: "max-width: 400px"}
},
onClick: _ => { onClick: _ => {
this.props.isActive = true; this.props.isActive = true;
BDFDB.ReactUtils.forceUpdate(this); BDFDB.ReactUtils.forceUpdate(this);
@ -105,12 +111,13 @@ module.exports = (_ => {
}; };
const TranslateSettingsComponent = class TranslateSettings extends BdApi.React.Component { const TranslateSettingsComponent = class TranslateSettings extends BdApi.React.Component {
filterLanguages(isOutput, place) { filterLanguages(direction, place) {
let isOutput = direction == languageTypes.OUTPUT;
return BDFDB.ObjectUtils.toArray(BDFDB.ObjectUtils.map(isOutput ? BDFDB.ObjectUtils.filter(languages, lang => !lang.auto) : languages, (lang, id) => ({ return BDFDB.ObjectUtils.toArray(BDFDB.ObjectUtils.map(isOutput ? BDFDB.ObjectUtils.filter(languages, lang => !lang.auto) : languages, (lang, id) => ({
value: id, value: id,
label: _this.getLanguageName(lang), label: BDFDB.LanguageUtils.getName(lang),
backup: this.isOnlyBackup(lang) backup: this.isOnlyBackup(lang)
}))).filter(isOutput && this.isOnlyBackup(languages[_this.getLanguageChoice("input", place)]) ? (n => n.backup) : (n => n)); }))).filter(isOutput && this.isOnlyBackup(languages[_this.getLanguageChoice(languageTypes.INPUT, place, this.props.channelId)]) ? (n => n.backup) : (n => n));
} }
isOnlyBackup(lang) { isOnlyBackup(lang) {
return lang.auto && !translationEngines[_this.settings.engines.translator].auto || !lang.auto && !lang.special && !translationEngines[_this.settings.engines.translator].languages.includes(lang.id); return lang.auto && !translationEngines[_this.settings.engines.translator].auto || !lang.auto && !lang.special && !translationEngines[_this.settings.engines.translator].languages.includes(lang.id);
@ -128,38 +135,61 @@ module.exports = (_ => {
className: BDFDB.disCN.marginbottom8 className: BDFDB.disCN.marginbottom8
}) })
], ],
Object.keys(_this.defaults.choices).map(key => { Object.keys(_this.defaults.choices).map(place => {
const isOutput = _this.defaults.choices[key].direction == "output"; const isSpecific = channelLanguages[this.props.channelId] && channelLanguages[this.props.channelId][place];
const place = _this.defaults.choices[key].place; return Object.keys(_this.defaults.choices[place].value).map(direction => [
return [
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, { BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: _this.defaults.choices[key].description, title: `${BDFDB.LibraryModules.StringUtils.upperCaseFirstChar(direction.toLowerCase())} Language in ${place.toLowerCase()} Messages: `,
titleChildren: isOutput ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Button, { titleChildren: direction == languageTypes.OUTPUT && [{
look: BDFDB.LibraryComponents.Button.Looks.BLANK, text: _ => isSpecific ? `${BDFDB.LibraryModules.StringUtils.upperCaseFirstChar(place)} Language Selection will be changed specifically for this Channel` : `${BDFDB.LibraryModules.StringUtils.upperCaseFirstChar(place)} Language Selection will be changed for all Channels`,
size: BDFDB.LibraryComponents.Button.Sizes.NONE, name: isSpecific ? BDFDB.LibraryComponents.SvgIcon.Names.LOCK_CLOSED : BDFDB.LibraryComponents.SvgIcon.Names.LOCK_OPEN,
color: isSpecific && "var(--bdfdb-red)",
onClick: _ => { onClick: _ => {
let input = _this.getLanguageChoice("input", place); if (channelLanguages[this.props.channelId] && channelLanguages[this.props.channelId][place]) {
let output = _this.getLanguageChoice("output", place); delete channelLanguages[this.props.channelId][place];
if (BDFDB.ObjectUtils.isEmpty(channelLanguages[this.props.channelId])) delete channelLanguages[this.props.channelId];
}
else {
if (!channelLanguages[this.props.channelId]) channelLanguages[this.props.channelId] = {};
channelLanguages[this.props.channelId][place] = {};
for (let l in languageTypes) channelLanguages[this.props.channelId][place][languageTypes[l]] = _this.getLanguageChoice(languageTypes[l], place, null);
}
BDFDB.DataUtils.save(channelLanguages, _this, "channelLanguages");
BDFDB.ReactUtils.forceUpdate(this);
}
}, {
iconSVG: `<svg width="21" height="21" fill="currentColor"><path d="M 0, 10.515 c 0, 2.892, 1.183, 5.521, 3.155, 7.361 L 0, 21.031 h 7.887 V 13.144 l -2.892, 2.892 C 3.549, 14.722, 2.629, 12.75, 2.629, 10.515 c 0 -3.418, 2.235 -6.309, 5.258 -7.492 v -2.629 C 3.418, 1.577, 0, 5.652, 0, 10.515 z M 21.031, 0 H 13.144 v 7.887 l 2.892 -2.892 C 17.482, 6.309, 18.402, 8.281, 18.402, 10.515 c 0, 3.418 -2.235, 6.309 -5.258, 7.492 V 20.768 c 4.469 -1.183, 7.887 -5.258, 7.887 -10.121 c 0 -2.892 -1.183 -5.521 -3.155 -7.361 L 21.031, 0 z"/></svg>`,
onClick: _ => {
let input = _this.getLanguageChoice(languageTypes.INPUT, place, this.props.channelId);
let output = _this.getLanguageChoice(languageTypes.OUTPUT, place, this.props.channelId);
input = input == "auto" ? "en" : input; input = input == "auto" ? "en" : input;
_this.settings.choices["input" + place] = output; _this.saveLanguageChoice(output, languageTypes.INPUT, place, this.props.channelId);
_this.settings.choices["output" + place] = input; _this.saveLanguageChoice(input, languageTypes.OUTPUT, place, this.props.channelId);
BDFDB.DataUtils.save(_this.settings.choices, _this, "choices");
_this.setLanguages(); _this.setLanguages();
BDFDB.ReactUtils.forceUpdate(this); BDFDB.ReactUtils.forceUpdate(this);
}, }
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, { }].map(data => {
className: BDFDB.disCN._googletranslateoptionreversebutton, const icon = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
iconSVG: `<svg width="21" height="21" fill="currentColor"><path d="M 0, 10.515 c 0, 2.892, 1.183, 5.521, 3.155, 7.361 L 0, 21.031 h 7.887 V 13.144 l -2.892, 2.892 C 3.549, 14.722, 2.629, 12.75, 2.629, 10.515 c 0 -3.418, 2.235 -6.309, 5.258 -7.492 v -2.629 C 3.418, 1.577, 0, 5.652, 0, 10.515 z M 21.031, 0 H 13.144 v 7.887 l 2.892 -2.892 C 17.482, 6.309, 18.402, 8.281, 18.402, 10.515 c 0, 3.418 -2.235, 6.309 -5.258, 7.492 V 20.768 c 4.469 -1.183, 7.887 -5.258, 7.887 -10.121 c 0 -2.892 -1.183 -5.521 -3.155 -7.361 L 21.031, 0 z"/></svg>` className: "aaa",
}) onClick: data.onClick,
}) : null, children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
width: 24,
height: 24,
color: data.color || "currentColor",
name: data.name,
iconSVG: data.iconSVG
})
});
return data.text ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {text: data.text, children: icon}) : icon;
}),
className: BDFDB.disCN.marginbottom8, className: BDFDB.disCN.marginbottom8,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Select, { children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Select, {
value: _this.getLanguageChoice(key), value: _this.getLanguageChoice(direction, place, this.props.channelId),
options: this.filterLanguages(isOutput, place), options: this.filterLanguages(direction, place),
optionRenderer: lang => languages[lang.value] ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, { optionRenderer: lang => languages[lang.value] ? BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
align: BDFDB.LibraryComponents.Flex.Align.CENTER, align: BDFDB.LibraryComponents.Flex.Align.CENTER,
children: [ children: [
@ -176,7 +206,7 @@ module.exports = (_ => {
nativeClass: true, nativeClass: true,
width: 20, width: 20,
height: 20, height: 20,
color: BDFDB.DiscordConstants.Colors.STATUS_RED, color: "var(--bdfdb-red)",
name: BDFDB.LibraryComponents.SvgIcon.Names.WARNING name: BDFDB.LibraryComponents.SvgIcon.Names.WARNING
}) })
}), }),
@ -191,16 +221,13 @@ module.exports = (_ => {
}) })
] ]
}) : null, }) : null,
onChange: value => { onChange: value => _this.saveLanguageChoice(value, direction, place, this.props.channelId)
_this.settings.choices[key] = value;
BDFDB.DataUtils.save(_this.settings.choices, _this, "choices");
}
}) })
}), }),
isOutput && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, { direction == languageTypes.OUTPUT && BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormDivider, {
className: BDFDB.disCN.marginbottom8 className: BDFDB.disCN.marginbottom8
}) })
]; ]);
}), }),
Object.keys(_this.defaults.engines).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, { Object.keys(_this.defaults.engines).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.FormComponents.FormItem, {
title: _this.defaults.engines[key].description, title: _this.defaults.engines[key].description,
@ -217,14 +244,14 @@ module.exports = (_ => {
} }
}) })
})), })),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, { Object.keys(_this.defaults.general).filter(key => _this.defaults.general[key].popout).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch", type: "Switch",
plugin: _this, plugin: _this,
keys: ["general", "sendOriginalMessage"], keys: ["general", key],
label: _this.defaults.general.sendOriginalMessage.description, label: _this.defaults.general[key].description,
tag: BDFDB.LibraryComponents.FormComponents.FormTitle.Tags.H5, tag: BDFDB.LibraryComponents.FormComponents.FormTitle.Tags.H5,
value: _this.settings.general.sendOriginalMessage value: _this.settings.general[key]
}), })),
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, { BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsItem, {
type: "Switch", type: "Switch",
label: "Translate your Messages before sending", label: "Translate your Messages before sending",
@ -258,25 +285,35 @@ module.exports = (_ => {
var languages = {}; var languages = {};
var favorites = []; var favorites = [];
var channelLanguages = {};
var translationEnabledStates = [], isTranslating; var translationEnabledStates = [], isTranslating;
var translatedMessages = {}, oldMessages = {}; var translatedMessages = {}, oldMessages = {};
const defaultLanguages = {
INPUT: "auto",
OUTPUT: "$discord"
};
const languageTypes = {
INPUT: "input",
OUTPUT: "output"
};
const messageTypes = {
RECEIVED: "received",
SENT: "sent",
};
return class GoogleTranslateOption extends Plugin { return class GoogleTranslateOption extends Plugin {
onLoad () { onLoad () {
_this = this; _this = this;
this.defaults = { this.defaults = {
general: { general: {
addTranslateButton: {value: true, description: "Adds a Translate Button to the Channel Textarea"}, addTranslateButton: {value: true, popout: false, description: "Adds a Translate Button to the Channel Textarea"},
usePerChatTranslation: {value: true, description: "Enables/Disables the Translator Button State per Channel and not globally"}, usePerChatTranslation: {value: true, popout: false, description: "Enables/Disables the Translator Button State per Channel and not globally"},
sendOriginalMessage: {value: false, description: "Sends the original Message together with the Translation"} sendOriginalMessage: {value: false, popout: true, description: "Also sends the original Message when translating your own Message"},
}, showOriginalMessage: {value: false, popout: true, description: "Also shows the original Message when translating someones Message"}
choices: {
inputContext: {value: "auto", direction: "input", place: "Context", description: "Input Language in received Messages: "},
outputContext: {value: "$discord", direction: "output", place: "Context", description: "Output Language in received Messages: "},
inputMessage: {value: "auto", direction: "input", place: "Message", description: "Input Language in your sent Messages: "},
outputMessage: {value: "$discord", direction: "output", place: "Message", description: "Output Language in your sent Messages: "}
}, },
choices: {},
exceptions: { exceptions: {
wordStart: {value: ["!"], max: 1, description: "Words starting with any of these will be ignored"} wordStart: {value: ["!"], max: 1, description: "Words starting with any of these will be ignored"}
}, },
@ -285,6 +322,7 @@ module.exports = (_ => {
backup: {value: "----", description: "Backup Engine"} backup: {value: "----", description: "Backup Engine"}
} }
}; };
for (let m in messageTypes) this.defaults.choices[messageTypes[m]] = {value: Object.keys(languageTypes).reduce((newObj, l) => (newObj[languageTypes[l]] = defaultLanguages[l], newObj), {})};
this.patchedModules = { this.patchedModules = {
before: { before: {
@ -303,15 +341,7 @@ module.exports = (_ => {
this.css = ` this.css = `
${BDFDB.dotCN._googletranslateoptiontranslatebutton + BDFDB.dotCNS._googletranslateoptiontranslating + BDFDB.dotCN.textareaicon} { ${BDFDB.dotCN._googletranslateoptiontranslatebutton + BDFDB.dotCNS._googletranslateoptiontranslating + BDFDB.dotCN.textareaicon} {
color: #F04747 !important; color: var(--bdfdb-red) !important;
}
${BDFDB.dotCN._googletranslateoptionreversebutton} {
opacity: 0.5;
margin-right: 5px;
transition: all 200ms ease;
}
${BDFDB.dotCN._googletranslateoptionreversebutton}:hover {
opacity: 1;
} }
`; `;
} }
@ -378,6 +408,8 @@ module.exports = (_ => {
favorites = BDFDB.DataUtils.load(this, "favorites"); favorites = BDFDB.DataUtils.load(this, "favorites");
favorites = !BDFDB.ArrayUtils.is(favorites) ? [] : favorites; favorites = !BDFDB.ArrayUtils.is(favorites) ? [] : favorites;
channelLanguages = BDFDB.DataUtils.load(this, "channelLanguages");
this.setLanguages(); this.setLanguages();
BDFDB.PatchUtils.forceAllUpdates(this); BDFDB.PatchUtils.forceAllUpdates(this);
BDFDB.MessageUtils.rerenderAll(); BDFDB.MessageUtils.rerenderAll();
@ -427,9 +459,14 @@ module.exports = (_ => {
let item = BDFDB.DOMUtils.getParent(BDFDB.dotCN.menuitem, event.target); let item = BDFDB.DOMUtils.getParent(BDFDB.dotCN.menuitem, event.target);
if (item) { if (item) {
let createTooltip = _ => { let createTooltip = _ => {
BDFDB.TooltipUtils.create(item, `${BDFDB.LanguageUtils.LibraryStrings.from} ${foundInput.name}:\n${text}\n\n${BDFDB.LanguageUtils.LibraryStrings.to} ${foundOutput.name}:\n${foundTranslation}`, { BDFDB.TooltipUtils.create(item, [
`${BDFDB.LanguageUtils.LibraryStrings.from} ${foundInput.name}:`,
text,
`${BDFDB.LanguageUtils.LibraryStrings.to} ${foundOutput.name}:`,
foundTranslation
].map(n => BDFDB.ReactUtils.createElement("div", {children: n})), {
type: "right", type: "right",
color: "brand", color: "black",
className: "googletranslate-tooltip" className: "googletranslate-tooltip"
}); });
}; };
@ -442,7 +479,7 @@ module.exports = (_ => {
} }
else if (!translating) { else if (!translating) {
translating = true; translating = true;
this.translateText(text, "context", (translation, input, output) => { this.translateText(text, messageTypes.RECEIVED, (translation, input, output) => {
if (translation) { if (translation) {
foundTranslation = translation, foundInput = input, foundOutput = output; foundTranslation = translation, foundInput = input, foundOutput = output;
createTooltip(); createTooltip();
@ -506,7 +543,7 @@ module.exports = (_ => {
BDFDB.PatchUtils.patch(this, e.instance, "handleSendMessage", {instead: e2 => { BDFDB.PatchUtils.patch(this, e.instance, "handleSendMessage", {instead: e2 => {
if (this.isTranslationEnabled(e.instance.props.channel.id)) { if (this.isTranslationEnabled(e.instance.props.channel.id)) {
e2.stopOriginalMethodCall(); e2.stopOriginalMethodCall();
this.translateText(e2.methodArguments[0], "message", (translation, input, output) => { this.translateText(e2.methodArguments[0], messageTypes.SENT, (translation, input, output) => {
translation = !translation ? e2.methodArguments[0] : (this.settings.general.sendOriginalMessage ? (e2.methodArguments[0] + "\n\n" + translation) : translation); translation = !translation ? e2.methodArguments[0] : (this.settings.general.sendOriginalMessage ? (e2.methodArguments[0] + "\n\n" + translation) : translation);
e2.originalMethod(translation); e2.originalMethod(translation);
}); });
@ -562,7 +599,7 @@ module.exports = (_ => {
if (e.instance.props.message) { if (e.instance.props.message) {
let translation = translatedMessages[e.instance.props.message.id]; let translation = translatedMessages[e.instance.props.message.id];
if (translation && translation.content) e.returnvalue.props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, { if (translation && translation.content) e.returnvalue.props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: `${BDFDB.LanguageUtils.LibraryStrings.from}: ${this.getLanguageName(translation.input)}\n${BDFDB.LanguageUtils.LibraryStrings.to}: ${this.getLanguageName(translation.output)}`, text: `${BDFDB.LanguageUtils.getName(translation.input)}${BDFDB.LanguageUtils.LibraryStrings.to} ${BDFDB.LanguageUtils.getName(translation.output)}`,
tooltipConfig: {style: "max-width: 400px"}, tooltipConfig: {style: "max-width: 400px"},
children: BDFDB.ReactUtils.createElement("span", { children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN._googletranslateoptiontranslated), className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN._googletranslateoptiontranslated),
@ -586,7 +623,7 @@ module.exports = (_ => {
else { else {
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {props: [["className", BDFDB.disCN.embeddescription]]}); let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {props: [["className", BDFDB.disCN.embeddescription]]});
if (index > -1) children[index].props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, { if (index > -1) children[index].props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: `${BDFDB.LanguageUtils.LibraryStrings.from}: ${this.getLanguageName(translation.input)}\n${BDFDB.LanguageUtils.LibraryStrings.to}: ${this.getLanguageName(translation.output)}`, text: `${BDFDB.LanguageUtils.getName(translation.input)}${BDFDB.LanguageUtils.getName(translation.output)}`,
tooltipConfig: {style: "max-width: 400px"}, tooltipConfig: {style: "max-width: 400px"},
children: BDFDB.ReactUtils.createElement("span", { children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN._googletranslateoptiontranslated), className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN._googletranslateoptiontranslated),
@ -653,15 +690,27 @@ module.exports = (_ => {
languages = BDFDB.ObjectUtils.sort(languages, "fav"); languages = BDFDB.ObjectUtils.sort(languages, "fav");
} }
getLanguageChoice (direction, place) { getLanguageChoice (direction, place, channelId) {
this.setLanguages(); this.setLanguages();
let type = place === undefined ? direction : direction.toLowerCase() + place.charAt(0).toUpperCase() + place.slice(1).toLowerCase(); let choice;
let choice = this.settings.choices[type]; if (channelLanguages[channelId] && channelLanguages[channelId][place]) choice = channelLanguages[channelId][place][direction];
else choice = this.settings.choices[place] && this.settings.choices[place][direction];
choice = languages[choice] ? choice : Object.keys(languages)[0]; choice = languages[choice] ? choice : Object.keys(languages)[0];
choice = type.indexOf("output") > -1 && choice == "auto" ? "en" : choice; choice = direction == languageTypes.OUTPUT && choice == "auto" ? "en" : choice;
return choice; return choice;
} }
saveLanguageChoice (choice, direction, place, channelId) {
if (channelLanguages[channelId] && channelLanguages[channelId][place]) {
channelLanguages[channelId][place][direction] = choice;
BDFDB.DataUtils.save(channelLanguages, this, "channelLanguages");
}
else {
this.settings.choices[place][direction] = choice;
BDFDB.DataUtils.save(this.settings.choices, this, "choices");
}
}
translateMessage (message, channel) { translateMessage (message, channel) {
return new Promise(callback => { return new Promise(callback => {
if (!message) return callback(null); if (!message) return callback(null);
@ -671,16 +720,20 @@ module.exports = (_ => {
callback(false); callback(false);
} }
else { else {
let content = message.content || ""; let orignalContent = message.content || "";
for (let embed of message.embeds) content += ("\n__________________ __________________ __________________\n" + embed.rawDescription); for (let embed of message.embeds) orignalContent += ("\n__________________ __________________ __________________\n" + embed.rawDescription);
this.translateText(content, "context", (translation, input, output) => { this.translateText(orignalContent, messageTypes.RECEIVED, (translation, input, output) => {
if (translation) { if (translation) {
oldMessages[message.id] = new BDFDB.DiscordObjects.Message(message); oldMessages[message.id] = new BDFDB.DiscordObjects.Message(message);
let oldStrings = orignalContent.split(/\n{0,1}__________________ __________________ __________________\n{0,1}/);
let strings = translation.split(/\n{0,1}__________________ __________________ __________________\n{0,1}/); let strings = translation.split(/\n{0,1}__________________ __________________ __________________\n{0,1}/);
let content = strings.shift().trim(), embeds = {}; let oldContent = this.settings.general.showOriginalMessage && (oldStrings.shift() || "").trim();
let content = (strings.shift() || "").trim() + (oldContent ? `\n\n${oldContent}` : "");
let embeds = {};
for (let i in message.embeds) { for (let i in message.embeds) {
message.embeds[i].message_id = message.id; message.embeds[i].message_id = message.id;
embeds[message.embeds[i].id] = (strings.shift() || message.embeds[i].rawDescription).trim(); let oldEmbedString = this.settings.general.showOriginalMessage && (oldStrings.shift() || "").trim();
embeds[message.embeds[i].id] = (strings.shift() || message.embeds[i].rawDescription).trim() + (oldEmbedString ? `\n\n${oldEmbedString}` : "");
} }
translatedMessages[message.id] = {content, embeds, input, output}; translatedMessages[message.id] = {content, embeds, input, output};
BDFDB.MessageUtils.rerenderAll(true); BDFDB.MessageUtils.rerenderAll(true);
@ -691,7 +744,7 @@ module.exports = (_ => {
}); });
} }
translateText (text, type, callback) { translateText (text, place, callback) {
let toast = null, toastInterval, finished = false, finishTranslation = translation => { let toast = null, toastInterval, finished = false, finishTranslation = translation => {
isTranslating = false; isTranslating = false;
if (toast) toast.close(); if (toast) toast.close();
@ -702,9 +755,10 @@ module.exports = (_ => {
if (translation) translation = this.addExceptions(translation, excepts); if (translation) translation = this.addExceptions(translation, excepts);
callback(translation == text ? "" : translation, input, output); callback(translation == text ? "" : translation, input, output);
}; };
let [newText, excepts, translate] = this.removeExceptions(text.trim(), type); let [newText, excepts, translate] = this.removeExceptions(text.trim(), place);
let input = Object.assign({}, languages[this.getLanguageChoice("input", type)]); let channelId = BDFDB.LibraryModules.LastChannelStore.getChannelId();
let output = Object.assign({}, languages[this.getLanguageChoice("output", type)]); let input = Object.assign({}, languages[this.getLanguageChoice(languageTypes.INPUT, place, channelId)]);
let output = Object.assign({}, languages[this.getLanguageChoice(languageTypes.OUTPUT, place, channelId)]);
if (translate && input.id != output.id) { if (translate && input.id != output.id) {
let specialCase = this.checkForSpecialCase(newText, input); let specialCase = this.checkForSpecialCase(newText, input);
if (specialCase) { if (specialCase) {
@ -815,6 +869,7 @@ module.exports = (_ => {
catch (err) {callback("");} catch (err) {callback("");}
} }
else { else {
console.log(response);
if (response.statusCode == 429 || response.statusCode == 456) BDFDB.NotificationUtils.toast(`${this.labels.toast_translating_failed}. ${this.labels.toast_translating_tryanother}. Request Limit reached.`, { if (response.statusCode == 429 || response.statusCode == 456) BDFDB.NotificationUtils.toast(`${this.labels.toast_translating_failed}. ${this.labels.toast_translating_tryanother}. Request Limit reached.`, {
type: "danger", type: "danger",
position: "center" position: "center"
@ -1044,9 +1099,9 @@ module.exports = (_ => {
return string; return string;
} }
removeExceptions (string, type) { removeExceptions (string, place) {
let excepts = {}, newString = [], count = 0; let excepts = {}, newString = [], count = 0;
if (type == "context") { if (place == messageTypes.RECEIVED) {
let text = [], i = 0; let text = [], i = 0;
string.split("").forEach(chara => { string.split("").forEach(chara => {
if (chara == "<" && text[i]) i++; if (chara == "<" && text[i]) i++;
@ -1080,11 +1135,6 @@ module.exports = (_ => {
return `https://translate.google.com/#${BDFDB.LanguageUtils.languages[input] ? input : "auto"}/${output}/${encodeURIComponent(text)}`; return `https://translate.google.com/#${BDFDB.LanguageUtils.languages[input] ? input : "auto"}/${output}/${encodeURIComponent(text)}`;
} }
getLanguageName (language) {
if (language.name.startsWith("Discord")) return language.name.slice(0, -1) + (language.ownlang && languages[language.id].name != language.ownlang ? ` / ${language.ownlang}` : "") + ")";
else return language.name + (language.ownlang && language.name != language.ownlang ? ` / ${language.ownlang}` : "");
}
setLabelsByLanguage () { setLabelsByLanguage () {
switch (BDFDB.LanguageUtils.getLanguage().id) { switch (BDFDB.LanguageUtils.getLanguage().id) {
case "bg": // Bulgarian case "bg": // Bulgarian