Snippet: Android Plurals form

Android supports Plurals. Plurals are XML based resources which allow to handle different quantities.
Here you can find a simple implemetation.
Plurals can be placed in any xml-file in the values directory, but usually, it will be placed in strings.xml.
<plurals name="books">
   <item quantity="zero">No books</item>
   <item quantity="one">One book</item>
   <item quantity="two">Two books</item>
   <item quantity="few">Few books</item>
   <item quantity="many">Many books</item>
   <item quantity="other">%d books</item>
</plurals>
To get the plurals from code, you can use the getQuantityString(int id, int quantity).
Resources res = getResources();
String book = res.getQuantityString(R.plurals.books, count, count);
The call to getQuantityString() takes the id of the plural, the quantity used to select the appropriate plural, and optionally any arguments used for substitution in the display string.
If your plural strings do not include string formatting, you don't need to pass the third parameter.

Here a simple code:
public class PluralFormsActivity extends SherlockActivity {

   private void retrivePlurals() {
     
     TextView tw = (TextView) findViewById(R.id.text_xml);
     if (tw != null) {
        Resources res = getResources();
        String books0 = res.getQuantityString(R.plurals.books, 0);
        String books1 = res.getQuantityString(R.plurals.books, 1);
        String books2 = res.getQuantityString(R.plurals.books, 2);
        String booksN = res.getQuantityString(R.plurals.books, 10, 10);

        StringBuffer sb = new StringBuffer();
        sb.append(books0);
        sb.append("\n");
        sb.append(books1);
        sb.append("\n");
        sb.append(books2);
        sb.append("\n");
        sb.append(booksN);
        sb.append("\n");

        tw.setText(sb.toString());
    }
  }
Now, if you try this code, you will find that PluralRules, in english or italian, does not handle quantity "zero".
Selection works fine with one and other but not with zero and two.


Why?? It is not a issue.

Android is using the CLDR plurals system. What is CLDR? Here you can find how the system works.

The possible quantifiers are limited to these:
  • zero
  • one
  • two
  • few
  • many
  • other

It's important to understand that "one" does not mean the number 1.
The specific numbers n are keywords associated with categories.These associations are defined by rules in the CLDR database.

These categories are only mnemonics -- the names don't necessarily imply the exact contents of the category. For example, for both English and French the number 1 has the category one (singular). In English, every other number has a plural form, and is given the category other.....


With debugger you can see what happens in String books0 = res.getQuantityString(R.plurals.books, 0); with English Locale.



You can read in official doc:

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


Here you can find technical doc about PluralRules.

You can get code from GitHub:

Popular posts from this blog

Expand and collapse animation

How to centralize the support libraries dependencies in gradle

Android-5: Card and images with rounded corners in Android 4