-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update i18n pipe on locale change (#512)
- Loading branch information
Showing
3 changed files
with
47 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@alauda/ui": patch | ||
--- | ||
|
||
fix: update i18n pipe on locale change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void>(); | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters