Implement the localizer helper API
This commit is contained in:
parent
b428eb3bf6
commit
6997c736dc
2 changed files with 34 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ import { QueryStatus } from "./apps/QueryStatus.mjs";
|
|||
// Utils
|
||||
import { attributeSorter } from "./utils/attributeSort.mjs";
|
||||
import { DialogManager } from "./utils/DialogManager.mjs";
|
||||
import { localizer } from "./utils/localizer.mjs";
|
||||
import { QueryManager } from "./utils/QueryManager.mjs";
|
||||
import { toID } from "./utils/toID.mjs";
|
||||
|
||||
|
|
@ -27,6 +28,7 @@ Object.defineProperty(
|
|||
},
|
||||
utils: {
|
||||
attributeSorter,
|
||||
localizer,
|
||||
toID,
|
||||
},
|
||||
}),
|
||||
|
|
|
|||
32
module/utils/localizer.mjs
Normal file
32
module/utils/localizer.mjs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
const config = Object.preventExtensions({
|
||||
subKeyPattern: /@(?<key>[a-zA-Z.]+)/gm,
|
||||
maxDepth: 10,
|
||||
});
|
||||
|
||||
export function localizer(key, args = {}, depth = 0) {
|
||||
/** @type {string} */
|
||||
let localized = game.i18n.format(key, args);
|
||||
const subkeys = localized.matchAll(config.subKeyPattern);
|
||||
|
||||
// Short-cut to help prevent infinite recursion
|
||||
if (depth > config.maxDepth) {
|
||||
return localized;
|
||||
};
|
||||
|
||||
/*
|
||||
Helps prevent recursion on the same key so that we aren't doing excess work.
|
||||
*/
|
||||
const localizedSubkeys = new Map();
|
||||
for (const match of subkeys) {
|
||||
const subkey = match.groups.key;
|
||||
if (localizedSubkeys.has(subkey)) { continue };
|
||||
localizedSubkeys.set(subkey, localizer(subkey, args, depth + 1));
|
||||
};
|
||||
|
||||
return localized.replace(
|
||||
config.subKeyPattern,
|
||||
(_fullMatch, subkey) => {
|
||||
return localizedSubkeys.get(subkey);
|
||||
},
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue