3

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

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • 1
    You may read the docs again. Zero values need a custom threatment: http://stackoverflow.com/questions/17261290/plural-definition-is-ignored-for-zero-quantity – eduyayo Mar 17 '15 at 11:51
  • Plurals = nightmare, write a utils class for your interpretation of what you need to happen, you will either get it right or learn something new :) – CaptRisky Oct 24 '17 at 15:12

4 Answers4

5

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"

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
2

On the android device where input language is English the case with 0 or 2 will be handled the same. So in russian language (and very possible in others) if you app use russian language and you use it on the device with English input, you always will get the value for zero case from many case. For example 2 Билетов а не 2 Билета

Mikhail Valuyskiy
  • 1,238
  • 2
  • 16
  • 31
0

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

Because English has only a special case for quantity one. The quantity other is picked up for all other cases.

Please read the documentation.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

From developer.android.com:

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.

You'd only need to specify one and other here ("song" vs. "songs").

gosr
  • 4,593
  • 9
  • 46
  • 82