From 010c3e9daa9148a4f68149de8a4416beefc70163 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Tue, 25 Jul 2023 15:49:44 +0200 Subject: [PATCH] super optimize localization context to avoid rxjs error handling --- .../libs/localization-api/localization.context.ts | 6 +++--- .../packages/core/localization/localize.element.ts | 13 ++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.ts b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.ts index 90cc7ce54f..d64da33eff 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/localization-api/localization.context.ts @@ -3,7 +3,7 @@ import { UMB_AUTH } from '@umbraco-cms/backoffice/auth'; import { UmbContextToken } from '@umbraco-cms/backoffice/context-api'; import { UmbElement } from '@umbraco-cms/backoffice/element-api'; import { UmbBackofficeExtensionRegistry } from '@umbraco-cms/backoffice/extension-registry'; -import { of, switchMap, throwError, type Observable } from '@umbraco-cms/backoffice/external/rxjs'; +import { of, switchMap, type Observable, map } from '@umbraco-cms/backoffice/external/rxjs'; export class UmbLocalizationContext { #translationRegistry; @@ -34,8 +34,8 @@ export class UmbLocalizationContext { */ localize(key: string, fallback?: string): Observable { return this.translations.pipe( - switchMap((dictionary) => { - return dictionary.get(key) ?? fallback ?? throwError(() => new Error(`Key not found: ${key}`)); + map((dictionary) => { + return dictionary.get(key) ?? fallback ?? ''; }) ); } diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/localization/localize.element.ts b/src/Umbraco.Web.UI.Client/src/packages/core/localization/localize.element.ts index 944a926e63..22d85d4d33 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/localization/localize.element.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/localization/localize.element.ts @@ -50,18 +50,17 @@ export class UmbLocalizeElement extends UmbLitElement { this.#subscription.destroy(); } - this.#subscription = this.observe(localizationContext!.localize(this.key), { - next: (value) => { + this.#subscription = this.observe(localizationContext!.localize(this.key), (value) => { + if (value) { (this.getHostElement() as HTMLElement).removeAttribute('data-umb-localize-error'); this.value = value; - }, - error: (error) => { - (this.getHostElement() as HTMLElement).setAttribute('data-umb-localize-error', (error as Error).message); + } else { + (this.getHostElement() as HTMLElement).setAttribute('data-umb-localize-error', `Key not found: ${this.key}`); + console.warn('Key not found:', this.key); if (this.debug) { - console.error('Failed to localize key:', this.key, error); this.value = this.key; } - }, + } }); }