39

I use plurals to compile a quantity string for an Android application. I follow exactly what one can find in the tutorials:

res.getQuantityString(
    R.plurals.number_of_comments, commentsCount, commentsCount);

Here is the definition of the plurals:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="number_of_comments">
        <item quantity="zero">No comments</item>
        <item quantity="one">One comment</item>
        <item quantity="other">%d comments</item>
    </plurals>
</resources>

Interesting enough, the output string is odd to what I definied:

commentsCount = 0 => "0 comments"  
commentsCount = 1 => "One comment"  
commentsCount = 2 => "2 comments"

I guess this is because the docs state When the language requires special treatment of the number 0 (as in Arabic). for zero quantity. Is there any way to force my definition?

JJD
  • 50,076
  • 60
  • 203
  • 339

3 Answers3

66

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);
Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • 13
    crappy android that makes us do these dirty workarounds. `plurals` definition should be used in all cases so our code is cleaner! – voghDev Nov 08 '16 at 06:50
  • 3
    So why did android place autocomplete for zero , few, many , etc .. if they won't work anyway? – htafoya Sep 16 '17 at 23:18
  • 3
    @htafoya They _do_ work, they are just not mapped for English because there is no grammatical necessity. The combination of quantity and language determines the category (zero, one, two, few, many, other), and for "en" it's just `one` or `other`. For Welsh there are all six categories. It's up to the translators to know which categories are used in which case for their language. – LWChris Oct 18 '17 at 10:30
  • 2
    @LWChris ok, my problem is that the DEFAULT language (res folder with no mods) is not in English, and android think it is so it is ignoring the zero texts? Is there a way to set up default lang properties? – htafoya Oct 19 '17 at 22:08
  • 1
    Anyway, they should enable it in english because many times you use sentences and not the single word. And I would prefer to place "none" or "empty" instead of 0. – htafoya Oct 19 '17 at 22:10
  • @LWChris, evidently Android works wrong. This is the same as you wanted 5 + 0 = 5, but Android returned 7. 0 is zero, and for Russian and French strings (in English locale) it doesn't pay any attention to `zero`. If we want "no items", why we get "0 items"? – CoolMind Apr 25 '19 at 09:55
  • Thanks. See also https://stackoverflow.com/questions/5651902/android-plurals-treatment-of-zero/5671704 for another variant. – CoolMind Apr 25 '19 at 10:13
  • @CoolMind You're taking "zero" too literally here. "Zero" is not the quantity `0` itself, it's the _name_ of a quantity-class for "zero-ish" values. Instead of "Zero", "One", "Two", etc., the classes could've been named "A", "B" and "C" as well. The `quantity` parameter is used to determine the quantity _class_ with respect to the language. And if a language only has two classes, Android will only have two classes. Some languages use the quantity class _named_ "two" for all "two-ish" quantities, i. e. `2`, `12`, `22` etc. – LWChris Apr 25 '19 at 11:16
  • @LWChris, in Russian we have "two-ish" quantities for `2`, `22`, `32` etc. (except ending from 10 to 19, there is another rule). So, we distinguish several situations and have all items in plurals (`zero`, `one`, `two`, `few`, `many`, `other`). I agree, English locale has only two states. But why they remove zero variant, if we provide it? Why Android decides for us what to do? If we want to have "No items available" instead of "0 items", why should we programmatically do it? I think, in this case English has 3 states. – CoolMind Apr 25 '19 at 12:10
  • 1
    @CoolMind Because `plurals` are a localization tool, so it's not about UI elegance but linguistical correctness. The desire to display "No results" instead of "0 results" is not a grammatical necessity, it is purely a choice of design, like displaying "a week" instead of "7 days". – LWChris Apr 25 '19 at 21:33
  • @LWChris, maybe you are right. In Russian we have equal forms for 0 and 5-9. But strangely Android distinguishes between 0 and 5, so we have "No items" and "5 items" on devices with Russian locale. In English locale we have "0 items" for the same. – CoolMind Apr 26 '19 at 06:59
  • 1
    @CoolMind CLDR (whose standard Android uses) states that implementations are encouraged to provide special cases for 0 to allow for more natural language. According to [the docs](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ru), `0` _should_ have the same category as `5`. – LWChris Apr 26 '19 at 10:59
  • @LWChris, thanks! So, should we check for 0 implicitly to avoid a situation of 10, 100, 1000 etc? – CoolMind Apr 26 '19 at 14:22
4

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)
CoolMind
  • 26,736
  • 15
  • 188
  • 224
3

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.

LWChris
  • 3,320
  • 1
  • 22
  • 39
cuasodayleo
  • 466
  • 5
  • 16