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

问题描述 投票:0回答:2

当使用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中的链接中修复类似于此问题

javascript angular typescript ngx-translate
2个回答
0
投票

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

    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}`));
        }
      }

0
投票

更新:

由于消息来自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) 
© www.soinside.com 2019 - 2024. All rights reserved.