Skip to content

Commit 9971744

Browse files
authored
Merge pull request #22817 from abpframework/issue/refactorLazyLocalization
Angular - Refactor localization for basic and lepton themes
2 parents 8f9d85c + 8eebc69 commit 9971744

File tree

8 files changed

+47
-27
lines changed

8 files changed

+47
-27
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { NgModule } from '@angular/core';
22
import { LocalizationPipe } from './pipes/localization.pipe';
3+
import { LazyLocalizationPipe } from './pipes';
34

45
@NgModule({
5-
exports: [LocalizationPipe],
6+
imports: [LazyLocalizationPipe],
7+
exports: [LocalizationPipe, LazyLocalizationPipe],
68
declarations: [LocalizationPipe],
79
})
810
export class LocalizationModule {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './to-injector.pipe';
55
export * from './short-date.pipe';
66
export * from './short-time.pipe';
77
export * from './short-date-time.pipe';
8+
export * from './lazy-localization.pipe';
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: 'abpLazyLocalization',
17+
})
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+
}

npm/ng-packs/packages/theme-basic/src/lib/components/routes/routes.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@if (route.iconClass) {
1313
<i [ngClass]="route.iconClass"></i>
1414
}
15-
{{ route.name | abpLazyTranslate | async }}
15+
{{ route.name | abpLazyLocalization | async }}
1616
</a>
1717
</li>
1818
</ng-template>
@@ -39,7 +39,7 @@
3939
@if (route.iconClass) {
4040
<i [ngClass]="route.iconClass"></i>
4141
}
42-
{{ route.name | abpLazyTranslate | async }}
42+
{{ route.name | abpLazyLocalization | async }}
4343
</a>
4444
<div
4545
#routeContainer

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

npm/ng-packs/packages/theme-basic/src/lib/pipes/lazy-translate.pipe.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

npm/ng-packs/packages/theme-basic/src/lib/theme-basic.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { PageAlertContainerComponent } from './components/page-alert-container/p
1616
import { RoutesComponent } from './components/routes/routes.component';
1717
import { ValidationErrorComponent } from './components/validation-error/validation-error.component';
1818
import { provideThemeBasicConfig } from './providers';
19-
import { LazyTranslatePipe } from './pipes';
2019

2120
export const LAYOUTS = [ApplicationLayoutComponent, AccountLayoutComponent, EmptyLayoutComponent];
2221

@@ -49,7 +48,6 @@ export const LAYOUTS = [ApplicationLayoutComponent, AccountLayoutComponent, Empt
4948
NgbCollapseModule,
5049
NgbDropdownModule,
5150
NgxValidateCoreModule,
52-
LazyTranslatePipe,
5351
],
5452
})
5553
export class BaseThemeBasicModule {}

npm/ng-packs/packages/theme-basic/src/public-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export * from './lib/components';
66
export * from './lib/enums';
77
export * from './lib/handlers';
88
export * from './lib/models';
9-
export * from './lib/pipes';
109
export * from './lib/providers';
1110
export * from './lib/theme-basic.module';
1211
export * from './lib/tokens';

0 commit comments

Comments
 (0)