android - 零数量忽略复数定义

标签 android android-xml android-resources plural

我使用 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 documentation :

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/

相关文章:

android - 向表布局中的行添加数据

android - 将 Android SQLite 移植到 iOS

Android NaviagtionDrawer 项目文本对齐

android - TextView - 分割线

Android textview 不支持换行

android - 动态需要可绘制资源的整数资源ID

android - 在路径 : DexPathList for KeyGenParameterSpec$Builder 上找不到类

java - "One App"安卓系统

android - 如何在 Android Jetpack Compose 中使用字符串资源?

java - 是否可以在 Android 中使用字符串加载资源?