Search code examples
angularangular2-pipeangular-i18n

Angular2 i18n pipe transform in .ts file


I want to translate messages in my angular2 material snackbars. Problem is to show snackbar I need to pass message as parameter. To translate using i18n: https://angular.io/api/common/I18nSelectPipe . I can't use interpolation as parameter. So how I can transform my message in snackbar?

<button md-button (click)="openSnackBar(message, action)">Show snack-bar</button>

https://plnkr.co/edit/H6rrayAUhBuCx3JOuDCF?p=preview


Solution

  • You can use it explicity by calling tranform method on new instance of pipe:

    import { I18nSelectPipe } from '@angular/common';
    
    const msg = new I18nSelectPipe().transform(message, this.inviteMap);
    

    Plunker Example