diff --git a/.changeset/hot-dodos-help.md b/.changeset/hot-dodos-help.md new file mode 100644 index 000000000..2e84ccd95 --- /dev/null +++ b/.changeset/hot-dodos-help.md @@ -0,0 +1,5 @@ +--- +"@alauda/ui": patch +--- + +fix: update i18n pipe on locale change diff --git a/src/i18n/i18n.pipe.ts b/src/i18n/i18n.pipe.ts index 8937e07d7..1d0e0f63d 100644 --- a/src/i18n/i18n.pipe.ts +++ b/src/i18n/i18n.pipe.ts @@ -1,14 +1,36 @@ -import { Pipe, PipeTransform } from '@angular/core'; +import { + ChangeDetectorRef, + OnDestroy, + Pipe, + PipeTransform, +} from '@angular/core'; +import { Subject, takeUntil } from 'rxjs'; import { I18nService } from './i18n.service'; import { StringMap } from './i18n.type'; @Pipe({ name: 'auiI18n', + pure: false, }) -export class I18nPipe implements PipeTransform { - constructor(private readonly i18n: I18nService) {} +export class I18nPipe implements PipeTransform, OnDestroy { + private readonly destroy$$ = new Subject(); + + constructor( + private readonly i18n: I18nService, + private readonly cdr: ChangeDetectorRef, + ) { + this.i18n.localeChange$ + .pipe(takeUntil(this.destroy$$)) + .subscribe(() => this.cdr.markForCheck()); + } + transform(value: any, data?: StringMap) { return this.i18n.translate(value, data); } + + ngOnDestroy(): void { + this.destroy$$.next(); + this.destroy$$.complete(); + } } diff --git a/stories/rangepicker/basic.component.ts b/stories/rangepicker/basic.component.ts index c329703b8..3eaaac9d5 100644 --- a/stories/rangepicker/basic.component.ts +++ b/stories/rangepicker/basic.component.ts @@ -1,8 +1,17 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; -import dayjs from 'dayjs'; +import { Dayjs } from 'dayjs'; + +import { I18nService, en, zh } from '@alauda/ui'; @Component({ template: ` +
+ Current Locale: {{ i18n.i18n.locale }} + +
+ +
+