首页 > 解决方案 > 如何使用 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}`));
    }
  }

请帮我处理这个问题

我们需要在组件 https://github.com/ngx-translate/core/pull/990中的链接中修复与此问题类似的问题

标签: javascriptangulartypescriptngx-translate

解决方案


更新:

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

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

以前的答案:

ifmessage.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) 

推荐阅读