From f8364888f2238fce5a971d1accdb9d0166df4691 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sat, 27 Apr 2024 00:37:30 -0600 Subject: [PATCH] Fix issues with the localizer's replacement to be more reliable --- module/utils/localizer.mjs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/module/utils/localizer.mjs b/module/utils/localizer.mjs index 550c1cb..7cfebb0 100644 --- a/module/utils/localizer.mjs +++ b/module/utils/localizer.mjs @@ -18,12 +18,20 @@ export function localizer(key, args = {}, depth = 0) { 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; - localized = - localized.slice(0, match.index) - + localizer(subkey, args, depth + 1) - + localized.slice(match.index + subkey.length + 1) + if (localizedSubkeys.has(subkey)) continue; + localizedSubkeys.set(subkey, localizer(subkey, args, depth + 1)); }; - return localized; + + return localized.replace( + localizerConfig.subKeyPattern, + (_fullMatch, subkey) => { + return localizedSubkeys.get(subkey); + } + ); };