Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android plurals not working

Tags:

android

I start to meet with android plurals and i'm stack in usability of this feature. I declare plurals.xml and try to read them from my code, but i getting incorrect results.

Plurals :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="numbers">
        <item quantity="zero">No comments</item>
        <item quantity="one">%1$d comment.</item>
        <item quantity="two">%1$d comments.</item>
        <item quantity="few">%1$d comments.</item>
        <item quantity="many">%1$d comments.</item>
        <item quantity="other">%1$d comments.</item>
    </plurals>
    <plurals name="numberOfSongsAvailable">
      
        <item quantity="zero">No song found.</item>
        <item quantity="one">One song found.</item>
        <item quantity="two">Two song found.</item>
        <item quantity="other">Other songs found.</item>
    </plurals>
</resources>   

Activity :

public class AndroidPlurals extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plurals);
        ((ListView) findViewById(R.id.listView)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getPluralsList(10)));
        Toast.makeText(this, getClass().getSimpleName(), Toast.LENGTH_LONG).show();
    }

    private String[] getPluralsList(int k) {
        String[] array = new String[k];
        for (int i = 0; i < array.length; i++) {
            String quantityString = (String) this.getResources().getQuantityText(R.plurals.numberOfSongsAvailable, i);
            array[i] = quantityString;
            android.util.Log.i("Plurals", "i = " + i + ", quantityString = " + quantityString);
        }
        return array;
    }
}

OutPut :

I/Plurals (17689): i = 0, quantityString = Other songs found.
I/Plurals (17689): i = 1, quantityString = One song found.
I/Plurals (17689): i = 2, quantityString = Other songs found.
I/Plurals (17689): i = 3, quantityString = Other songs found.
I/Plurals (17689): i = 4, quantityString = Other songs found.
I/Plurals (17689): i = 5, quantityString = Other songs found.
I/Plurals (17689): i = 6, quantityString = Other songs found.
I/Plurals (17689): i = 7, quantityString = Other songs found.
I/Plurals (17689): i = 8, quantityString = Other songs found.
I/Plurals (17689): i = 9, quantityString = Other songs found.

Why i have not correct result for 0 quantity like a No song found. text???

Note : tested on Android 5.0 Lolipop

like image 633
Sergey Shustikov Avatar asked Sep 23 '26 13:09

Sergey Shustikov


1 Answers

The short answer is that on Android, plurals are meant for grammatical distinctions.

Quote from the docs (my highlight):

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). Conversely, in Korean only the other string will ever be used.

And:

Don't be misled either by the fact that, say, two sounds like it could only apply to the quantity 2: a language may require that 2, 12, 102 (and so on) are all treated like one another but differently to other quantities. Rely on your translator to know what distinctions their language actually insists upon.

More details and alternatives here: Android plurals treatment of "zero"

like image 108
MH. Avatar answered Sep 25 '26 19:09

MH.