6

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

例如我有变量peopleCount

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

但对于立陶宛语翻译:

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

我该如何做到这一点?

4

3 回答 3

12

Angular-translate 具有 MessageFormat 功能的服务,该功能非常强大,并且还具有立陶宛语的内置语言环境。关于 MessageFormat 和角度翻译的文章。

安装

你可以通过 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”。

于 2015-06-15T14:29:09.573 回答
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

于 2015-06-15T13:57:13.820 回答
0

我没有做到这一点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 件商品

于 2018-02-21T10:47:35.260 回答