getQuantityString returns wrong string with 0 value

荒凉一梦 提交于 2020-01-14 07:26:24

问题


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 ?


回答1:


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"




回答2:


Change the code like this

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


来源:https://stackoverflow.com/questions/13493011/getquantitystring-returns-wrong-string-with-0-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!