ngx/translate-仅翻译字符串的一部分 [英] ngx/translate - translate only part of a string
问题描述
我需要使用 ngx/translate 翻译来自服务器的字符串.
I need to translate strings coming from a server using ngx/translate.
当我需要翻译字符串但在某些情况下我只需要翻译字符串的一部分时,效果很好.
It works fine when i need to translate a string but in some case i need to translate only part of the string.
例如: 'hello Shay'或'hello John'
For example: 'hello Shay' or 'hello John'
我只需要翻译"hello",并保持原样. 我试过了:
And i need to translate only 'hello' and leave the name as is. I tried:
this.i18n.instant('hello {Shay}')
并在en.json文件中: "hello {var}":"Hello {var}"
and in the en.json file: "hello {var}": "Hello {var}"
,但没有成功. 有什么想法可以做到吗?
but with no success. Any Ideas how it can be done?
推荐答案
您正在寻找参数化的翻译.您不需要转换键中的该参数.像这样尝试:
You are looking for parametrized translations. You do not need that parameter in the translation key. Try it like this:
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-定义翻译
这篇关于ngx/translate-仅翻译字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!