Skip to content

Instantly share code, notes, and snippets.

@FFKL
Created February 11, 2021 08:32
Show Gist options
  • Save FFKL/9a4000fca3a90744ee32f31bf3721719 to your computer and use it in GitHub Desktop.
Save FFKL/9a4000fca3a90744ee32f31bf3721719 to your computer and use it in GitHub Desktop.
export interface MultiLanguage {}
export type Constructor<T> = new(...args: any[]) => T;
export type MultiLanguageCtor = Constructor<MultiLanguage>;
export interface TranslationAvailable extends PipeTransform, OnDestroy {
translate: TranslateService;
cd: ChangeDetectorRef;
}
export function multiLanguageMixin<T extends Constructor<TranslationAvailable>>(base: T): MultiLanguageCtor & T {
return class extends base implements PipeTransform, OnDestroy {
private onLangChange?: Subscription;
constructor(...args: any[]) { super(...args); }
transform(value: any, ...args: any[]): any {
if (!this.onLangChange) {
this.onLangChange = this.translate.onLangChange.subscribe(() => this.cd.markForCheck());
}
return super.transform(value, ...args);
}
ngOnDestroy(): void {
super.ngOnDestroy();
this.onLangChange?.unsubscribe();
this.onLangChange = undefined;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment