mirror of https://github.com/abpframework/abp.git
Browse Source
Add AsyncLocalizationPipe and deprecate LazyLocalizationPipe issue-22901pull/24214/head
committed by
GitHub
5 changed files with 56 additions and 41 deletions
@ -1,14 +1,14 @@ |
|||||
import { NgModule } from '@angular/core'; |
import { NgModule } from '@angular/core'; |
||||
import { LocalizationPipe } from './pipes/localization.pipe'; |
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. |
* @deprecated Use `LocalizationPipe`, `AsyncLocalizationPipe` and `LazyLocalizationPipe` directly as a standalone pipe. |
||||
* This module is no longer necessary for using the `LocalizationPipe` and `LazyLocalizationPipe` pipes. |
* This module is no longer necessary for using the `LocalizationPipe`, `AsyncLocalizationPipe` and `LazyLocalizationPipe` pipes. |
||||
*/ |
*/ |
||||
|
|
||||
@NgModule({ |
@NgModule({ |
||||
exports: [LocalizationPipe, LazyLocalizationPipe], |
exports: [LocalizationPipe, AsyncLocalizationPipe, LazyLocalizationPipe], |
||||
imports: [LocalizationPipe, LazyLocalizationPipe], |
imports: [LocalizationPipe, AsyncLocalizationPipe, LazyLocalizationPipe], |
||||
}) |
}) |
||||
export class LocalizationModule {} |
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 { Injectable, Pipe } from '@angular/core'; |
||||
import { |
import { AsyncLocalizationPipe } from './async-localization.pipe'; |
||||
Observable, |
|
||||
of, |
|
||||
filter, |
|
||||
take, |
|
||||
switchMap, |
|
||||
map, |
|
||||
startWith, |
|
||||
distinctUntilChanged, |
|
||||
} from 'rxjs'; |
|
||||
import { ConfigStateService, LocalizationService } from '../services'; |
|
||||
|
|
||||
|
/** |
||||
|
* @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() |
@Injectable() |
||||
@Pipe({ |
@Pipe({ |
||||
name: 'abpLazyLocalization', |
name: 'abpLazyLocalization', |
||||
}) |
}) |
||||
export class LazyLocalizationPipe implements PipeTransform { |
export class LazyLocalizationPipe extends AsyncLocalizationPipe {} |
||||
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(), |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
|
|||||
Loading…
Reference in new issue