Skip to content

Commit c7da283

Browse files
committed
simplify args
1 parent 0c6bef0 commit c7da283

File tree

3 files changed

+5
-57
lines changed

3 files changed

+5
-57
lines changed

src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.controller.test.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -312,31 +312,9 @@ describe('UmbLocalizeController', () => {
312312
expect(controller.string(str)).to.equal('close');
313313
});
314314

315-
it('should replace tokens in each key with the provided values', async () => {
315+
it('should replace tokens in each key with the provided args', async () => {
316316
const str = '#withInlineToken #withInlineTokenLegacy';
317-
expect(
318-
controller.string(str, { withInlineToken: ['value1', 'value2'], withInlineTokenLegacy: ['value3', 'value4'] }),
319-
).to.equal('value1 value2 value3 value4');
320-
});
321-
});
322-
323-
describe('getKeysFromString', () => {
324-
it('should return an empty array if the input is not a string', async () => {
325-
// @ts-expect-error
326-
expect(controller.getKeysFromString(123)).to.deep.equal([]);
327-
// @ts-expect-error
328-
expect(controller.getKeysFromString({})).to.deep.equal([]);
329-
// @ts-expect-error
330-
expect(controller.getKeysFromString(undefined)).to.deep.equal([]);
331-
});
332-
333-
it('should return an empty array if the input is an empty string', async () => {
334-
expect(controller.getKeysFromString('')).to.deep.equal([]);
335-
});
336-
337-
it('should return an array of keys', async () => {
338-
const str = '#close #logout';
339-
expect(controller.getKeysFromString(str)).to.deep.equal(['close', 'logout']);
317+
expect(controller.string(str, 'value1', 'value2')).to.equal('value1 value2 value1 value2');
340318
});
341319
});
342320

src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.controller.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ export class UmbLocalizationController<LocalizationSetType extends UmbLocalizati
191191
* If the term is found in the localization set, it will be replaced with the localized term.
192192
* If the term is not found, the original term will be returned.
193193
* @param {string} text The text to translate.
194-
* @param {Record<string, Array<string>>} argsMap An object where the keys are the terms to translate and the values are arrays of arguments to pass to the term function.
194+
* @param {...any} args The arguments to parse for this localization entry.
195195
* @returns {string} The translated text.
196196
*/
197-
string(text: unknown, argsMap?: Record<string, Array<string>>): string {
197+
string(text: unknown, ...args: any): string {
198198
if (typeof text !== 'string') {
199199
return '';
200200
}
@@ -204,7 +204,6 @@ export class UmbLocalizationController<LocalizationSetType extends UmbLocalizati
204204

205205
const localizedText = text.replace(regex, (match: string) => {
206206
const key = match.slice(1);
207-
const args = argsMap?.[key] || [];
208207

209208
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
210209
// @ts-ignore
@@ -215,19 +214,4 @@ export class UmbLocalizationController<LocalizationSetType extends UmbLocalizati
215214

216215
return localizedText;
217216
}
218-
219-
/**
220-
* Extracts all keys from a string that start with a `#` character.
221-
* @param {string} text The text to parse.
222-
* @returns {Array<string>} An array of keys.
223-
*/
224-
getKeysFromString(text: string): Array<string> {
225-
if (typeof text !== 'string') {
226-
return [];
227-
}
228-
229-
const regex = /#\w+/g;
230-
const keys = text.match(regex) || [];
231-
return keys.map((key) => key.slice(1));
232-
}
233217
}

src/Umbraco.Web.UI.Client/src/packages/core/entity-action/common/delete/delete.action.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import type { UmbDetailRepository, UmbItemRepository } from '@umbraco-cms/backof
77
import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action';
88
import { UmbLocalizationController } from '@umbraco-cms/backoffice/localization-api';
99

10-
type UmbLocalizeStringArgsMap = Record<string, Array<string>>;
11-
1210
export class UmbDeleteEntityAction extends UmbEntityActionBase<MetaEntityActionDeleteKind> {
1311
// TODO: make base type for item and detail models
1412
#localize = new UmbLocalizationController(this);
@@ -28,22 +26,10 @@ export class UmbDeleteEntityAction extends UmbEntityActionBase<MetaEntityActionD
2826
const headline = this.args.meta.confirm?.headline ?? '#actions_delete';
2927
const message = this.args.meta.confirm?.message ?? '#defaultdialogs_confirmdelete';
3028

31-
/* We need to replace the token in a translation with the item name.
32-
A string can potentially consist of multiple translation keys.
33-
First we find all the translation keys in message. */
34-
const keysFromMessageString = this.#localize.getKeysFromString(message);
35-
36-
// Second we create a map of the translation keys found in the message and the arguments that should replace the tokens.
37-
// This will enable the localize service to pass and replace the tokens in the all the translations with the item name.
38-
const argsMap: UmbLocalizeStringArgsMap = keysFromMessageString.reduce((acc: UmbLocalizeStringArgsMap, key) => {
39-
acc[key] = [item.name];
40-
return acc;
41-
}, {});
42-
4329
// TODO: handle items with variants
4430
await umbConfirmModal(this._host, {
4531
headline: this.#localize.string(headline),
46-
content: this.#localize.string(message, argsMap),
32+
content: this.#localize.string(message, item.name),
4733
color: 'danger',
4834
confirmLabel: '#general_delete',
4935
});

0 commit comments

Comments
 (0)