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

DRAFT - Important rewrite of the module #35

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
u
  • Loading branch information
p4535992 committed May 5, 2024
commit 453d5f38a537e38b90885e840d3a123544dc2f0e
17 changes: 0 additions & 17 deletions src/scripts/ComprehendLanguages.js

This file was deleted.

245 changes: 0 additions & 245 deletions src/scripts/ComprehendLanguagesTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,222 +11,6 @@ import {
determineNewName,
} from "./lib";

export class Translator {
/**
*
* @param {JournalEntry|Object} documentToTranslate
* @returns {Promise<void>}
*/
translateButton(documentToTranslate) {}
}

export class JournalEntryTranslator extends Translator {
/**
*
* @param {JournalEntry} documentToTranslate
* @returns {Promise<void>}
*/
async translateButton(documentToTranslate) {
const { token, target_lang, makeSeparateFolder, translateInPlace } = await getTranslationSettings();
if (!token) {
dialogTokenMissing();
} else {
if (!translateInPlace) {
await this.translateAndCreateJournalEntry(documentToTranslate, target_lang, makeSeparateFolder, token);
} else {
await this.translateAndReplaceOriginal(documentToTranslate, target_lang, token);
}
}
}

/**
*
* @param {JournalEntry} documentToTranslate
* @param {string} target_lang
* @param {string} token
* @returns {Promise<void>}
*/
async translateAndReplaceOriginal(documentToTranslate, target_lang, token) {
const pages = documentToTranslate.pages;
pages.map(async (page) => {
const journalText = await this.getJournalPageText(page);
let translation = await translate_html(journalText, token, target_lang);
await page.update({
text: {
content: translation,
format: CONST.JOURNAL_ENTRY_PAGE_FORMATS.HTML,
},
});
});
}

/**
*
* @param {JournalEntry} documentToTranslate
* @param {string} target_lang
* @param {boolean} makeSeparateFolder
* @param {string} token
* @returns {Promise<void>}
*/
async translateAndCreateJournalEntry(documentToTranslate, target_lang, makeSeparateFolder, token) {
const folder = await determineFolder(documentToTranslate, target_lang, makeSeparateFolder);
const pages = documentToTranslate.pages;
let newName = await determineNewName(documentToTranslate);
const newPages = await Promise.all(
pages.map(async (page) => this.translateSinglePage(page, token, target_lang)),
).catch((e) => {
new ErrorDialog(e.message);
});
if (newPages) {
const newJournalEntry = await JournalEntry.createDocuments([
{ ...documentToTranslate, name: newName, folder: folder },
]);
await newJournalEntry[0].createEmbeddedDocuments("JournalEntryPage", newPages.flat());
}
}

/**
*
* @param {JournalEntryPage} journalPage
* @param {string} token
* @param {string} target_lang
* @returns {Promise<JournalEntryPage>}
*/
async translateSinglePage(journalPage, token, target_lang) {
const journalText = await this.getJournalPageText(journalPage);
let translation = await translate_html(journalText, token, target_lang);
const newJournalPage = await this.createNewJournalEntry(journalPage, translation);
return newJournalPage;
}

/**
*
* @param {JournalEntryPage} journalPage
* @returns {Promise<string>}
*/
async getJournalPageText(journalPage) {
if (journalPage.text.content) {
let text = journalPage.text.content;
text = text.replace("#", "");
return text;
} else {
return "";
}
}

/**
*
* @param {JournalEntryPage} journal
* @param {string} translation
* @returns {Promise<JournalEntryPage[]>}
*/
async createNewJournalEntry(journal, translation) {
const newName = await determineNewName(journal);
const newJournalPage = [
{
...journal,
name: newName,
text: {
content: translation,
format: CONST.JOURNAL_ENTRY_PAGE_FORMATS.HTML,
},
},
];
return newJournalPage;
}
}

export class ItemTranslator extends Translator {
/**
*
* @param {Item} documentToTranslate
* @returns {Promise<void>}
*/
async translateButton(documentToTranslate) {
const { token, target_lang, makeSeparateFolder, translateInPlace } = await getTranslationSettings();
if (!token) {
dialogTokenMissing();
} else {
// TODO Not every system has the "description" property on system item

if (documentToTranslate.system.description) {
if (!translateInPlace) {
await this.translateAndCreateItem(documentToTranslate, token, target_lang, makeSeparateFolder);
} else {
await this.translateAndReplaceOriginal(documentToTranslate, token, target_lang);
}
} else {
// DO NOTHING
console.warn(`Nothing to translate on the item ${documentToTranslate.name}`);
}
}
}

/**
*
* @param {Item} documentToTranslate
* @param {string} token
* @param {string} target_lang
* @returns {Promise<void>}
*/
async translateAndReplaceOriginal(documentToTranslate, token, target_lang) {
const newDescriptionText = await translate_html(
documentToTranslate.system.description.value,
token,
target_lang,
).catch((e) => {
new ErrorDialog(e.message);
});
documentToTranslate.update({
system: { description: { value: newDescriptionText } },
});
}

/**
*
* @param {Item} documentToTranslate
* @param {string} token
* @param {string} target_lang
* @param {boolean} makeSeparateFolder
* @returns {Promise<void>}
*/
async translateAndCreateItem(documentToTranslate, token, target_lang, makeSeparateFolder) {
let newName = await determineNewName(documentToTranslate);
const newDescriptionText = await translate_html(
documentToTranslate.system.description.value,
token,
target_lang,
).catch((e) => {
new ErrorDialog(e.message);
});
if (!newDescriptionText) {
}
const newFolder = await determineFolder(documentToTranslate, target_lang, makeSeparateFolder);
const newItems = await Item.createDocuments([
{
...documentToTranslate,
name: newName,
folder: newFolder,
type: documentToTranslate.type,
},
]);
if (!newItems || newItems.length <= 0) {
//
}

// await newItems[0].update({
// system: {
// description: {
// value:
// }newDescriptionText
// }
// });
await newItems[0].update({
system: { description: { value: newDescriptionText } },
});
}
}

export class ComprehendLanguagesTranslator {
/**
*
Expand All @@ -248,32 +32,3 @@ export class ComprehendLanguagesTranslator {
translator.translateButton(item);
}
}

export class SelectionTranslator {
/**
*
* @returns {Promise<void>}
*/
static async translateSelectedText() {
const { token, target_lang, makeSeparateFolder } = await getTranslationSettings();
const selectedText = window.getSelection().toString();
const translatedText = await translate_html(selectedText, token, target_lang).catch((e) => {
new ErrorDialog(e.message);
});
if (!translatedText) {
return;
}
let d = new Dialog({
title: "Translation",
content: `<p>${translatedText}</p>`,
buttons: {
one: {
icon: '<i class="fas fa-check"></i>',
label: "Close Translation",
},
},
default: "one",
});
d.render(true);
}
}
28 changes: 28 additions & 0 deletions src/scripts/SelectionTranslator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export class SelectionTranslator {
/**
*
* @returns {Promise<void>}
*/
static async translateSelectedText() {
const { token, target_lang, makeSeparateFolder } = await getTranslationSettings();
const selectedText = window.getSelection().toString();
const translatedText = await translate_html(selectedText, token, target_lang).catch((e) => {
new ErrorDialog(e.message);
});
if (!translatedText) {
return;
}
let d = new Dialog({
title: "Translation",
content: `<p>${translatedText}</p>`,
buttons: {
one: {
icon: '<i class="fas fa-check"></i>',
label: "Close Translation",
},
},
default: "one",
});
d.render(true);
}
}
20 changes: 10 additions & 10 deletions src/scripts/statics.js → src/scripts/constants.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
export class ComprehendLanguagesStatic {
static ID = "comprehend-languages";

static FLAGS = {
COMPREHENDLANGUAGES: "COMPREHENDLANGUAGES",
};

static SETTINGS = {
const CONSTANTS = {
MODULE_ID: "comprehend-languages",
PATH: `modules/comprehend-languages/`,
SETTINGS: {
DEEPL_TOKEN: "deepl-token",
TARGET_LANG: "target-language",
SUBSETTINGS_MENU: "subsetting-menu",
Expand All @@ -15,5 +11,9 @@ export class ComprehendLanguagesStatic {
TRANSLATE_JOURNAL_NAME: "translate-journal-name",
IN_PLACE: "in-place",
FORMALITY: "formality",
};
}
},
FLAGS: {
COMPREHENDLANGUAGES: "COMPREHENDLANGUAGES",
},
};
export default CONSTANTS;
29 changes: 10 additions & 19 deletions src/scripts/lib.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComprehendLanguagesTranslator } from "./ComprehendLanguagesTranslator";
import { ComprehendLanguages } from "./ComprehendLanguages";
import { ComprehendLanguagesStatic } from "./statics";
import { CONSTANTS } from "./constants";

export const addTranslateButton = async function (app) {
if (!game.user.isGM) {
Expand Down Expand Up @@ -143,10 +143,7 @@ export async function translate_html(long_html, token, target_lang) {
* @returns {Promise<string>} Translated text
*/
export async function translate_text(text, token, target_lang) {
const formality = await game.settings.get(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.FORMALITY,
);
const formality = await game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.FORMALITY);

let newText = text;
newText = replaceAll(newText, `@Scene[`, `@UUID[Scene.`);
Expand Down Expand Up @@ -191,16 +188,10 @@ export function replaceAll(string, search, replace) {
* @returns {Promise<{token: string;target_lang: string;makeSeparateFolder: boolean;translateInPlace: boolean;}>}
*/
export async function getTranslationSettings() {
const token = game.settings.get(ComprehendLanguagesStatic.ID, ComprehendLanguagesStatic.SETTINGS.DEEPL_TOKEN);
const target_lang = game.settings.get(ComprehendLanguagesStatic.ID, ComprehendLanguagesStatic.SETTINGS.TARGET_LANG);
const makeSeparateFolder = game.settings.get(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.SEPARATE_FOLDER,
);
const translateInPlace = game.settings.get(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.IN_PLACE,
);
const token = game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.DEEPL_TOKEN);
const target_lang = game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.TARGET_LANG);
const makeSeparateFolder = game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.SEPARATE_FOLDER);
const translateInPlace = game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.IN_PLACE);

return {
token: token,
Expand Down Expand Up @@ -240,11 +231,11 @@ export async function determineFolder(translatable, target_lang, makeSeparateFol
}
let oldFolderName = translatable.folder.name;
var newFolderName = "";
if (game.settings.get(ComprehendLanguagesStatic.ID, ComprehendLanguagesStatic.SETTINGS.TRANSLATE_FOLDER_NAME)) {
if (game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.TRANSLATE_FOLDER_NAME)) {
newFolderName = await translate_text(
oldFolderName,
game.settings.get(ComprehendLanguagesStatic.ID, ComprehendLanguagesStatic.SETTINGS.DEEPL_TOKEN),
game.settings.get(ComprehendLanguagesStatic.ID, ComprehendLanguagesStatic.SETTINGS.TARGET_LANG),
game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.DEEPL_TOKEN),
game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.TARGET_LANG),
);
} else {
newFolderName = target_lang + "_" + oldFolderName;
Expand Down Expand Up @@ -278,7 +269,7 @@ export async function determineFolder(translatable, target_lang, makeSeparateFol
export async function determineNewName(documentToTranslate) {
const { token, target_lang, makeSeparateFolder } = await getTranslationSettings();
let newName = "";
if (game.settings.get(ComprehendLanguagesStatic.ID, ComprehendLanguagesStatic.SETTINGS.TRANSLATE_JOURNAL_NAME)) {
if (game.settings.get(CONSTANTS.ID, CONSTANTS.SETTINGS.TRANSLATE_JOURNAL_NAME)) {
newName = await translate_text(documentToTranslate.name, token, target_lang);
} else {
if (documentToTranslate instanceof JournalEntryPage) {
Expand Down
Loading