How to use angular 9 $localize with plurals?
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.
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;
}
I've just read issue https://github.com/angular/angular/issues/35912 and I think that intl-messageformat
can do what you need.
See https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat.