Skip to content

Commit cb21587

Browse files
Merge pull request #24076 from abpframework/issue-22901
Add AsyncLocalizationPipe and deprecate LazyLocalizationPipe issue-22901
2 parents 1db5697 + 9e40244 commit cb21587

File tree

5 files changed

+56
-41
lines changed

5 files changed

+56
-41
lines changed

npm/ng-packs/packages/core/src/lib/core.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { ABP } from './models/common';
2929
import './utils/date-extensions';
3030
import { provideAbpCoreChild, provideAbpCore, withOptions } from './providers';
3131
import {
32+
AsyncLocalizationPipe,
3233
LazyLocalizationPipe,
3334
UtcToLocalPipe,
3435
SafeHtmlPipe,
@@ -60,6 +61,7 @@ const CORE_PIPES = [
6061
ShortDatePipe,
6162
ToInjectorPipe,
6263
UtcToLocalPipe,
64+
AsyncLocalizationPipe,
6365
LazyLocalizationPipe,
6466
];
6567

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { LocalizationPipe } from './pipes/localization.pipe';
3-
import { LazyLocalizationPipe } from './pipes';
3+
import { AsyncLocalizationPipe, LazyLocalizationPipe } from './pipes';
44

55
/**
6-
* @deprecated Use `LocalizationPipe` and `LazyLocalizationPipe` directly as a standalone pipe.
7-
* This module is no longer necessary for using the `LocalizationPipe` and `LazyLocalizationPipe` pipes.
6+
* @deprecated Use `LocalizationPipe`, `AsyncLocalizationPipe` and `LazyLocalizationPipe` directly as a standalone pipe.
7+
* This module is no longer necessary for using the `LocalizationPipe`, `AsyncLocalizationPipe` and `LazyLocalizationPipe` pipes.
88
*/
99

1010
@NgModule({
11-
exports: [LocalizationPipe, LazyLocalizationPipe],
12-
imports: [LocalizationPipe, LazyLocalizationPipe],
11+
exports: [LocalizationPipe, AsyncLocalizationPipe, LazyLocalizationPipe],
12+
imports: [LocalizationPipe, AsyncLocalizationPipe, LazyLocalizationPipe],
1313
})
1414
export class LocalizationModule {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { inject, Injectable, Pipe, PipeTransform } from '@angular/core';
2+
import {
3+
Observable,
4+
of,
5+
filter,
6+
take,
7+
switchMap,
8+
map,
9+
startWith,
10+
distinctUntilChanged,
11+
} from 'rxjs';
12+
import { ConfigStateService, LocalizationService } from '../services';
13+
14+
@Injectable()
15+
@Pipe({
16+
name: 'abpAsyncLocalization',
17+
})
18+
export class AsyncLocalizationPipe implements PipeTransform {
19+
private localizationService = inject(LocalizationService);
20+
private configStateService = inject(ConfigStateService);
21+
22+
transform(key: string, ...params: (string | string[])[]): Observable<string> {
23+
if (!key) {
24+
return of('');
25+
}
26+
27+
const flatParams = params.reduce<string[]>(
28+
(acc, val) => (Array.isArray(val) ? acc.concat(val) : [...acc, val]),
29+
[],
30+
);
31+
32+
return this.configStateService.getAll$().pipe(
33+
filter(config => !!config.localization),
34+
take(1),
35+
switchMap(() => this.localizationService.get(key, ...flatParams)),
36+
map(translation => (translation && translation !== key ? translation : '')),
37+
startWith(''),
38+
distinctUntilChanged(),
39+
);
40+
}
41+
}

npm/ng-packs/packages/core/src/lib/pipes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export * from './short-date.pipe';
66
export * from './short-time.pipe';
77
export * from './short-date-time.pipe';
88
export * from './utc-to-local.pipe';
9+
export * from './async-localization.pipe';
910
export * from './lazy-localization.pipe';
1011
export * from './html-encode.pipe';
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
1-
import { inject, Injectable, Pipe, PipeTransform } from '@angular/core';
2-
import {
3-
Observable,
4-
of,
5-
filter,
6-
take,
7-
switchMap,
8-
map,
9-
startWith,
10-
distinctUntilChanged,
11-
} from 'rxjs';
12-
import { ConfigStateService, LocalizationService } from '../services';
1+
import { Injectable, Pipe } from '@angular/core';
2+
import { AsyncLocalizationPipe } from './async-localization.pipe';
133

4+
/**
5+
* @deprecated Use `AsyncLocalizationPipe` instead. This pipe will be removed in a future version.
6+
* LazyLocalizationPipe has been renamed to AsyncLocalizationPipe to better express its async nature.
7+
*/
148
@Injectable()
159
@Pipe({
1610
name: 'abpLazyLocalization',
1711
})
18-
export class LazyLocalizationPipe implements PipeTransform {
19-
private localizationService = inject(LocalizationService);
20-
private configStateService = inject(ConfigStateService);
21-
22-
transform(key: string, ...params: (string | string[])[]): Observable<string> {
23-
if (!key) {
24-
return of('');
25-
}
26-
27-
const flatParams = params.reduce<string[]>(
28-
(acc, val) => (Array.isArray(val) ? acc.concat(val) : [...acc, val]),
29-
[],
30-
);
31-
32-
return this.configStateService.getAll$().pipe(
33-
filter(config => !!config.localization),
34-
take(1),
35-
switchMap(() => this.localizationService.get(key, ...flatParams)),
36-
map(translation => (translation && translation !== key ? translation : '')),
37-
startWith(''),
38-
distinctUntilChanged(),
39-
);
40-
}
41-
}
12+
export class LazyLocalizationPipe extends AsyncLocalizationPipe {}

0 commit comments

Comments
 (0)