If have the following plural ressource in my strings.xml:
- No item
I've wrote a Kotlin extension to handle all the scenarios that I can think about.
I have zeroResId
as optional, so that sometimes we want to handle the zero by displaying "No Items", rather than "0 Items".
English treats zero grammatically as plural.
The selection of which string to use is made solely based on grammatical necessity.
In English, a string for zero is ignored even if the quantity is 0, because 0 isn't grammatically different from 2, or any other number except 1 ("zero books", "one book", "two books", and so on).
https://developer.android.com/guide/topics/resources/string-resource.html#Plurals
fun Context.getQuantityStringZero(
quantity: Int,
pluralResId: Int,
zeroResId: Int? = null
): String {
return if (zeroResId != null && quantity == 0) {
resources.getString(zeroResId)
} else {
resources.getQuantityString(pluralResId, quantity, quantity)
}
}