Sha*_*hay 7 ngx-translate angular
我需要使用ngx/translate翻译来自服务器的字符串。
当我需要翻译字符串时它工作得很好,但在某些情况下我只需要翻译字符串的一部分。
例如:“你好谢伊”或“你好约翰”
我只需要翻译“你好”并保留名称不变。我试过:
this.i18n.instant('hello {Shay}')
Run Code Online (Sandbox Code Playgroud)
并在 en.json 文件中: "hello {var}": "Hello {var}"
但没有成功。有什么想法可以做到吗?
您正在寻找参数化翻译。您不需要翻译密钥中的该参数。试试这样:
en.json:
"hello_name": "Hello {{name}}"
Run Code Online (Sandbox Code Playgroud)
控制器:
this.i18n.instant('hello_name', {name: 'Shay'});
Run Code Online (Sandbox Code Playgroud)
或在模板中:
{{ 'hello_name' | translate: {name: 'Shay'} }}
Run Code Online (Sandbox Code Playgroud)
https://github.com/ngx-translate/core#3-define-the-translations
| 归档时间: |
|
| 查看次数: |
4947 次 |
| 最近记录: |