mirror of https://github.com/abpframework/abp.git
Browse Source
Introduces AsyncLocalizationPipe for asynchronous localization and updates modules to use it. LazyLocalizationPipe is now deprecated and extends AsyncLocalizationPipe for backward compatibility.pull/24076/head
5 changed files with 56 additions and 41 deletions
@ -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 {} |
|||
|
|||
@ -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<string> { |
|||
if (!key) { |
|||
return of(''); |
|||
} |
|||
|
|||
const flatParams = params.reduce<string[]>( |
|||
(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(), |
|||
); |
|||
} |
|||
} |
|||
@ -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<string> { |
|||
if (!key) { |
|||
return of(''); |
|||
} |
|||
|
|||
const flatParams = params.reduce<string[]>( |
|||
(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 {} |
|||
|
|||
Loading…
Reference in new issue