零数量忽略多个定义

JJD*_*JJD 32 android plural android-xml android-resources

我使用复数来编译Android应用程序的数量字符串.我完全按照教程中的内容进行操作:

res.getQuantityString(
    R.plurals.number_of_comments, commentsCount, commentsCount);
Run Code Online (Sandbox Code Playgroud)

这是复数的定义:

<?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>
Run Code Online (Sandbox Code Playgroud)

有趣的是,输出字符串与我所定义的一样奇怪:

commentsCount = 0 => "0 comments"  
commentsCount = 1 => "One comment"  
commentsCount = 2 => "2 comments"
Run Code Online (Sandbox Code Playgroud)

我想这是因为文档说明When the language requires special treatment of the number 0 (as in Arabic).zero数量.有没有办法强迫我的定义?

Dal*_*mas 55

根据文件:

选择使用哪个字符串完全基于语法必要性.在英语中,即使数量为0,也会忽略零字符串,因为0在语法上与2不相同,或者除了1之外的任何其他数字("零书","一本书","两本书"和等等).

如果您仍希望将自定义字符串用于零,则可以在数量为零时加载其他字符串:

if (commentsCount == 0)
    str = res.getString(R.string.number_of_comments_zero);
else
    str = res.getQuantityString(R.plurals.number_of_comments, commentsCount, commentsCount);
Run Code Online (Sandbox Code Playgroud)

  • 蹩脚的机器人,让我们做这些肮脏的解决方法.在所有情况下都应该使用`plurals`定义,因此我们的代码更清晰! (6认同)
  • 那么,为什么Android会将自动完成功能设置为零,几,多等等,如果它们仍然无法正常工作呢? (2认同)
  • @htafoya他们_do_工作,他们只是没有映射为英语,因为没有语法上的必要性.数量和语言的组合决定了类别(零,一,二,几,多,其他),而对于"en",它只是"一个"或"其他".对威尔士人来说,有六个类别.翻译人员应该知道在哪种情况下使用哪种类别的语言. (2认同)
  • @LWChris 好的,我的问题是默认语言(没有 mods 的 res 文件夹)不是英语,android 认为是英语,所以它忽略了零文本?有没有办法设置默认的语言属性? (2认同)