0

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

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

在这个我使用 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>

在这一个中,我category.{{category}}在 ng-repeat 选项标签的选项中使用。

$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/ <- interpolation

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

4

2 回答 2

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>
于 2015-02-05T10:11:31.697 回答
0

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

return getttext.getString(input);

然后我像这样使用它:

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

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

于 2015-05-21T16:41:14.570 回答