angular-gettext:插入选择选项

时间:2015-01-26 12:33:32

标签: angularjs grunt-angular-gettext

我希望能够使用angular-gettext翻译角度生成下拉列表选项中的内容。

我有两种不同的解决方案,但都不起作用:

在这个中我使用ng-repeat并在js中使用textKeys:

$scope.categories = ['category.Art', 'category.Automotive'];

<select class="form-control" ng-model="category" >
    <option value="{{category}}" ng-repeat="category in categories" translate="">category.{{category}}</option>
</select>

在本文中,我在ng-repeat选项标记的选项中使用category.{{category}}

$scope.categories = ['Art', 'Automotive'];

<select class="form-control" ng-model="category" >
    <option value="{{category}}" ng-repeat="category in categories" translate="">category.{{category}}</option>
</select>

结果是显示了textKey本身,但没有显示翻译。如果我更改语言,则会显示[MISSING]。

根据angular-gettext,最后一个应该有效:https://angular-gettext.rocketeer.be/dev-guide/annotate/&lt; - interpolation

这可能吗?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:3)

我找到了答案:事先在javascript中翻译文本:

angular.module("myApp").controller("helloController", function (gettextCatalog) {
    var translated = gettextCatalog.getString("Hello");
});

或者只是这个:

<option value="{{category}}" ng-repeat="category in categories" translate="">{{category|translate}}</option>

答案 1 :(得分:0)

或者,您可以编写一个过滤器来转换值,但不会被angular-gettext-tools解析。我打电话给我的过滤器post-translate,我所做的就是:

return getttext.getString(input);

然后我像这样使用它:

<option value="{{category}}" ng-repeat="category in categories" translate="">{{category|postTranslate}}</option>

如果您要使用标准翻译过滤器,您将在po文件中获得文字字符串category,而不是内插值,这不是您想要的。