ngx/translate - 只翻译字符串的一部分

ngx/translate - translate only part of a string

我需要使用 ngx/translate 翻译来自服务器的字符串。

当我需要翻译一个字符串时它工作正常,但在某些情况下我只需要翻译字符串的一部分。

例如: 'hello Shay' 或 'hello John'

我只需要翻译 'hello' 并保留原名。 我试过了:

this.i18n.instant('hello {Shay}')

并在 en.json 文件中: "hello {var}": "Hello {var}"

但没有成功。 有什么想法可以做到吗?

您正在寻找参数化翻译。您不需要翻译密钥中的该参数。像这样尝试:

en.json:

"hello_name": "Hello {{name}}"

控制器:

this.i18n.instant('hello_name', {name: 'Shay'});

或在模板中:

{{ 'hello_name' | translate: {name: 'Shay'} }}

https://github.com/ngx-translate/core#3-define-the-translations