问题描述
我正在使用ng2-translate在angular 2中进行国际化.我有一个包含字符串的标签,如下所示:
I am working on internationalization in angular 2 using ng2-translate. I have a label which contains string as follows:
<label> Step {{stepNumber}} of {{totalSteps}} </label>
其中stepNumber和totalSteps是组件中的属性.
where stepNumber and totalSteps are properties in my component.
为了国际化,我将密钥存储在资源文件中,并与ng2-translate管道一起使用.
for internationalization I am storing a key in my resource files and utilizing with ng2-translate pipe.
en.json:
{
"KEY_STEP_COUNT": "Step {{stepNumber}} of {{totalSteps}}"
}
fr.json:
{
"KEY_STEP_COUNT": "Étape {{stepNumber}} sur {{totalSteps}}"
}
在我的html中利用它的方式如下:
an utilizing it in my html as follows:
<label> {{ 'KEY_STEP_COUNT' | translate}} </label>
我得到的输出是
en:
"Step {{stepNumber}} of {{totalSteps}}"
fr:
"Étape {{stepNumber}} sur {{totalSteps}}"
预期输出:
en:
"Step 1 of 11"
fr:
"Étape 1 sur 11"
是否可以在与angular2绑定的字符串中添加表达式?
Is is possible to add an expression in a string binding with angular2?
如果可能的话,我想使用angular2绑定而不是string.replace.
I want to use angular2 bindings and not string.replace if it is possible.
感谢您的帮助!
推荐答案
您必须将对象添加为管道参数,例如:
You have to add an object as a pipe parameter like :
{{ 'KEY_STEP_COUNT' | translate: {stepNumber: 15, totalSteps: 25} }}
这篇关于字符串中的Angular 2绑定表达式(ng2-translate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!