如何使用成角度的9 $ localize复数? [英] How to use angular 9 $localize with plurals?
问题描述
自Angular 9开始,我们就可以使用
Since Angular 9 we can use
$localize`Hello ${name}:name:`
对于i18n,使用打字稿代码.这仍然有一些限制,因为 ng xi18n
命令无法检测到字符串,但是如果将这些文本手动添加到翻译文件中,它将起作用.
For i18n in typescript code. This still has some limitations as the ng xi18n
command does not detect the strings, but if these texts are added manually to the translation file it works.
The $localize
function is quite well documented in the JSDoc in the source, however it does not explain how to work with plurals. What I mean is something like this (pseudo-code):
$localize`Hello {${count}, plural, =1 {reader} other {readers}}`
$ localize
是否可能?如果是:如何?如果不是:Angular如何将此类表达式从HTML编译为TypeScript?
Is this possible with $localize
? If yes: How? If no: How does Angular compile such expressions from HTML to TypeScript?
推荐答案
For now, it is not possible to use ICUs with $localize
, as discussed in this github issue. From the last comments, it looks like angular team is considering it if it remains lightweight.
同时,建议的解决方法是创建自己的帮助程序方法,该方法根据count参数返回正确的翻译.
Meanwhile, the suggested workaround is to create your own helper method that returns the correct translation based on the count parameter.
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;
}
这篇关于如何使用成角度的9 $ localize复数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!