diff --git a/npm/ng-packs/packages/core/src/lib/core.module.ts b/npm/ng-packs/packages/core/src/lib/core.module.ts index 09607ae317f..249c5246c83 100644 --- a/npm/ng-packs/packages/core/src/lib/core.module.ts +++ b/npm/ng-packs/packages/core/src/lib/core.module.ts @@ -29,6 +29,7 @@ import { ABP } from './models/common'; import './utils/date-extensions'; import { provideAbpCoreChild, provideAbpCore, withOptions } from './providers'; import { + AsyncLocalizationPipe, LazyLocalizationPipe, UtcToLocalPipe, SafeHtmlPipe, @@ -60,6 +61,7 @@ const CORE_PIPES = [ ShortDatePipe, ToInjectorPipe, UtcToLocalPipe, + AsyncLocalizationPipe, LazyLocalizationPipe, ]; diff --git a/npm/ng-packs/packages/core/src/lib/localization.module.ts b/npm/ng-packs/packages/core/src/lib/localization.module.ts index 7a74e267838..a863e95296f 100644 --- a/npm/ng-packs/packages/core/src/lib/localization.module.ts +++ b/npm/ng-packs/packages/core/src/lib/localization.module.ts @@ -1,14 +1,14 @@ import { NgModule } from '@angular/core'; import { LocalizationPipe } from './pipes/localization.pipe'; -import { LazyLocalizationPipe } from './pipes'; +import { AsyncLocalizationPipe, LazyLocalizationPipe } from './pipes'; /** - * @deprecated Use `LocalizationPipe` and `LazyLocalizationPipe` directly as a standalone pipe. - * This module is no longer necessary for using the `LocalizationPipe` and `LazyLocalizationPipe` pipes. + * @deprecated Use `LocalizationPipe`, `AsyncLocalizationPipe` and `LazyLocalizationPipe` directly as a standalone pipe. + * This module is no longer necessary for using the `LocalizationPipe`, `AsyncLocalizationPipe` and `LazyLocalizationPipe` pipes. */ @NgModule({ - exports: [LocalizationPipe, LazyLocalizationPipe], - imports: [LocalizationPipe, LazyLocalizationPipe], + exports: [LocalizationPipe, AsyncLocalizationPipe, LazyLocalizationPipe], + imports: [LocalizationPipe, AsyncLocalizationPipe, LazyLocalizationPipe], }) export class LocalizationModule {} diff --git a/npm/ng-packs/packages/core/src/lib/pipes/async-localization.pipe.ts b/npm/ng-packs/packages/core/src/lib/pipes/async-localization.pipe.ts new file mode 100644 index 00000000000..0162d7c2472 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/pipes/async-localization.pipe.ts @@ -0,0 +1,41 @@ +import { inject, Injectable, Pipe, PipeTransform } from '@angular/core'; +import { + Observable, + of, + filter, + take, + switchMap, + map, + startWith, + distinctUntilChanged, +} from 'rxjs'; +import { ConfigStateService, LocalizationService } from '../services'; + +@Injectable() +@Pipe({ + name: 'abpAsyncLocalization', +}) +export class AsyncLocalizationPipe implements PipeTransform { + private localizationService = inject(LocalizationService); + private configStateService = inject(ConfigStateService); + + transform(key: string, ...params: (string | string[])[]): Observable { + if (!key) { + return of(''); + } + + const flatParams = params.reduce( + (acc, val) => (Array.isArray(val) ? acc.concat(val) : [...acc, val]), + [], + ); + + return this.configStateService.getAll$().pipe( + filter(config => !!config.localization), + take(1), + switchMap(() => this.localizationService.get(key, ...flatParams)), + map(translation => (translation && translation !== key ? translation : '')), + startWith(''), + distinctUntilChanged(), + ); + } +} diff --git a/npm/ng-packs/packages/core/src/lib/pipes/index.ts b/npm/ng-packs/packages/core/src/lib/pipes/index.ts index b7f945c6f54..0320350f55e 100644 --- a/npm/ng-packs/packages/core/src/lib/pipes/index.ts +++ b/npm/ng-packs/packages/core/src/lib/pipes/index.ts @@ -6,5 +6,6 @@ export * from './short-date.pipe'; export * from './short-time.pipe'; export * from './short-date-time.pipe'; export * from './utc-to-local.pipe'; +export * from './async-localization.pipe'; export * from './lazy-localization.pipe'; export * from './html-encode.pipe'; diff --git a/npm/ng-packs/packages/core/src/lib/pipes/lazy-localization.pipe.ts b/npm/ng-packs/packages/core/src/lib/pipes/lazy-localization.pipe.ts index b715b25ad86..9cccba088ed 100644 --- a/npm/ng-packs/packages/core/src/lib/pipes/lazy-localization.pipe.ts +++ b/npm/ng-packs/packages/core/src/lib/pipes/lazy-localization.pipe.ts @@ -1,41 +1,12 @@ -import { inject, Injectable, Pipe, PipeTransform } from '@angular/core'; -import { - Observable, - of, - filter, - take, - switchMap, - map, - startWith, - distinctUntilChanged, -} from 'rxjs'; -import { ConfigStateService, LocalizationService } from '../services'; +import { Injectable, Pipe } from '@angular/core'; +import { AsyncLocalizationPipe } from './async-localization.pipe'; +/** + * @deprecated Use `AsyncLocalizationPipe` instead. This pipe will be removed in a future version. + * LazyLocalizationPipe has been renamed to AsyncLocalizationPipe to better express its async nature. + */ @Injectable() @Pipe({ name: 'abpLazyLocalization', }) -export class LazyLocalizationPipe implements PipeTransform { - private localizationService = inject(LocalizationService); - private configStateService = inject(ConfigStateService); - - transform(key: string, ...params: (string | string[])[]): Observable { - if (!key) { - return of(''); - } - - const flatParams = params.reduce( - (acc, val) => (Array.isArray(val) ? acc.concat(val) : [...acc, val]), - [], - ); - - return this.configStateService.getAll$().pipe( - filter(config => !!config.localization), - take(1), - switchMap(() => this.localizationService.get(key, ...flatParams)), - map(translation => (translation && translation !== key ? translation : '')), - startWith(''), - distinctUntilChanged(), - ); - } -} +export class LazyLocalizationPipe extends AsyncLocalizationPipe {}