【问题标题】:angularjs/gettext: how to translate text in attributesangularjs/gettext:如何翻译属性中的文本
【发布时间】:2013-11-05 15:38:50
【问题描述】:

我有一段这样的html

<bar title="'My Title'"></bar>

现在,当我想翻译它时,它看起来像这样

<bar title="'My Title'|translate"></bar>

我没有 {{ 和 }} 的原因是因为 'bar' 指令使用 '=' 将标题绑定到其范围

scope: {
    title: '=',
    ...
}

问题是任务“nggettext_extract”没有提取此文本,因为它正在寻找大括号之间的内容。我找到了解决这个问题的方法:

<bar dummy="{{My Title'|translate}}" title="'My Title'|translate"></bar>

但我希望这个问题有更好的解决方案?

更新:我现在实施的解决方法是我将指令更改如下

scope: true,
link: function(scope, element, attrs) {
    scope.title = attrs.title;
}

当然,如果有人知道更好的解决方案,请告诉我!

【问题讨论】:

  • bar 指令内翻译?
  • 不,这不起作用,该过程不够聪明,无法解析变量。
  • bar 指令内:$filter('translate')(title)。请注意,需要注入过滤器依赖项
  • 这在提取阶段不起作用(nggettext_extract)
  • 这种方法是否也会触发动态语言更改?

标签: javascript angularjs gettext angular-gettext


【解决方案1】:

你可以这样做:

// Inside your controller

$scope.lbl = gettextCatalog.getString('Some text');

// And inside your template you can use

<bar title={{lbl}} > </bar>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2012-06-19
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多