2015-06-15 87 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商品