Skip to content

Commit f906d28

Browse files
authored
Fix: #18272 (#18300)
* only localize if string * better types of localize string * test for string update
1 parent 5ee351a commit f906d28

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ describe('UmbLocalizeController', () => {
298298
});
299299

300300
it('should return an empty string if the input is not a string', async () => {
301-
expect(controller.string(123)).to.equal('');
302-
expect(controller.string({})).to.equal('');
301+
expect(controller.string(123 as any)).to.equal('');
302+
expect(controller.string({} as any)).to.equal('');
303303
expect(controller.string(undefined)).to.equal('');
304304
});
305305

@@ -338,5 +338,15 @@ describe('UmbLocalizeController', () => {
338338
await elementUpdated(element);
339339
expect(element.localize.term('close')).to.equal('Luk');
340340
});
341+
342+
it('should update the string when the language changes', async () => {
343+
expect(element.localize.string('testing #close')).to.equal('testing Close');
344+
345+
// Switch browser to Danish
346+
element.lang = danishRegional.$code;
347+
348+
await elementUpdated(element);
349+
expect(element.localize.string('testing #close')).to.equal('testing Luk');
350+
});
341351
});
342352
});

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ export class UmbLocalizationController<LocalizationSetType extends UmbLocalizati
190190
* Translates a string containing one or more terms. The terms should be prefixed with a `#` character.
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.
193-
* @param {string} text The text to translate.
193+
* @param {string | null | undefined} text The text to translate.
194194
* @param {...any} args The arguments to parse for this localization entry.
195195
* @returns {string} The translated text.
196196
*/
197-
string(text: unknown, ...args: any): string {
197+
string(text: string | null | undefined, ...args: any): string {
198198
if (typeof text !== 'string') {
199199
return '';
200200
}
@@ -204,6 +204,9 @@ export class UmbLocalizationController<LocalizationSetType extends UmbLocalizati
204204

205205
const localizedText = text.replace(regex, (match: string) => {
206206
const key = match.slice(1);
207+
if (!this.#usedKeys.includes(key)) {
208+
this.#usedKeys.push(key);
209+
}
207210

208211
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
209212
// @ts-ignore

src/Umbraco.Web.UI.Client/src/packages/core/modal/common/confirm/confirm-modal.element.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class UmbConfirmModalElement extends UmbLitElement {
2222
override render() {
2323
return html`
2424
<uui-dialog-layout class="uui-text" .headline=${this.localize.string(this.data?.headline) ?? null}>
25-
${unsafeHTML(this.localize.string(this.data?.content))}
25+
${typeof this.data?.content === 'string'
26+
? unsafeHTML(this.localize.string(this.data?.content))
27+
: this.data?.content}
2628
2729
<uui-button
2830
slot="actions"

0 commit comments

Comments
 (0)