如何使用 Angular 翻译来翻译具有 angular 绑定的字符串?
How to use Angular Translate to translate a string that has angular bindings with it?
我需要使用 Angular 翻译来为我正在开发的应用提供本地化服务。
我有这样的东西:
<p class="md-caption" translate="vm_stats_score"></p>
现在,我想将其分配给 Angular 翻译需要的 ID,但每当我这样做时
$translateProvider.translations('en', {
vm_stats_score: 'Today from {{::vm.stats.votes}} votes'
});
它不包括我要绑定到它的值。
正确的做法是什么?
不能直接绑定,还有一种方法:
在您的翻译文件中为输入创建变量,例如:
{
"vm_stats_score": 'Today from {{votes}} votes'
}
在您的 html
中传递值,例如:
<p class="md-caption" translate="vm_stats_score" translate-values="{ votes: vm.stats.votes"></p>
根据this,您可以执行以下操作:
<p class="md-caption" translate="vm_stats_score" translate-compile translate-values="{'votes': vm.stats.votes}"></p>
这会将您通过 translate-values
传递给 <p>
的值相加。
如您在示例中所见,我无法使其与一次性绑定一起使用。这意味着您必须将字符串定义更改为:
$translateProvider.translations('en', {
vm_stats_score: 'Today from {{vm.stats.votes}} votes'
});
我需要使用 Angular 翻译来为我正在开发的应用提供本地化服务。
我有这样的东西:
<p class="md-caption" translate="vm_stats_score"></p>
现在,我想将其分配给 Angular 翻译需要的 ID,但每当我这样做时
$translateProvider.translations('en', {
vm_stats_score: 'Today from {{::vm.stats.votes}} votes'
});
它不包括我要绑定到它的值。
正确的做法是什么?
不能直接绑定,还有一种方法:
在您的翻译文件中为输入创建变量,例如:
{
"vm_stats_score": 'Today from {{votes}} votes'
}
在您的 html
中传递值,例如:
<p class="md-caption" translate="vm_stats_score" translate-values="{ votes: vm.stats.votes"></p>
根据this,您可以执行以下操作:
<p class="md-caption" translate="vm_stats_score" translate-compile translate-values="{'votes': vm.stats.votes}"></p>
这会将您通过 translate-values
传递给 <p>
的值相加。
如您在示例中所见,我无法使其与一次性绑定一起使用。这意味着您必须将字符串定义更改为:
$translateProvider.translations('en', {
vm_stats_score: 'Today from {{vm.stats.votes}} votes'
});