Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Plurals ends in ResourcesNotFoundException

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.

like image 314
Harald Wilhelm Avatar asked Jul 13 '11 10:07

Harald Wilhelm


2 Answers

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.

like image 176
Matyas Avatar answered Oct 31 '22 00:10

Matyas


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.

like image 1
CoolMind Avatar answered Oct 31 '22 00:10

CoolMind