2015-06-15 88 views
5

你好,我需要根据一个值进行多元化的翻译,但无法找到如何做到这一点。AngularJS翻译使用角度翻译的多元化

例如我有变量peopleCount

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

但对于立陶宛语翻译:

  • 如果peopleCount是2,9或任意数量的不同之处,其与在第四规则提供号码结束数字与这些号码结束之间(例如:225,249,210 < ---这些是正确的数字,以及不正确的数字:215,250 ...)。这将是{{ peopleCount }} zmones tai megsta
  • 如果计数是10和20或30,40或具有零像150或90结束任何其它数量之间这将是{{ peopleCount }} zmoniu tai megsta
  • 如何实现这个目标?

    +1

    Just注意适当的英语语法应该是“5人喜欢这样”(不喜欢)。 – mkaj

    回答

    12

    角翻译与MessageFormat中的功能服务,这是真正的强大,还内置了语言环境立陶宛。 Article about MessageFormat and angular-translate.

    安装

    您可以通过亭子安装该软件包:

    $ bower install angular-translate-interpolation-messageformat 
    

    此后包括与该顺序的MessageFormat和角翻译插值的MessageFormat必要的脚本:

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

    最后在你的配置函数中调用useMessageFormatInter从$ translateProvider polation功能:

    $translateProvider.useMessageFormatInterpolation(); 
    

    使用

    安装angular-translate-interpolation-messageformat到您的应用程序,你可以使用它之后。

    例如,您可以创建密码“人”英语本地化此:

    { 
        "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“。

    +0

    仍然想知道是否有一种方法可以使用更多的选项来代替'one','few'和'other'。 – Einius

    +0

    您可以在[github](https://github.com/SlexAxton/messageformat.js/)上阅读更多内容。还有'select'声明,这不是真正的声明,但可能会有助于您的需求。选择你可以创建如下:'“people”:“{peopleCount,select,1 {one man} 2 {two men} other {#people}}”' –

    +0

    输出结果如何可以是“12 zmones tai megsta” ,当'peopleCount'的值是5时? 12!= 5 – cezar

    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商品