零数量忽略复数定义 [英] Plural definition is ignored for zero quantity
问题描述
我使用 复数 来编译数量字符串对于 Android 应用程序.我完全按照教程中可以找到的内容进行操作:
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);
这里是复数的定义:
<?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"
我想这是因为文档声明 当语言需要对数字 0 进行特殊处理时(如阿拉伯语).
用于 zero
数量.有没有办法强制我的定义?
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?
推荐答案
根据 文档 :
选择使用哪个字符串完全基于语法上的必要.在英语中,零的字符串将被忽略即使数量为 0,因为 0 在语法上没有区别从 2 或除 1 以外的任何其他数字(零本书"、一本书"、两书"等).
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);
这篇关于零数量忽略复数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!