如何在ts文件中对多个值进行角度平移并同时具有参数?

问题描述

this.snackBar.open(
            `Only files of size less than ${this.fileSizeAllowed}KB are allowed`,this.translate.instant('USER_REG.close'),{
              panelClass: 'errorSnackbar',duration: 5500,}
          );

我只希望将SnackBar中显示的消息(即“仅小于文件大小”)翻译成另一种语言,然后再翻译动态变量,然后再翻译“ KB允许”行。去做吧?完全不具备角度翻译功能

解决方法

也许这个答案可以帮助您:https://stackoverflow.com/a/45737562/13331446

您是否尝试过以下操作?

this.snackBar.open(
            this.translate.instant('FIRST.MESSAGE')+ ${this.fileSizeAllowed} + this.translate.instant('SECOND.MESSAGE'),this.translate.instant('USER_REG.close'),{
              panelClass: 'errorSnackbar',duration: 5500,}
          );
,

您可以从模板字符串文字中使用translate.instant方法。注意单词than后的空格。如果翻译服务删除了空格,则可以在第一个${}块之后立即添加空格。

this.snackBar.open(
`${this.translate.instant('Only files of size less than ')}${this.fileSizeAllowed}${this.translate.instant('KB are allowed')}`,{ panelClass: 'errorSnackbar',} 
);
,

您可以像下面的代码中那样简单地使用参数

    this.snackBar.open(
this.translateService.stream('MESSAGEKEY',{ dynamicVariable: this.fileSizeAllowed}).subscribe((res) => { return res; })
this.translate.instant('USER_REG.close'),{
                  panelClass: 'errorSnackbar',};
    );

,并且在您的资源文件中,该值为

'MESSAGEKEY' : "Only files of size less than" {{ dynamicVariable }} "KB are allowed"