diff --git a/npm/ng-packs/packages/core/src/lib/models/config.ts b/npm/ng-packs/packages/core/src/lib/models/config.ts index 8238864b2f..e6c9a05f35 100644 --- a/npm/ng-packs/packages/core/src/lib/models/config.ts +++ b/npm/ng-packs/packages/core/src/lib/models/config.ts @@ -30,4 +30,9 @@ export namespace Config { export interface Requirements { layouts: Type[]; } + + export interface LocalizationWithDefault { + key: string; + defaultValue: string; + } } diff --git a/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts b/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts index 2050e742cf..b18a12946f 100644 --- a/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts +++ b/npm/ng-packs/packages/core/src/lib/pipes/localization.pipe.ts @@ -1,46 +1,20 @@ -import { Pipe, PipeTransform, OnDestroy } from '@angular/core'; +import { Pipe, PipeTransform } from '@angular/core'; import { Store } from '@ngxs/store'; +import { Config } from '../models'; import { ConfigState } from '../states'; -import { takeUntilDestroy } from '../utils'; -import { distinctUntilChanged, takeUntil } from 'rxjs/operators'; -import { Subject } from 'rxjs'; @Pipe({ name: 'abpLocalization', - // tslint:disable-next-line: no-pipe-impure - pure: false // required to update the value }) -export class LocalizationPipe implements PipeTransform, OnDestroy { - initialValue = ''; - - value: string; - - destroy$ = new Subject(); - +export class LocalizationPipe implements PipeTransform { constructor(private store: Store) {} - transform(value: string = '', ...interpolateParams: string[]): string { - if (this.initialValue !== value) { - this.initialValue = value; - this.destroy$.next(); - - this.store - .select( - ConfigState.getCopy( - value, - ...interpolateParams.reduce((acc, val) => (Array.isArray(val) ? [...acc, ...val] : [...acc, val]), []) - ) - ) - .pipe( - takeUntil(this.destroy$), - takeUntilDestroy(this), - distinctUntilChanged() - ) - .subscribe(copy => (this.value = copy)); - } - - return this.value; + transform(value: string | Config.LocalizationWithDefault = '', ...interpolateParams: string[]): string { + return this.store.selectSnapshot( + ConfigState.getLocalization( + value, + ...interpolateParams.reduce((acc, val) => (Array.isArray(val) ? [...acc, ...val] : [...acc, val]), []), + ), + ); } - - ngOnDestroy() {} } diff --git a/npm/ng-packs/packages/core/src/lib/states/config.state.ts b/npm/ng-packs/packages/core/src/lib/states/config.state.ts index 1dcc5d8512..f454be2872 100644 --- a/npm/ng-packs/packages/core/src/lib/states/config.state.ts +++ b/npm/ng-packs/packages/core/src/lib/states/config.state.ts @@ -115,8 +115,7 @@ export class ConfigState { /** * - * @param deprecated, Use getLocalization instead. To be delete in v1 - * + * @deprecated, Use getLocalization instead. To be delete in v1 */ static getCopy(key: string, ...interpolateParams: string[]) { if (!key) key = ''; @@ -166,14 +165,21 @@ export class ConfigState { return selector; } - static getLocalization(key: string, ...interpolateParams: string[]) { + static getLocalization(key: string | Config.LocalizationWithDefault, ...interpolateParams: string[]) { + let defaultValue: string; + + if (typeof key !== 'string') { + defaultValue = key.defaultValue; + key = key.key; + } + if (!key) key = ''; const keys = key.split('::') as string[]; const selector = createSelector( [ConfigState], (state: Config.State) => { - if (!state.localization) return key; + if (!state.localization) return defaultValue || key; const { defaultResourceName } = state.environment.localization; if (keys[0] === '') { @@ -207,7 +213,7 @@ export class ConfigState { }); } - return localization || key; + return localization || defaultValue || key; }, ); diff --git a/npm/ng-packs/packages/core/src/lib/states/session.state.ts b/npm/ng-packs/packages/core/src/lib/states/session.state.ts index 0754b0d62b..544bd42b2b 100644 --- a/npm/ng-packs/packages/core/src/lib/states/session.state.ts +++ b/npm/ng-packs/packages/core/src/lib/states/session.state.ts @@ -4,6 +4,7 @@ import { ABP, Session } from '../models'; import { GetAppConfiguration } from '../actions/config.actions'; import { LocalizationService } from '../services/localization.service'; import { from, combineLatest } from 'rxjs'; +import { switchMap } from 'rxjs/operators'; @State({ name: 'SessionState', @@ -28,7 +29,9 @@ export class SessionState { language: payload, }); - return combineLatest([dispatch(new GetAppConfiguration()), from(this.localizationService.registerLocale(payload))]); + return dispatch(new GetAppConfiguration()).pipe( + switchMap(() => from(this.localizationService.registerLocale(payload))), + ); } @Action(SetTenant) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.html b/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.html index a3053fe6ba..8954910674 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.html +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.html @@ -1,5 +1,5 @@ diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts b/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts index 824fe683a7..b86eb78c6b 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts @@ -1,14 +1,15 @@ import { Component, Renderer2, ElementRef } from '@angular/core'; +import { Config } from '@abp/ng.core'; @Component({ selector: 'abp-error', templateUrl: './error.component.html', - styleUrls: ['error.component.scss'] + styleUrls: ['error.component.scss'], }) export class ErrorComponent { - title = 'Oops!'; + title: string | Config.LocalizationWithDefault = 'Oops!'; - details = 'Sorry, an error has occured.'; + details: string | Config.LocalizationWithDefault = 'Sorry, an error has occured.'; renderer: Renderer2; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts index 7e4c11d666..18efbc2b60 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts @@ -36,11 +36,11 @@ export const DEFAULT_ERROR_MESSAGES = { }, defaultError500: { title: '500', - details: 'AbpAccount::InternalServerErrorMessage', + details: { key: 'AbpAccount::InternalServerErrorMessage', defaultValue: 'Error detail not sent by server.' }, }, defaultErrorUnknown: { title: 'Unknown Error', - details: 'AbpAccount::InternalServerErrorMessage', + details: { key: 'AbpAccount::InternalServerErrorMessage', defaultValue: 'Error detail not sent by server.' }, }, };