如何使用 angular 9 $localize 与复数?
How to use angular 9 $localize with plurals?
因为 Angular 9 我们可以使用
$localize`Hello ${name}:name:`
对于 typescript 代码中的 i18n。这仍然有一些限制,因为 ng xi18n
命令不检测字符串,但如果将这些文本手动添加到翻译文件中,它就可以工作。
$localize
函数在 JSDoc in the source 中有很好的记录,但是它没有解释如何使用复数。我的意思是这样的(伪代码):
$localize`Hello {${count}, plural, =1 {reader} other {readers}}`
$localize
可以吗?如果是:如何?如果否:Angular 如何将此类表达式从 HTML 编译为 TypeScript?
目前,无法将 ICU 与 $localize
一起使用,如本 github issue 中所述。从最后的评论来看,angular 团队似乎正在考虑它是否保持轻量化。
同时,建议的解决方法是创建您自己的辅助方法,returns 根据计数参数进行正确的翻译。
title = $localize `Hi ${this.name}! You have ${
plural(this.users.length. {
0: $localize `no users`,
1: $localize `one user`,
other: $localize`${this.users.length} users`,
}.`
function plural(value, options) {
// Handle 0, 1, ... cases
const directResult = options[value];
if (directResult !== undefined) { return directResult; }
// handle zero, one, two, few, many
// ...
return options.other;
}
我刚刚阅读了第 https://github.com/angular/angular/issues/35912 期,我认为 intl-messageformat
可以满足您的需求。
参见https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat。
因为 Angular 9 我们可以使用
$localize`Hello ${name}:name:`
对于 typescript 代码中的 i18n。这仍然有一些限制,因为 ng xi18n
命令不检测字符串,但如果将这些文本手动添加到翻译文件中,它就可以工作。
$localize
函数在 JSDoc in the source 中有很好的记录,但是它没有解释如何使用复数。我的意思是这样的(伪代码):
$localize`Hello {${count}, plural, =1 {reader} other {readers}}`
$localize
可以吗?如果是:如何?如果否:Angular 如何将此类表达式从 HTML 编译为 TypeScript?
目前,无法将 ICU 与 $localize
一起使用,如本 github issue 中所述。从最后的评论来看,angular 团队似乎正在考虑它是否保持轻量化。
同时,建议的解决方法是创建您自己的辅助方法,returns 根据计数参数进行正确的翻译。
title = $localize `Hi ${this.name}! You have ${
plural(this.users.length. {
0: $localize `no users`,
1: $localize `one user`,
other: $localize`${this.users.length} users`,
}.`
function plural(value, options) {
// Handle 0, 1, ... cases
const directResult = options[value];
if (directResult !== undefined) { return directResult; }
// handle zero, one, two, few, many
// ...
return options.other;
}
我刚刚阅读了第 https://github.com/angular/angular/issues/35912 期,我认为 intl-messageformat
可以满足您的需求。
参见https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat。