From 25ca146eee9e41f793e6a32dcf5db933a9d8a4be Mon Sep 17 00:00:00 2001 From: Mads Rasmussen Date: Thu, 30 Jan 2025 22:36:39 +0100 Subject: [PATCH] handle strings with args in localize controller --- .../localization.controller.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.controller.ts b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.controller.ts index 1c92265e91..5d14b7eb2d 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.controller.ts @@ -191,9 +191,10 @@ export class UmbLocalizationController>} argsMap An object where the keys are the terms to translate and the values are arrays of arguments to pass to the term function. * @returns {string} The translated text. */ - string(text: unknown): string { + string(text: unknown, argsMap?: Record>): string { if (typeof text !== 'string') { return ''; } @@ -203,14 +204,26 @@ export class UmbLocalizationController { const key = match.slice(1); - // TODO: find solution to pass dynamic string to term + const args = argsMap?.[key] || []; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - const localized = this.term(key); + const localized = this.term(key, ...args); // we didn't find a localized string, so we return the original string with the # return localized === key ? match : localized; }); return localizedText; } + + /** + * Extracts all keys from a string that start with a `#` character. + * @param {string} text The text to parse. + * @returns {Array} An array of keys. + */ + getKeysFromString(text: string): Array { + const regex = /#\w+/g; + const keys = text.match(regex) || []; + return keys.map((key) => key.slice(1)); + } }