19

我正在尝试在我最新的 Android 项目(SDK9)中使用复数。它总是以 ResourcesNotFoundException 结束。但是资源在那里 - 绝对是:

这是我的 strings.xml 的那一部分:

<plurals name="count_files">
  <item quantity="one">%d file</item>
  <item quantity="other">%d files</item>
  <item quantity="zero">%d files</item>
</plurals>

<plurals name="count_folders">
  <item quantity="one">%d folder</item>
  <item quantity="other">%d folders</item>
  <item quantity="zero">%d folders</item>
</plurals>

这是使用它的那部分:

textView.setText(
    getResources().getQuantityString(R.plurals.count_folders, countfolders, countfolders) 
    + ", " 
    + getResources().getQuantityString(R.plurals.count_files, countfiles, countfiles));

这是一个例外:

android.content.res.Resources$NotFoundException: Plural resource ID #0x7f050001 quantity=0 item=other

我必须维护多达 15 种不同的语言,因此我需要本地化文本和复数。

知道我的代码有什么问题吗?

提前谢谢了。

4

2 回答 2

1

Android 以这样一种方式实现复数,即您需要考虑每个翻译以一致地要求每个复数项目。例如,如果您在 'es' 中声明了 'zero' 的复数形式,并且还有一个 'fr' 文件,则还需要在 'fr' 文件中定义 'zero' 复数形式。

于 2013-12-21T01:44:19.427 回答
0

就我而言,我错过了other价值并收到了异常:android.content.res.Resources$NotFoundException: Plural resource ID #0x7f050001 quantity=9 item=other在英语语言环境中。

<plurals>包含这组属性,可以任意使用(不同的语言使用不同的值):

<plurals name="some_plurals">
    <item quantity="zero">%1$d = zero</item>
    <item quantity="one">%1$d = one</item>
    <item quantity="two">%1$d = two</item>
    <item quantity="few">%1$d = few</item>
    <item quantity="many">%1$d = many</item>
    <item quantity="other">%1$d = other</item>
</plurals>

另请参阅如何将 Android 数量字符串(复数)用于不同的语言。

于 2021-12-10T15:37:14.470 回答