我使用 plurals为 Android 应用程序编译一个数量字符串。我完全按照教程中可以找到的内容进行操作:
res.getQuantityString(
R.plurals.number_of_comments, commentsCount, commentsCount);
这里是复数的定义:
<?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>
有趣的是,输出字符串与我定义的很奇怪:
commentsCount = 0 => "0 comments"
commentsCount = 1 => "One comment"
commentsCount = 2 => "2 comments"
我猜这是因为文档声明 当语言需要对数字 0 进行特殊处理时(如阿拉伯语)。
表示 zero
数量。有什么方法可以强制我的定义?
最佳答案
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 (commentsCount == 0)
str = res.getString(R.string.number_of_comments_zero);
else
str = res.getQuantityString(R.plurals.number_of_comments, commentsCount, commentsCount);
关于android - 零数量忽略复数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17261290/