Implement utility function that recursively localizes based on the config data (implements #54)
This commit is contained in:
parent
34c7dbed87
commit
c1fa5b3d9b
3 changed files with 33 additions and 0 deletions
21
module/utils/localizer.mjs
Normal file
21
module/utils/localizer.mjs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { localizerConfig } from "../config.mjs";
|
||||
|
||||
export function localizer(key, args = {}, depth = 0) {
|
||||
/** @type {string} */
|
||||
let localized = game.i18n.format(key, args);
|
||||
const subkeys = localized.matchAll(localizerConfig.subKeyPattern);
|
||||
|
||||
// Short-cut to help prevent infinite recursion
|
||||
if (depth > localizerConfig.maxDepth) {
|
||||
return localized;
|
||||
};
|
||||
|
||||
for (const match of subkeys) {
|
||||
const subkey = match.groups.key;
|
||||
localized =
|
||||
localized.slice(0, match.index)
|
||||
+ localizer(subkey.slice(1), args, depth + 1)
|
||||
+ localized.slice(match.index + subkey.length)
|
||||
};
|
||||
return localized;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue