Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getQuantityString returns wrong string with 0 value

In an android app, I have the following string resources:

<plurals name="test">
   <item quantity="zero">"I have 0 item"</item>
   <item quantity="one">"I have 1 item"</item>
   <item quantity="other">"I have several items"</item>
</plurals>

And the following line of code:

String text = getResources().getQuantityString(R.plurals.test, 0)

which I would expect to return

I have 0 item

But it actually returns

I have 1 item

Why ?

like image 298
sdabet Avatar asked Nov 21 '12 12:11

sdabet


1 Answers

Quantity Strings are broken on some Plattforms and phones as the issue Tracker and this discussion "Should Plurals and Quantity Strings be used" points out. It depends on many factors which you cannot control (i.e. localization on the phone).

One solution can be to take an external library like this one, which mimes the same functionallity.

Another solution is stated in the documentation of plurals in android. Avoid using it and use "quantity-neutral" formulations like "Books: 1"

like image 83
Rafael T Avatar answered Oct 23 '22 18:10

Rafael T