Skip to content

Commit 457e4e5

Browse files
authored
fix: update i18n pipe on locale change (#512)
1 parent 9d04180 commit 457e4e5

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

.changeset/hot-dodos-help.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alauda/ui": patch
3+
---
4+
5+
fix: update i18n pipe on locale change

src/i18n/i18n.pipe.ts

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
import { Pipe, PipeTransform } from '@angular/core';
1+
import {
2+
ChangeDetectorRef,
3+
OnDestroy,
4+
Pipe,
5+
PipeTransform,
6+
} from '@angular/core';
7+
import { Subject, takeUntil } from 'rxjs';
28

39
import { I18nService } from './i18n.service';
410
import { StringMap } from './i18n.type';
511

612
@Pipe({
713
name: 'auiI18n',
14+
pure: false,
815
})
9-
export class I18nPipe implements PipeTransform {
10-
constructor(private readonly i18n: I18nService) {}
16+
export class I18nPipe implements PipeTransform, OnDestroy {
17+
private readonly destroy$$ = new Subject<void>();
18+
19+
constructor(
20+
private readonly i18n: I18nService,
21+
private readonly cdr: ChangeDetectorRef,
22+
) {
23+
this.i18n.localeChange$
24+
.pipe(takeUntil(this.destroy$$))
25+
.subscribe(() => this.cdr.markForCheck());
26+
}
27+
1128
transform(value: any, data?: StringMap) {
1229
return this.i18n.translate(value, data);
1330
}
31+
32+
ngOnDestroy(): void {
33+
this.destroy$$.next();
34+
this.destroy$$.complete();
35+
}
1436
}

stories/rangepicker/basic.component.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
2-
import dayjs from 'dayjs';
2+
import { Dayjs } from 'dayjs';
3+
4+
import { I18nService, en, zh } from '@alauda/ui';
35

46
@Component({
57
template: `
8+
<div>
9+
Current Locale: {{ i18n.i18n.locale }}
10+
<button (click)="changeLocale()">Toggle</button>
11+
</div>
12+
13+
<br />
14+
615
<aui-range-picker
716
[(ngModel)]="range"
817
required
@@ -14,5 +23,11 @@ import dayjs from 'dayjs';
1423
changeDetection: ChangeDetectionStrategy.OnPush,
1524
})
1625
export class RangeBasicComponent {
17-
range = [dayjs(), dayjs()];
26+
range: [Dayjs, Dayjs] = null;
27+
28+
constructor(public i18n: I18nService) {}
29+
30+
changeLocale() {
31+
this.i18n.setI18n(this.i18n.i18n.locale === 'en' ? zh : en);
32+
}
1833
}

0 commit comments

Comments
 (0)