如何使用ngx-translate转换alertController和mat-snackbar消息?

时间:2019-10-07 08:00:32

标签: angular typescript angular-material ionic4 ngx-translate

我正在创建ionic 4 angular app,并使用ngx-translate来翻译一种或多种语言。通过使用ngx-translate可以很容易地在html中翻译,但是如何在alertController和mat-snackbar消息中翻译文本消息

 //using below ngx-translate
 import { TranslateService } from '@ngx-translate/core';

 constructor(public translate:TranslateService){}

 //Alert Controller
 async alert_Massage() {
 let alertMassage = await this.alertController.create({
  message: "How are you ?",//how to translate this message.
  buttons: ['Ok'],
});
await alertMassage.present();
}

//mat-snackbar //how to translate this some error message 
this.snackBar.open('Some Error messages', 'Ok', {
        verticalPosition: 'top',
        duration: 2000,
        panelClass: ['red-snackbar']
});

1 个答案:

答案 0 :(得分:0)

您可以从服务中获得翻译:

const translations = await this.translate.get([ 'How are you?' ])}).toPromise();
let alertMassage = await this.alertController.create({
  message: translations['How are you?'].
  buttons: ['Ok'],
});
await alertMassage.present();