File tree 3 files changed +47
-5
lines changed
3 files changed +47
-5
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @alauda/ui " : patch
3
+ ---
4
+
5
+ fix: update i18n pipe on locale change
Original file line number Diff line number Diff line change 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' ;
2
8
3
9
import { I18nService } from './i18n.service' ;
4
10
import { StringMap } from './i18n.type' ;
5
11
6
12
@Pipe ( {
7
13
name : 'auiI18n' ,
14
+ pure : false ,
8
15
} )
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
+
11
28
transform ( value : any , data ?: StringMap ) {
12
29
return this . i18n . translate ( value , data ) ;
13
30
}
31
+
32
+ ngOnDestroy ( ) : void {
33
+ this . destroy$$ . next ( ) ;
34
+ this . destroy$$ . complete ( ) ;
35
+ }
14
36
}
Original file line number Diff line number Diff line change 1
1
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' ;
3
5
4
6
@Component ( {
5
7
template : `
8
+ <div>
9
+ Current Locale: {{ i18n.i18n.locale }}
10
+ <button (click)="changeLocale()">Toggle</button>
11
+ </div>
12
+
13
+ <br />
14
+
6
15
<aui-range-picker
7
16
[(ngModel)]="range"
8
17
required
@@ -14,5 +23,11 @@ import dayjs from 'dayjs';
14
23
changeDetection : ChangeDetectionStrategy . OnPush ,
15
24
} )
16
25
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
+ }
18
33
}
You can’t perform that action at this time.
0 commit comments