super optimize localization context to avoid rxjs error handling

This commit is contained in:
Jacob Overgaard
2023-07-25 15:49:44 +02:00
parent d3c372f68b
commit 010c3e9daa
2 changed files with 9 additions and 10 deletions

View File

@@ -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<string> {
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 ?? '';
})
);
}

View File

@@ -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;
}
},
}
});
}