【问题标题】:AngularJS translation with pluralization using angular-translateAngularJS 使用 angular-translate 进行复数翻译
【发布时间】:2015-08-31 01:01:39
【问题描述】:

您好,我需要根据值进行复数翻译,但找不到如何做到这一点。

例如我有变量peopleCount

  1. peopleCount = 1 翻译应为:英语:{{ peopleCount }} person likes this 立陶宛语:{{ peopleCount }} zmogus tai megsta
  2. 如果peopleCount 超过1 个英文翻译将是:{{ peopleCount }} people like this

但对于立陶宛语翻译:

  1. 如果peopleCount 介于 2 和 9 之间或任何以这些数字结尾的数字,但以第 4 条规则中提供的数字结尾的数字除外(例如:225、249、210 {{ peopleCount }} zmones tai megsta
  2. 如果计数介于 10 和 20 或 30、40 或任何其他以零结尾的数字(如 150 或 90)之间,则为 {{ peopleCount }} zmoniu tai megsta

我该如何做到这一点?

【问题讨论】:

  • 请注意,正确的英语语法应该是“5 people like this”(不喜欢)。

标签: angularjs translation angular-translate pluralize


【解决方案1】:

这样的事情适用于您的场景:

<ng-pluralize count="peopleCount"
             when="{
                 'one': 'zmogus tai megsta',
                 'few': '{} zmones tai megsta',
                 'other': '{} zmoniu tai megsta'}">
</ng-pluralize>

您可以查看此内容以获取更多详细信息。 https://docs.angularjs.org/api/ng/directive/ngPluralize 对于语言特定的复数字符串: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html

【讨论】:

    【解决方案2】:

    Angular-translate 具有 MessageFormat 功能的服务,该功能非常强大,并且还具有内置的立陶宛语语言环境。 Article about MessageFormat and angular-translate.

    正在安装

    你可以通过 bower 安装这个包:

    $ bower install angular-translate-interpolation-messageformat
    

    然后按顺序包含必要的带有 MessageFormat 和 angular-translate-interpolation-messageformat 的脚本:

    <script src="path/to/messageformat.js"></script>
    <script src="path/to/angular-translate-interpolation-messageformat.js"></script>
    

    最后在你的配置函数中从 $translateProvider 调用 useMessageFormatInterpolation 函数:

    $translateProvider.useMessageFormatInterpolation();
    

    用法

    angular-translate-interpolation-messageformat 安装到您的应用后,您就可以使用它了。

    例如,您可以为代码“PEOPLE”创建英文本地化,如下所示:

    {
        "PEOPLE" : "{peopleCount, plural, one {There is one man (in lithuanian)} few {# zmones tai megsta} other {# zmoniu tai megsta}}"
    }
    

    比在你的 html 中这样使用它:

    <span translate="PEOPLE" translate-values="{peopleCount: 12}" translate-interpolation="messageformat"></span>
    

    输出将是:“12 zmones tai megsta”。

    【讨论】:

    • 仍然想知道是否有一种方法可以通过某种语句而不是onefewother 来提供更多选项?
    • 您可以在github 上阅读更多信息。还有select 声明,这不是真正的声明,但可能会对您的需求有所帮助。使用 select,您可以创建如下内容:"PEOPLE" : "{peopleCount, select, 1 {one man} 2 {two men} other {# people}}"
    • peopleCount 的值为 5 时,输出怎么会是“12 zmones tai megsta”? 12 != 5
    • $translateProvider.useMessageFormatInterpolation(); 对我不起作用,但查看源代码发现我需要 $translateProvider.addInterpolation('$translateMessageFormatInterpolation');
    【解决方案3】:

    我在没有angular-translate-interpolation-messageformat 的情况下实现了这一目标

    我的情况是:
    我有资源包标签:
    label.myItem=You have {{count}} item
    label.myItems=You have {{count}} items

    在 HTML 中我是这样写的:

    <ng-pluralize count="itemCount"
           when="{'one':'{{&quot;label.myItem&quot; | translate:{count: itemCount} }}',
                 'other':'{{&quot;label.myItems&quot; | translate: {count: itemCount} }}' }">
    </ng-pluralize>
    

    这里的itemCount 将是一个$scope 变量。

    通过这种方式,您无需安装任何新的角度包。

    输出是: 当我有 1 个时:

    您有 1 件商品

    当我有 2 个(超过 1 个)时:

    您有 2 件商品

    【讨论】:

      猜你喜欢
      • 2014-02-27
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多