From 2d8674c74b3876e49f98732549ae123456495917 Mon Sep 17 00:00:00 2001 From: Matheus Clemente Date: Tue, 16 Jan 2024 20:24:47 -0300 Subject: [PATCH] PF2e: integrate with system's languages manager --- src/module/hooks.js | 7 +++++ src/module/providers/pf2e.js | 52 ++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/module/hooks.js b/src/module/hooks.js index 564ef27..00039e7 100644 --- a/src/module/hooks.js +++ b/src/module/hooks.js @@ -250,6 +250,13 @@ export default class PolyglotHooks { } } + static async closeHomebrewElements(homebrewElements, html) { + if (game.system.id === "pf2e") { + await game.polyglot.languageProvider.getLanguages(); + await game.settings.set("polyglot", "Languages", game.polyglot.languageProvider.languages); + } + } + // Re-checks the user languages for the GM when activating another party on the Actors sidebar. static renderActorDirectoryPF2e(actors, html, data) { game.polyglot.updateUserLanguages(game.polyglot.chatElement); diff --git a/src/module/providers/pf2e.js b/src/module/providers/pf2e.js index 1f2814e..c0f9421 100644 --- a/src/module/providers/pf2e.js +++ b/src/module/providers/pf2e.js @@ -180,24 +180,42 @@ export default class pf2eLanguageProvider extends LanguageProvider { }; } - addLanguage(lang) { - if (!lang) return; - lang = lang.trim(); - let key = lang.toLowerCase().replace(/[\s']/g, "_"); - const homebrewLanguagesObj = game.settings.get("pf2e", "homebrew.languages"); - const homebrewLanguagesKeys = homebrewLanguagesObj.map((a) => a.id); - const homebrewLanguagesValues = homebrewLanguagesObj.map((a) => a.value); - if (homebrewLanguagesValues.includes(lang)) { - const index = homebrewLanguagesValues.indexOf(lang); - key = homebrewLanguagesKeys[index]; - } + async getLanguages() { const languagesSetting = game.settings.get("polyglot", "Languages"); - this.languages[key] = { - label: lang, - font: languagesSetting[key]?.font || this.languages[key]?.font || this.defaultFont, - rng: languagesSetting[key]?.rng ?? "default", - }; - this.addToConfig(key, lang); + const langs = {}; + const systemLanguages = foundry.utils.deepClone(CONFIG.PF2E.languages); + delete systemLanguages.common; + Object.keys(systemLanguages).forEach((key) => { + langs[key] = { + label: game.i18n.localize(`PF2E.Actor.Creature.Language.${key}`), + font: languagesSetting[key]?.font || this.languages[key]?.font || this.defaultFont, + rng: languagesSetting[key]?.rng ?? "default", + }; + }); + const customSystemLanguages = game.settings.get("pf2e", "homebrew.languages"); + customSystemLanguages.forEach((l) => { + const key = l.id; + langs[key] = { + label: l.value, + font: languagesSetting[key]?.font || this.languages[key]?.font || this.defaultFont, + rng: languagesSetting[key]?.rng ?? "default", + }; + }); + this.languages = langs; + } + + loadLanguages() {} + + addLanguage() {} + + removeLanguage() {} + + getSystemDefaultLanguage() { + return game.settings.get("pf2e", "homebrew.languageRarities").commonLanguage; + } + + getDefaultLanguage() { + this.defaultLanguage = this.getSystemDefaultLanguage(); } filterUsers(ownedActors) {