AngularJS 使用 angular-translate 进行复数翻译
AngularJS translation with pluralization using angular-translate
您好,我需要根据值进行复数翻译,但找不到如何操作。
例如我有变量 peopleCount
.
peopleCount = 1
翻译应为:英语:{{ 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
- 如果计数介于 10 和 20 或 30、40 或任何其他以零结尾的数字,如 150 或 90,它将是
{{ peopleCount }} zmoniu tai megsta
我该如何完成?
类似这样的内容适用于您的场景:
<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
Angular-translate 提供具有 MessageFormat 功能的服务,该功能非常强大,并且还具有立陶宛语的内置语言环境。 Article about MessageFormat and angular-translate.
正在安装
你可以通过 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”。
我在没有 angular-translate-interpolation-messageformat
的情况下做到了这一点
我的情况是:
我有资源包标签:
label.myItem=You have {{count}} item
label.myItems=You have {{count}} items
在HTML我是这样写的:
<ng-pluralize count="itemCount"
when="{'one':'{{"label.myItem" | translate:{count: itemCount} }}',
'other':'{{"label.myItems" | translate: {count: itemCount} }}' }">
</ng-pluralize>
此处 itemCount
将是一个 $scope
变量。
通过这种方式,您无需安装任何新的 angular 软件包。
输出为:
当我有 1:
You have 1 item
当我有 2 个(多于 1 个)时:
You have 2 items
您好,我需要根据值进行复数翻译,但找不到如何操作。
例如我有变量 peopleCount
.
peopleCount = 1
翻译应为:英语:{{ 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
- 如果计数介于 10 和 20 或 30、40 或任何其他以零结尾的数字,如 150 或 90,它将是
{{ peopleCount }} zmoniu tai megsta
我该如何完成?
类似这样的内容适用于您的场景:
<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
Angular-translate 提供具有 MessageFormat 功能的服务,该功能非常强大,并且还具有立陶宛语的内置语言环境。 Article about MessageFormat and angular-translate.
正在安装
你可以通过 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”。
我在没有 angular-translate-interpolation-messageformat
我的情况是:
我有资源包标签:
label.myItem=You have {{count}} item
label.myItems=You have {{count}} items
在HTML我是这样写的:
<ng-pluralize count="itemCount"
when="{'one':'{{"label.myItem" | translate:{count: itemCount} }}',
'other':'{{"label.myItems" | translate: {count: itemCount} }}' }">
</ng-pluralize>
此处 itemCount
将是一个 $scope
变量。
通过这种方式,您无需安装任何新的 angular 软件包。
输出为: 当我有 1:
You have 1 item
当我有 2 个(多于 1 个)时:
You have 2 items