Plural definition is ignored for zero quantity

梦想与她 提交于 2019-11-27 22:42:01

According to the documentation :

The selection of which string to use is made solely based on grammatical necessity. In English, a string for zero will be 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).

If you still want to use a custom string for zero, you can load a different string when the quantity is zero :

if (commentsCount == 0)
    str = res.getString(R.string.number_of_comments_zero);
else
    str = res.getQuantityString(R.plurals.number_of_comments, commentsCount, commentsCount);
cuasodayleo

Plural is Unicode form. all of plural value here. In English, plural for zero like 2, 3,4 so you must if else this value to use others string for this.

In Kotlin (thanks to Dalmas):

val result = commentsCount.takeIf { it != 0 }?.let {
    resources.getQuantityString(R.plurals.number_of_comments, it, it)
} ?: resources.getString(R.string.number_of_comments_zero)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!