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 { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
|
||||||
import { UmbElement } from '@umbraco-cms/backoffice/element-api';
|
import { UmbElement } from '@umbraco-cms/backoffice/element-api';
|
||||||
import { UmbBackofficeExtensionRegistry } from '@umbraco-cms/backoffice/extension-registry';
|
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 {
|
export class UmbLocalizationContext {
|
||||||
#translationRegistry;
|
#translationRegistry;
|
||||||
@@ -34,8 +34,8 @@ export class UmbLocalizationContext {
|
|||||||
*/
|
*/
|
||||||
localize(key: string, fallback?: string): Observable<string> {
|
localize(key: string, fallback?: string): Observable<string> {
|
||||||
return this.translations.pipe(
|
return this.translations.pipe(
|
||||||
switchMap((dictionary) => {
|
map((dictionary) => {
|
||||||
return dictionary.get(key) ?? fallback ?? throwError(() => new Error(`Key not found: ${key}`));
|
return dictionary.get(key) ?? fallback ?? '';
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,18 +50,17 @@ export class UmbLocalizeElement extends UmbLitElement {
|
|||||||
this.#subscription.destroy();
|
this.#subscription.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#subscription = this.observe(localizationContext!.localize(this.key), {
|
this.#subscription = this.observe(localizationContext!.localize(this.key), (value) => {
|
||||||
next: (value) => {
|
if (value) {
|
||||||
(this.getHostElement() as HTMLElement).removeAttribute('data-umb-localize-error');
|
(this.getHostElement() as HTMLElement).removeAttribute('data-umb-localize-error');
|
||||||
this.value = value;
|
this.value = value;
|
||||||
},
|
} else {
|
||||||
error: (error) => {
|
(this.getHostElement() as HTMLElement).setAttribute('data-umb-localize-error', `Key not found: ${this.key}`);
|
||||||
(this.getHostElement() as HTMLElement).setAttribute('data-umb-localize-error', (error as Error).message);
|
console.warn('Key not found:', this.key);
|
||||||
if (this.debug) {
|
if (this.debug) {
|
||||||
console.error('Failed to localize key:', this.key, error);
|
|
||||||
this.value = this.key;
|
this.value = this.key;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user