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 40f0c48197c803d27e8a11037e8fe8b76a013a37
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Comprehend languages leverages the Deepl API to automatically translate Foundry
A bit of setup work is required before the module functions. You need to create a DeepL API Free account at [Deepl.com](https://www.deepl.com/pro#developer). The Free account should give you way more translated characters than you should need (500.000 characters/month). Unfortunately, a credit card (that will not be charged unless you upgrade to a Pro account) is required for the account creation process.
After setting up your account, go into your DeepL account settings and copy the "Authentication Key for DeepL API".

![DeepL Token](img/deepl-token-copy.png)
![DeepL Token](wiki/img/deepl-token-copy.png)

After enabling the module in your world, open the Module Settings and paste the API Key into the **DeepL Token** input field. Here you can also set your preferred target language.

![Module Settings](img/settings.png)
![Module Settings](wiki/img/settings.png)

Now you are good to go. When opening a Journal Entry or Item, a new button appears in the header only for the GM (**Translate**). Click on that button and after a few seconds (depending on the length of the text) a new JournalEntry or Item will be created which is called _xx_OldName_. XX is a two letter abbreviation for your target language. Optionally you can also enable a setting that saves all translated entries into their own folder.

![Example Translation](img/example-translation.png)
![Example Translation](wiki/img/example-translation.png)

The module retains HTML formatting.

Expand Down Expand Up @@ -127,12 +127,6 @@ Any issues, bugs, or feature requests are always welcome to be reported directly

## Licenses

This package is under an [GPL-3.0 license](LICENSE) and the [Foundry Virtual Tabletop Limited License Agreement for module development](https://foundryvtt.com/article/license/).
This package is under an [MIT](LICENSE) and the [Foundry Virtual Tabletop Limited License Agreement for module development](https://foundryvtt.com/article/license/).

## Credit

Thanks to anyone who helps me with this code! I appreciate the user community's feedback on this project!

* [data-toolbox](https://foundryvtt.com/packages/data-toolbox) For providing an easy method for importing items programmatically from a csv to a compendium
* [p4535992](https://github.com/p4535992) For adding Compatibility with Better Roll Tables, Requestor & Ongoing Support/Development
* [ctbritt](https://github.com/ctbritt) For adding Item Piles integration
## Credit
2 changes: 1 addition & 1 deletion src/languages/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"MODULE.hello": "hello"
}
}
14 changes: 3 additions & 11 deletions src/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { registerSettings } from "./scripts/settings.js";
import { initHooks, readyHooks, setupHooks } from "./scripts/raritycolors.js";
import CONSTANTS from "./scripts/constants.js";
import Logger from "./scripts/lib/Logger.js";

Expand All @@ -14,16 +13,10 @@ Hooks.once("init", () => {
// word = "activate";
// throw Logger.error(`Requires the 'libWrapper' module. Please ${word} it.`);
// }
if (!game.modules.get("colorsettings")?.active && game.user?.isGM) {
let word = "install and activate";
if (game.modules.get("colorsettings")) word = "activate";
throw Logger.error(`Requires the 'colorsettings' module. Please ${word} it.`);
}
// Register custom module settings
registerSettings();

initHooks();
// readyHooks();
// initHooks();
// Assign custom classes and constants here
// Register custom module settings
// registerSettings();
Expand All @@ -38,8 +31,7 @@ Hooks.once("init", () => {
Hooks.once("setup", () => {
// Do anything after initialization but before ready
// setupModules();
setupHooks();
// registerSettings();
// setupHooks();
});
/* ------------------------------------ */
/* When ready */
Expand All @@ -52,7 +44,7 @@ Hooks.once("ready", () => {
// }
// Do anything once the module is ready
// prepareConfigurations();
readyHooks();
// readyHooks();
});

/* ------------------------------------ */
Expand Down
76 changes: 35 additions & 41 deletions src/module.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
{
"id": "comprehend-languages",
"title": "Comprehend Languages",
"description": "Comprehend languages leverages the Deepl API to automatically translate Foundry Journal Entries & (some) item descriptions from English into a language of your choice. In the process, a new journal entry is created that contains the translated text.",
"version": "2.3.0",
"library": "false",
"compatibility":{
"minimum": "11",
"verified": "11.302"
},
"authors": [
{
"name": "Meneltamar",
"url": "https://github.com/meneltamar",
"discord": "Meneltamar#3331"
}
],
"conflicts": [

],
"esmodules": [
"module.js"
],
"styles": [
"/styles/comprehend-languages.css"
],
"languages": [
{
"lang": "en",
"name": "English",
"path": "languages/en.json"
}
],
"url": "https://github.com/meneltamar/fvtt-comprehend-languages",
"manifest": "https://github.com/meneltamar/fvtt-comprehend-languages/releases/latest/download/module.json",
"download": "https://github.com/meneltamar/fvtt-comprehend-languages/releases/download/v2.2.3/module.zip",
"media": [
{
"type": "icon",
"url": "https://avatars2.githubusercontent.com/u/71292812?s=400&u=ccdb4eeb7abf551ca8f314e5a9bfd0479a4d3d41&v=4"
}
]
"id": "comprehend-languages",
"title": "Comprehend Languages",
"description": "Comprehend languages leverages the Deepl API to automatically translate Foundry Journal Entries & (some) item descriptions from English into a language of your choice. In the process, a new journal entry is created that contains the translated text.",
"version": "2.3.0",
"library": "false",
"compatibility": {
"minimum": "11",
"verified": "11.302"
},
"authors": [
{
"name": "Meneltamar",
"url": "https://github.com/meneltamar",
"discord": "Meneltamar#3331"
}
],
"conflicts": [],
"esmodules": ["module.js"],
"styles": ["/styles/comprehend-languages.css"],
"languages": [
{
"lang": "en",
"name": "English",
"path": "languages/en.json"
}
],
"url": "https://github.com/meneltamar/fvtt-comprehend-languages",
"manifest": "https://github.com/meneltamar/fvtt-comprehend-languages/releases/latest/download/module.json",
"download": "https://github.com/meneltamar/fvtt-comprehend-languages/releases/download/v2.2.3/module.zip",
"media": [
{
"type": "icon",
"url": "https://avatars2.githubusercontent.com/u/71292812?s=400&u=ccdb4eeb7abf551ca8f314e5a9bfd0479a4d3d41&v=4"
}
]
}
217 changes: 9 additions & 208 deletions src/scripts/ComprehendLanguages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,214 +3,15 @@ import { SelectionTranslator } from "./ComprehendLanguagesTranslator";
import { addTranslateButton } from "./lib";
import { ComprehendLanguagesStatic } from "./statics";

declare const game: Game;
export class ComprehendLanguages {
// static ID = "comprehend-languages";

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

// static SETTINGS = {
// DEEPL_TOKEN: "deepl-token",
// TARGET_LANG: "target-language",
// SUBSETTINGS_MENU: "subsetting-menu",
// ICON_ONLY: "iconOnly",
// SEPARATE_FOLDER: "separate-folder",
// TRANSLATE_FOLDER_NAME: "translate-folder-name",
// TRANSLATE_JOURNAL_NAME: "translate-journal-name",
// };

static log(force, ...args) {
const shouldLog =
force ||
// @ts-ignore
game.modules.get("_dev-mode")?.api?.getPackageDebugValue(this.ID);

if (shouldLog) {
console.log(ComprehendLanguagesStatic.ID, "|", ...args);
}
}

static initialize() {
game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.DEEPL_TOKEN,
{
name: "DeepL Token",
config: true,
hint: "Insert your DeepL Token here",
type: String,
default: "",
scope: "world",
}
);

game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.TARGET_LANG,
{
name: "Target Language",
config: true,
hint: "What should your target language be",
type: String,
default: "DE",
choices: {
BG: "Bulgarian",
CS: "Czech",
DA: "Danish",
DE: "German",
EL: "Greek",
EN: "English",
ES: "Spanish",
ET: "Estonian",
FI: "Finnish",
FR: "French",
HU: "Hungarian",
IT: "Italian",
JA: "Japanese",
LT: "Lithuanian",
LV: "Latvian",
NL: "Dutch",
PL: "Polish",
PT: "Portuguese (all Portuguese varieties mixed)",
RO: "Romanian",
RU: "Russian",
SK: "Slovak",
SL: "Slovenian",
SV: "Swedish",
ZH: "Chinese",
},
scope: "world",
}
);

game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.FORMALITY,
{
name: "Formality",
config: true,
hint: "How formal should the translations be (if the language supports it)",
type: String,
default: "prefer_more",
choices: {
prefer_more: "Prefer more formal",
prefer_less: "Prefer less formal",
},
scope: "world",
}
);
game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.ICON_ONLY,
{
name: "Icon Only",
config: true,
hint: "If enabled the header button will show with only the icon and no text",
type: Boolean,
default: false,
scope: "world",
}
);
game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.IN_PLACE,
{
name: "Translate In Place (Overwriting the original)",
config: true,
hint: "If enabled the original document will be overwritten with the translated text. The following three settings will be ignored if this is enabled.",
type: Boolean,
default: false,
scope: "world",
}
);
game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.SEPARATE_FOLDER,
{
name: "Separate Folder",
config: true,
hint: "If enabled the translated documents & items will be put into a separate folder.",
type: Boolean,
default: false,
scope: "world",
}
);

game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.TRANSLATE_FOLDER_NAME,
{
name: "Translate Folder Name",
config: true,
hint: "If enabled together with the *Separate Folder* setting, the name of the folder will be translated as well.",
type: Boolean,
default: false,
scope: "world",
}
);

game.settings.register(
ComprehendLanguagesStatic.ID,
ComprehendLanguagesStatic.SETTINGS.TRANSLATE_JOURNAL_NAME,
{
name: "Translate Document Names",
config: true,
hint: "If enabled the names of Journals, Journal Pages and Items will be translated as well and the language prefix omitted.",
type: Boolean,
default: false,
scope: "world",
}
);

game.keybindings.register(
ComprehendLanguagesStatic.ID,
"translate-highlighted-text",
{
name: "Translate highlighted text",
hint: "Translate the currently selected piece of text and pop it out into a Dialog",
editable: [{ key: "KeyT", modifiers: ["Alt"] }],
onDown: () => {
SelectionTranslator.translateSelectedText();
return true;
},
}
);
// We replace the games window registry with a proxy object so we can intercept
// every new application window creation event.
const handler = {
ownKeys: (target) => {
return Reflect.ownKeys(target).filter((app: any) => {
const appId = parseInt(app);
if (!isNaN(appId)) {
// TODO DO SOMETHING ??
return false;
}
return true;
});
},
set: (
obj: Record<number, Application>,
prop: number,
value: FormApplication
) => {
const result = Reflect.set(obj, prop, value);
// console.log("Intercept ui-window create", value);
if (value && value.object) {
if (
value.object instanceof JournalEntry ||
value.object instanceof Item
) {
addTranslateButton(value).catch((err) => console.error(err));
}
static log(force, ...args) {
const shouldLog =
force ||
// @ts-ignore
game.modules.get("_dev-mode")?.api?.getPackageDebugValue(this.ID);

if (shouldLog) {
console.log(ComprehendLanguagesStatic.ID, "|", ...args);
}
return result;
},
};
//@ts-ignore
ui.windows = new Proxy(ui.windows, handler); // eslint-disable-line no-undef
//@ts-ignore
console.log("Installed window interceptor", ui.windows); // eslint-disable-line no-undef
}
}
}
Loading