ds4/src/hooks/setup.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
// SPDX-FileCopyrightText: 2021 Siegfried Krug
//
// SPDX-License-Identifier: MIT
2021-03-16 08:22:27 +01:00
import { DS4 } from "../config";
2022-11-04 21:47:18 +01:00
import { getGame } from "../utils/utils";
2021-03-16 08:22:27 +01:00
export function registerForSetupHook() {
2023-07-10 22:23:13 +02:00
Hooks.once("setup", () => {
localizeAndSortConfigObjects();
});
2021-03-16 08:22:27 +01:00
}
/**
* Localizes all objects in {@link DS4.i18n} and sorts them unless they are explicitly excluded.
*/
function localizeAndSortConfigObjects() {
2023-07-10 22:23:13 +02:00
const noSort = [
"attributes",
"combatValues",
"cooldownDurations",
"creatureSizeCategories",
"spellGroups",
"traits",
"checkModifiers",
];
2021-03-16 08:22:27 +01:00
2023-07-10 22:23:13 +02:00
/**
* @template {Record<string, string>} T
* @param {T} obj The object to localize
* @param {boolean} [sort=true] whether or not to sort the object
* @returns {T} the localized object
*/
const localizeObject = (obj, sort = true) => {
const localized = Object.entries(obj).map(([key, value]) => {
return [key, getGame().i18n.localize(value)];
});
if (sort) localized.sort((a, b) => a[1].localeCompare(b[1]));
return Object.fromEntries(localized);
};
2021-03-16 08:22:27 +01:00
2023-07-10 22:23:13 +02:00
DS4.i18n = Object.fromEntries(
Object.entries(DS4.i18n).map(([key, value]) => {
return [key, localizeObject(value, !noSort.includes(key))];
}),
);
2021-03-16 08:22:27 +01:00
}