8

In res/values/strings.xml, I have

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="days_amount">
        <item quantity="one">%d day EN</item>
        <item quantity="other">%d days EN</item>
</plurals>
</resources>

And in res/values-fr/strings.xml, i have

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="days_amount">
        <item quantity="one">%d jour FR</item>
        <item quantity="other">%d jours FR</item>
    </plurals>
</resources>

With English locale res.getQuantityString(R.plurals.days_amount, 0, 0) returns:

"0 days"

which is okay per English rules (aka zero quantity <=> several quantities).


But with a French locale, it returns:

"0 jours"

which is NOT okay per French rules (aka zero quantity <=> singular quantity).

It should have returned "O jour"

French rules: http://www.unicode.org/cldr/charts/25/supplemental/language_plural_rules.html#fr


So, is this a bug, or am I doing something wrong ?

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
Teybeo
  • 490
  • 4
  • 11
  • 1
    In my opinion, it's right. Because `0 != 1`. Maybe, you can solve it adding `%d jour FR`? – Phantômaxx Jan 07 '15 at 16:41
  • If you have a device with English locale and speak Russian, it will write "0 days" (in Russian) instead of "zero days", "no days" (what you wrote in `zero` row). Englishmen don't know about foreign locales and make this mistake in Android. See https://stackoverflow.com/questions/17261290/plural-definition-is-ignored-for-zero-quantity/17261327. – CoolMind Apr 25 '19 at 09:17

3 Answers3

3

Your code looks correct to me. Here's why:

  1. The Android documentation contains the text zero, one, two, few, many, and other, which exactly matches the wording of the Unicode language plural rules. This suggests that Android uses them.
  2. These plural rules state that the French case of 0 books is handled by the one case.

Since the Android documentation doesn't explicitly mention the Unicode language plural rules, you should request clarification from the Android support.

Before you do that, you should consult this other question, where the French rules obviously worked.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
1

Indeed your concern is right, in French amount 0 should be treated as 'one' instead of 'other'. The issue is in some Android APIs: API <= 16

So in case you want still to support API 16 just add an extra string key for the zero amount. Let's assume you have:

<plurals name="label_message_reminder_text">
        <item quantity="other">Vous avez %1$s nouveaux messages</item>
        <item quantity="one">Vous avez %1$s nouveau message</item>
</plurals>

Just add an extra string:

<string name="label_message_reminder_text_zero">Vous avez %1$s nouveau message</string>

And then when retrieving plurals can do something like:

String newMessagesText = getMyString(context, resourceId, amount);
messageTextView.setText(String.format(newMessagesText, amount));

And you can have a method to do the getMyString():

public String getMyString(Context context, int resourceId, int amount){

  if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) {    
     if(amount == 0) {
        switch (resourceId) {
           case R.plurals.label_message_reminder_text:
              return context.getResources().getString(R.string.label_message_reminder_text_zero);
        }
     }
  }

  return context.getResources().getQuantityString(resourceId, amount);

}
denis_lor
  • 6,212
  • 4
  • 31
  • 55
-1

You're doing something wrong. It's doing exactly what you're telling it to.

Your rules say that when the value is other, use "days" or "jours". You then pass in the value 0, which is other, so it uses "days" and "jours", as requested.

You could try setting a third value for quantity "zero" which has the language-specific rules.

Dan Ambrogio
  • 356
  • 2
  • 10
  • 2
    Yeah i understand what you both means, but, as i have to discover myself, the plural system doesn't work this way. From the 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). Conversely, in Korean only the other string will ever be used. " http://developer.android.com/guide/topics/resources/string-resource.html#Plurals – Teybeo Jan 07 '15 at 16:50
  • Your link states to use `zero` "When the language requires special treatment of the number 0 (as in Arabic)." `Other` should be used "When the language does not require special treatment of the given quantity". In this case, it appears that the value requires special treatment. – Dan Ambrogio Jan 07 '15 at 16:53
  • 1
    Already tried adding a "zero", it sill chooses the "other" rule. In french, 0 isn't grammatically different from 1, so i expected the system to use the "one" rule when providing 0 as quantity. – Teybeo Jan 07 '15 at 17:05
  • 1
    Well, in Spanish, Italian, German and English 0 goes to plural. Strangely enough, in French it's singular. I learned something, today. – Phantômaxx Jan 07 '15 at 17:28