Android的复数的治疗和QUOT;零" [英] Android plurals treatment of "zero"
问题描述
如果在我的strings.xml以下复数的ressource:
If have the following plural ressource in my strings.xml:
<plurals name="item_shop">
<item quantity="zero">No item</item>
<item quantity="one">One item</item>
<item quantity="other">%d items</item>
</plurals>
我使用的显示结果给用户:
I'm showing the result to the user using:
textView.setText(getQuantityString(R.plurals.item_shop, quantity, quantity));
它的工作很好地与1以上,但如果数量为0,那么我看空的。 零的价值只支持阿拉伯语语言文档似乎预示? 还是我失去了一些东西?
It's working well with 1 and above, but if quantity is 0 then I see "0 items". Is "zero" value supported only in Arabic language as the documentation seems to indicate? Or am I missing something?
推荐答案
国际化的Android资源的方法是相当有限的。我曾使用标准的要好得多成功 java.text.MessageFormat中的
。
The Android resource method of internationalisation is quite limited. I have had much better success using the standard java.text.MessageFormat
.
基本上,所有你需要做的就是用这样的标准字符串资源:
Basically, all you have to do is use the standard string resource like this:
<resources>
<string name="item_shop">{0,choice,0#No items|1#One item|1<{0} items}</string>
</resources>
然后,从code所有你需要做的是以下内容:
Then, from the code all you have to do is the following:
String fmt = resources.getText(R.string.item_shop);
textView.setText(MessageFormat.format(fmt, amount));
您可以阅读更多有关格式字符串的 javadoc,了解的MessageFormat 一>
You can read more about the format strings in the javadocs for MessageFormat
这篇关于Android的复数的治疗和QUOT;零&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!