Fix translation fallbacks

This commit is contained in:
Zack Rauen 2021-10-22 16:34:48 -04:00
parent 58bfb6f51d
commit 631399991e
2 changed files with 30 additions and 1 deletions

View File

@ -32,7 +32,7 @@ export default new class LocaleManager {
newStrings = Locales[this.defaultLocale];
}
this.locale = newLocale;
Utilities.extend(this.strings, newStrings);
Utilities.extendTruthy(this.strings, newStrings);
Events.emit("strings-updated");
}
};

View File

@ -192,6 +192,35 @@ export default class Utilities {
return extendee;
}
/**
* Deep extends an object with a set of other objects. Objects later in the list
* of `extenders` have priority, that is to say if one sets a key to be a primitive,
* it will be overwritten with the next one with the same key. If it is an object,
* and the keys match, the object is extended. This happens recursively.
* @param {object} extendee - Object to be extended
* @param {...object} extenders - Objects to extend with
* @returns {object} - A reference to `extendee`
*/
static extendTruthy(extendee, ...extenders) {
for (let i = 0; i < extenders.length; i++) {
for (const key in extenders[i]) {
if (extenders[i].hasOwnProperty(key)) {
if (typeof extendee[key] === "object" && typeof extenders[i][key] === "object") {
this.extendTruthy(extendee[key], extenders[i][key]);
}
else if (typeof extenders[i][key] === "object") {
extendee[key] = {};
this.extendTruthy(extendee[key], extenders[i][key]);
}
else if (extenders[i][key]) {
extendee[key] = extenders[i][key];
}
}
}
}
return extendee;
}
/**
* Format strings with placeholders (`{{placeholder}}`) into full strings.
* Quick example: `PluginUtilities.formatString("Hello, {{user}}", {user: "Zerebos"})`