您好我需要根据值进行复数翻译,但无法找到如何做到这一点。
例如我有变量peopleCount
。
peopleCount = 1
翻译应为:english:{{ peopleCount }} person likes this
立陶宛语:{{ peopleCount }} zmogus tai megsta
peopleCount
超过1,则英文翻译为:{{ peopleCount }} people like this
。但对于立陶宛语翻译:
peopleCount
介于2和9之间,或任何以这些数字结尾的数字,除了以第4个规则中提供的数字结尾的数字(例如:225,249,210< ---这些是正确的数字。不正确的:215,250 ......)。它将是{{ peopleCount }} zmones tai megsta
{{ peopleCount }} zmoniu tai megsta
我如何做到这一点?
答案 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
我写的是这样的:
<ng-pluralize count="itemCount"
when="{'one':'{{"label.myItem" | translate:{count: itemCount} }}',
'other':'{{"label.myItems" | translate: {count: itemCount} }}' }">
</ng-pluralize>
此处itemCount
将是$scope
变量。
通过这种方式,您无需安装任何新的角度包。
输出为: 当我有1:
你有1件
当我有2个(超过1个)时:
你有2件