如何使用 Angular 5 翻译带有参数的通知消息?

How to translate notification message with parameter using Angular 5?

我无法使用 ngx-translate/core

翻译通知消息的参数

代码示例

en.json :
   "message": {
        "Successfully removed account": "Successfully removed account"
        }

以上为翻译json

constructor(private translate: TranslateService,
private userService : userService)

async deleteAccount(account) {
try {
  await this.userService.removeuserAccount(account.id);
  this.notificationService.sendSuccess(this.translate.instant(`message.Successfully removed account ${account.id}`));
} catch (err) {
  this.notificationService.sendError(this.translate.instant(`message.Unable to delete account: ${err.message}`));
    }
  }

请帮我处理这个问题

我们需要在组件的 link 中修复类似这个问题 https://github.com/ngx-translate/core/pull/990

更新:

由于消息来自JSON,您需要使用

this.translate.instant(message['Successfully removed account'] + account.id)

上一个答案:

如果message.Successfully也是像ENUM之类的组件变量,

在反引号 (`)

中使用 ${message.Successfully} removed account ${account.id} 作为方法的参数

如果不支持反引号且 message.Successfully 只是一个字符串,请使用

this.translate.instant("message.Successfully removed account " + account.id) 

如果 message.Successfully 不是字符串,如果它是变量,则使用

this.translate.instant(message.Successfully + "removed account " + account.id) 

试试这个答案。 翻译它并附加到变量。

    constructor(private translate: TranslateService,
    private userService : userService)

    async deleteAccount(account) {
    try {
      await this.userService.removeuserAccount(account.id);
      var status = this.translate.instant('message.Successfully  removed account');
this.notificationService.sendSuccess(this.translate.instant(status + `${account.id}`));
    } catch (err) {
var statuserr = this.translate.instant(`message.Unable to delete account:');
      this.notificationService.sendError(this.translate.instant(statuserr +` ${err.message}`));
        }
      }