Pluralizing is as natural in human language as it is painful to handle programmatically. Let’s say we want to address the following scenarios in an app that displays the number of posts:
- If we have zero, display “No post.”
- If we have one, display “1 post.”
- If we have more than one, say 5, display “5 posts.”
We could use a combination of ngIf / else
scenarios to handle this, but it would get ugly quickly. The upcoming control flow API would help, but it’s not available yet.
Instead, we can use the i18nPlural
pipe as follows:
The above syntax determines what to display for each scenario: 0,1 or any other value. You can find an example here on Stackblitz.