Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In French, plural system treats 0 as "other" instead of "one"

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 ?

like image 891
Teybeo Avatar asked Jan 07 '15 16:01

Teybeo


People also ask

Is 0 plural in French?

In french, 0 isn't grammatically different from 1, so i expected the system to use the "one" rule when providing 0 as quantity. Well, in Spanish, Italian, German and English 0 goes to plural. Strangely enough, in French it's singular.

What is the French word for plural?

plural → pluriel, plural.

How do you do plurals in Android?

Multiple quantities in one string If your display text has multiple quantities, e. g. %d match(es) found in %d file(s). , split it into three separate resources: %d match(es) ( plurals item) %d file(s) ( plurals item)


2 Answers

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.

like image 91
Roland Illig Avatar answered Oct 11 '22 17:10

Roland Illig


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

}
like image 42
denis_lor Avatar answered Oct 11 '22 17:10

denis_lor