javascript - 如何使用 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中的链接中修复与此问题类似的问题
解决方案
更新:
由于消息来自 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)
推荐阅读
- javascript - 时间条件不自行执行,需要重新加载整个页面
- mysql - 使用生成的列创建表
- cron - 使用 SLURM 调度程序的条件 cron 作业
- python-3.x - 如何在python中运行的另一个程序上模拟python中的按键事件
- c# - 用指数计算生成的数学表达式,即 (1+2-3)*4^5 = answer
- javascript - 如何稍后在测试用例中保存和重用动态 DOM 内容
- bash - .sh 文件中的“output_dir="${1%/}" 是什么意思?
- bash - 通过将模式替换为另一个模式来递归地重命名存储库和文件名
- c# - 使用 AspNetCore.OData 进行分页 - 响应中缺少 @odata 属性
- windows - 与实际图像相比,JavaFX DragView Image 降低了不透明度......可以改变吗?