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

Add support for embed translation (Title, Fields, Footer) #2676

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 94 additions & 32 deletions Plugins/Translator/Translator.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,27 +759,53 @@ module.exports = (_ => {
let translation = translatedMessages[e.instance.props.embed.message_id];
if (translation && Object.keys(translation.embeds).length) {
if (!e.returnvalue) e.instance.props.embed = Object.assign({}, e.instance.props.embed, {
rawDescription: translation.embeds[e.instance.props.embed.id],
originalDescription: e.instance.props.embed.originalDescription || e.instance.props.embed.rawDescription
rawDescription: translation.embeds[e.instance.props.embed.id].description,
rawTitle: translation.embeds[e.instance.props.embed.id].title,
footer: Object.assign({}, e.instance.props.embed.footer || {}, {
text: translation.embeds[e.instance.props.embed.id].footerText || ""
}),
fields: translation.embeds[e.instance.props.embed.id].fields.map(n => ({ rawName: n.name, rawValue: n.value })),
originalDescription: e.instance.props.embed.originalDescription || e.instance.props.embed.rawDescription,
originalTitle: e.instance.props.embed.originalTitle || e.instance.props.embed.rawTitle,
originalFields: e.instance.props.embed.originalFields || e.instance.props.embed.fields,
originalFooter: e.instance.props.embed.originalFooter || Object.assign({}, e.instance.props.embed.footer)
});
else {
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, {
text: `${BDFDB.LanguageUtils.getName(translation.input)} ➝ ${BDFDB.LanguageUtils.getName(translation.output)}`,
tooltipConfig: {style: "max-width: 400px"},
children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN._translatortranslated),
children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.disCN.messageedited,
children: `(${this.labels.translated_watermark})`
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, { props: [["className", BDFDB.disCN.embeddescription]] });
if (index > -1) {
// Ensure children[index].props.children is an array before attempting to push
// Fix for errors on multiple embeds in single messages
if (!Array.isArray(children[index].props.children)) {
children[index].props.children = [children[index].props.children];
}

children[index].props.children.push(
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: `${BDFDB.LanguageUtils.getName(translation.input)} ➝ ${BDFDB.LanguageUtils.getName(translation.output)}`,
tooltipConfig: { style: "max-width: 400px" },
children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.messagetimestamp, BDFDB.disCN.messagetimestampinline, BDFDB.disCN._translatortranslated),
children: BDFDB.ReactUtils.createElement("span", {
className: BDFDB.disCN.messageedited,
children: `(${this.labels.translated_watermark})`
})
})
})
})
}));
);
}
}
}
else if (!e.returnvalue && e.instance.props.embed.originalDescription) {
e.instance.props.embed = Object.assign({}, e.instance.props.embed, {rawDescription: e.instance.props.embed.originalDescription});
e.instance.props.embed = Object.assign({}, e.instance.props.embed, {
rawDescription: e.instance.props.embed.originalDescription,
rawTitle: e.instance.props.embed.originalTitle,
fields: e.instance.props.embed.originalFields,
footer: e.instance.props.embed.originalFooter
});
delete e.instance.props.embed.originalDescription;
delete e.instance.props.embed.originalTitle;
delete e.instance.props.embed.originalFields;
delete e.instance.props.embed.originalFooter;
}
}

Expand Down Expand Up @@ -869,24 +895,60 @@ module.exports = (_ => {
delete translatedMessages[message.id];
BDFDB.MessageUtils.rerenderAll(true);
callback(false);
}
else {
let orignalContent = message.content || "";
for (let embed of message.embeds) orignalContent += ("\n__________________ __________________ __________________\n" + embed.rawDescription);
this.translateText(orignalContent, messageTypes.RECEIVED, (translation, input, output) => {
if (translation) {
} else {
let originalContentData = {
content: message.content || "",
embeds: message.embeds.map(embed => ({
description: embed.rawDescription || "",
title: embed.rawTitle || "",
footerText: embed.footer ? embed.footer.text : "",
fields: embed.fields ? embed.fields.map(field => ({ name: field.rawName, value: field.rawValue })) : []
}))
};
let allTextsToTranslate = originalContentData.content;
originalContentData.embeds.forEach(embed => {
allTextsToTranslate += `\n__________________ __________________ __________________\n`;
allTextsToTranslate += embed.title + "\n" + embed.description;
embed.fields.forEach(field => {
allTextsToTranslate += "\n\n" + field.name + "__________________" + field.value;
});
if (embed.footerText) allTextsToTranslate += "\n" + embed.footerText;
});
message.embeds.forEach(embed => embed.message_id = message.id);
let embedIds = message.embeds.map(embed => embed.id);
this.translateText(allTextsToTranslate, messageTypes.RECEIVED, (translatedText, input, output) => {
if (translatedText) {
// Split the translated text back into components
let translatedSegments = translatedText.split(/\n{0,1}__________________ __________________ __________________\n{0,1}/);
let translatedContent = translatedSegments.shift();
// The first segment is the message content
// For each embed segment set id from embedIds
// Prepare the translated embeds based on the remaining segments
let translatedEmbeds = translatedSegments.reduce((dict, segment, index) => {
let embedId = embedIds[index];
let segmentLines = segment.split("\n");
let title = segmentLines.shift();
let description = segmentLines.shift();
let footerText = segmentLines.pop();
// Split remaining lines assuming they are fields separated by \n\n
// and field is separated by "__________________ " into name and value
let fieldsSegment = segmentLines.join("\n").split("\n\n");
let fields = fieldsSegment.map(line => {
let [name, value] = line.split("__________________");
return { name, value };
});

dict[embedId] = { title, description, fields, footerText };
return dict;
}, {});
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 oldContent = this.settings.general.showOriginalMessage && (oldStrings.shift() || "").trim();
let content = (strings.shift() || "").trim() + (oldContent ? (!this.settings.general.useSpoilerInOriginal ? ("\n\n> *" + oldContent.split("\n").join("*\n> *") + "*").replace(/> \*\*\n/g, "> \n") : `\n\n||${oldContent}||`) : "");
let embeds = {};
for (let i in message.embeds) {
message.embeds[i].message_id = message.id;
let oldEmbedString = this.settings.general.showOriginalMessage && (oldStrings.shift() || "").trim();
embeds[message.embeds[i].id] = (strings.shift() || message.embeds[i].rawDescription).trim() + (oldEmbedString ? (!this.settings.general.useSpoilerInOriginal ? ("\n\n> *" + oldEmbedString.split("\n").join("*\n> *") + "*").replace(/> \*\*\n/g, "> \n") : `\n\n||${oldEmbedString}||`) : "");
}
translatedMessages[message.id] = {content, embeds, input, output};
oldMessages[message.id].originalContentData = originalContentData;
translatedMessages[message.id] = {
content: translatedContent,
embeds: translatedEmbeds,
input,
output
};
BDFDB.MessageUtils.rerenderAll(true);
}
callback(true);
Expand Down Expand Up @@ -2468,4 +2530,4 @@ module.exports = (_ => {
}
};
})(window.BDFDB_Global.PluginUtils.buildPlugin(changeLog));
})();
})();