super optimize localization context to avoid rxjs error handling
This commit is contained in:
@@ -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 ?? '';
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user