使用角度平移的多重化的AngularJS平移

时间:2015-06-15 13:43:11

标签: angularjs translation angular-translate pluralize

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

例如我有变量peopleCount

  1. peopleCount = 1翻译应为:english:{{ peopleCount }} person likes this立陶宛语:{{ peopleCount }} zmogus tai megsta
  2. 如果peopleCount超过1,则英文翻译为:{{ peopleCount }} people like this
  3. 但对于立陶宛语翻译:

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

3 个答案:

答案 0 :(得分:12)

Angular-translate has service with functionality of MessageFormat which is really powerful and also has built-in locale for lithuanian. Article about MessageFormat and angular-translate.

Installing

You can install this package via bower:

$ bower install angular-translate-interpolation-messageformat

After that include necessary scripts with MessageFormat and angular-translate-interpolation-messageformat in that order:

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

And finally in your config function call useMessageFormatInterpolation function from $translateProvider:

$translateProvider.useMessageFormatInterpolation();

Usage

After installing angular-translate-interpolation-messageformat into your app you can work with it.

For example, you can create english localization for code 'PEOPLE' as this:

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

And than use it in your html like this:

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

The output will be: "12 zmones tai megsta".

答案 1 :(得分: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 :(得分: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件