19

I am trying to use plurals in my latest Android project (SDK9). It always ends in an ResourcesNotFoundException. But the Resource is there - definitely:

Here's that part of my 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>

And here's that part that's using it:

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

Here's the exception:

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

I do have to maintain up to 15 different languages thus I need localization of text and plurals.

Any idea what's wrong with my code?

Many thanks in advance.

Oliv
  • 10,221
  • 3
  • 55
  • 76
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
  • What do you want to tell me with this link? I don't see any difference between my code and that found on this website. I do get an error... – Harald Wilhelm Jul 14 '11 at 13:13
  • I'm using this same configuration just now and it works. Are you sure you're placing in your default "values" folder? – Christopher Orr Jul 14 '11 at 13:41
  • The XML code shown above is part of my strings.xml in the res/values folder. The Android docs say explicit that this is the usual, but not required, place: "A plurals collection is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine plurals resources with other simple resources in the one XML file, under one element". Scatching head, don't know what's wrong with my code ... – Harald Wilhelm Jul 14 '11 at 14:05
  • 10
    I found it. On string resources you can left out identical strings in translation files (values-fr, etc.). These missing strings will be fetched from the default language then. This seems to fail with plurals. In one language specific strings.xml file I had left out some plural because they were identical to the default language. After adding these missing plurals it worked. – Harald Wilhelm Jul 15 '11 at 06:26
  • 1
    I have to tell that there must be a bug at least on SDK9. If you use plurals in different language files and your user uses a different language, an app will crash even if the default language (values folder) has plurals defined. Example: You use plurals in folder values, values-pt and values-fr. Your user comes with values-es your app will crash. – Harald Wilhelm Jul 19 '11 at 02:59
  • 12
    Please answer your own question and accept your answer so that this question doesn't show up on the "unanswered" list. – David Wasser Dec 02 '13 at 14:54
  • Have a look at the comment on [this thread](http://stackoverflow.com/questions/17261290/plural-definition-is-ignored-for-zero-quantity) – voghDev Nov 08 '16 at 06:48

2 Answers2

1

Android implements plurals in such a way that you need to consider each translation to require every plural item consistently. For example, if you declared a plural for 'zero' in 'es', and you have a 'fr' file as well, you'll need to defined the 'zero' plural in the 'fr' file as well.

Matyas
  • 311
  • 1
  • 4
  • 14
1

In my case I missed other value and received the exception: android.content.res.Resources$NotFoundException: Plural resource ID #0x7f050001 quantity=9 item=other in English locale.

<plurals> contain this set of attributes, you can use any (different languages use different values):

<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>

See also How to use Android quantity strings (plurals) for different languages.

CoolMind
  • 26,736
  • 15
  • 188
  • 224