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

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 ?

Your code looks correct to me. Here's why:
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.
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.

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

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.

Related

Getting QuantityStrings to pick up on Zeros

I am using Quantity Strings to display the values chosen in Number Pickers that are in my custom Dialog.
Since my Number Pickers are the hours and minutes that I want to do a certain activity, the values chosen appear in a textview below the pickers.
On the hour picker, when I choose 0 initially, it reacts as defined in the XML file for a "zero" value, but subsequently, after having chosen other values for the hours, and then I choose 0 for the hours, it treats my choice as "other", not as "zero". Please advise!
My res/values/strings.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="hour_picker">
<item quantity="zero">"You want to walk for "</item>
<item quantity="one">"You want to walk for %1$d hour and "</item>
<item quantity="other">"You want to walk for %1$d hours and "</item>
</plurals>
<plurals name="minute_picker">
<item quantity="one"> %1$d minute</item>
<item quantity="other"> %1$d minutes</item>
</plurals>
</resources>
The .java file is as follows:
public class SelectDurationDialogFragment extends DialogFragment implements NumberPicker.OnValueChangeListener {
NumberPicker hour_picker,minute_picker;
TextView hour_txt,minute_txt;
...
#Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
switch (numberPicker.getId()){
case R.id.numberPicker1:
hour_txt.setText(String.format(getResources().getQuantityString((R.plurals.hour_picker),i1),i1));
break;
case R.id.numberPicker2:
minute_txt.setText(String.format(getResources().getQuantityString((R.plurals.minute_picker),i1),i1));
break;
}
}
From http://developer.android.com/guide/topics/resources/string-resource.html#Plurals:
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.
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.

Plural definition is ignored for zero quantity

I use plurals to compile a quantity string for an Android application. I follow exactly what one can find in the tutorials:
res.getQuantityString(
R.plurals.number_of_comments, commentsCount, commentsCount);
Here is the definition of the plurals:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="number_of_comments">
<item quantity="zero">No comments</item>
<item quantity="one">One comment</item>
<item quantity="other">%d comments</item>
</plurals>
</resources>
Interesting enough, the output string is odd to what I definied:
commentsCount = 0 => "0 comments"
commentsCount = 1 => "One comment"
commentsCount = 2 => "2 comments"
I guess this is because the docs state When the language requires special treatment of the number 0 (as in Arabic). for zero quantity. Is there any way to force my definition?
According to the documentation :
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).
If you still want to use a custom string for zero, you can load a different string when the quantity is zero :
if (commentsCount == 0)
str = res.getString(R.string.number_of_comments_zero);
else
str = res.getQuantityString(R.plurals.number_of_comments, commentsCount, commentsCount);
In Kotlin (thanks to Dalmas):
val result = commentsCount.takeIf { it != 0 }?.let {
resources.getQuantityString(R.plurals.number_of_comments, it, it)
} ?: resources.getString(R.string.number_of_comments_zero)
Plural is Unicode form. all of plural value here.
In English, plural for zero like 2, 3,4 so you must if else this value to use others string for this.

getQuantityString returns wrong string with 0 value

In an android app, I have the following string resources:
<plurals name="test">
<item quantity="zero">"I have 0 item"</item>
<item quantity="one">"I have 1 item"</item>
<item quantity="other">"I have several items"</item>
</plurals>
And the following line of code:
String text = getResources().getQuantityString(R.plurals.test, 0)
which I would expect to return
I have 0 item
But it actually returns
I have 1 item
Why ?
Quantity Strings are broken on some Plattforms and phones as the issue Tracker and this discussion "Should Plurals and Quantity Strings be used" points out. It depends on many factors which you cannot control (i.e. localization on the phone).
One solution can be to take an external library like this one, which mimes the same functionallity.
Another solution is stated in the documentation of plurals in android. Avoid using it and use "quantity-neutral" formulations like "Books: 1"
Change the code like this
String text = getResources().getQuantityString(R.plurals.test, 0,0);

Android plurals treatment of "zero"

If have the following plural ressource in my strings.xml:
<plurals name="item_shop">
<item quantity="zero">No item</item>
<item quantity="one">One item</item>
<item quantity="other">%d items</item>
</plurals>
I'm showing the result to the user using:
textView.setText(getQuantityString(R.plurals.item_shop, quantity, quantity));
It's working well with 1 and above, but if quantity is 0 then I see "0 items".
Is "zero" value supported only in Arabic language as the documentation seems to indicate?
Or am I missing something?
The Android resource method of internationalisation is quite limited. I have had much better success using the standard java.text.MessageFormat.
Basically, all you have to do is use the standard string resource like this:
<resources>
<string name="item_shop">{0,choice,0#No items|1#One item|1<{0} items}</string>
</resources>
Then, from the code all you have to do is the following:
String fmt = getResources().getText(R.string.item_shop).toString();
textView.setText(MessageFormat.format(fmt, amount));
You can read more about the format strings in the javadocs for MessageFormat
From http://developer.android.com/guide/topics/resources/string-resource.html#Plurals:
Note that the selection is made based on grammatical necessity. A string for zero in English 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). 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.
In conclusion, 'zero' is only used for certain languages (same goes for 'two' 'few' etc.) because the other languages do not have a special conjugation and therefore the 'zero' field is considered unnecessary
Here is a workaround I am using to handle this issue without switching to MessageFormat.
First I extract the "zero" string into its own string resource.
<string name="x_items_zero">No items.</string>
<plurals name="x_items">
<!-- NOTE: This "zero" value is never accessed but is kept here to show the intended usage of the "zero" string -->
<item quantity="zero">#string/x_items_zero</item>
<item quantity="one">One item.</item>
<item quantity="other">%d items.</item>
</plurals>
Then I have some convenience methods in my own ResourcesUtil
public static String getQuantityStringZero(Resources resources, int resId, int zeroResId, int quantity) {
if (quantity == 0) {
return resources.getString(zeroResId);
} else {
return resources.getQuantityString(resId, quantity, quantity);
}
}
public static String getQuantityStringZero(Resources resources, int resId, int zeroResId, int quantity, Object... formatArgs) {
if (quantity == 0) {
return resources.getString(zeroResId);
} else {
return resources.getQuantityString(resId, quantity, formatArgs);
}
}
Now anytime I want to use a specific string for quantity zero I call:
String pluralString = ResourcesUtil.getQuantityStringZero(
getContext().getResources(),
R.plural.x_items,
R.string.x_items_zero,
quantity
);
I wish there was something better but this at least gets the job done while keeping the string resource XML legible.
Android is using the CLDR plurals system, and this is just not how it works (so don't expect this to change).
The system is described here:
http://cldr.unicode.org/index/cldr-spec/plural-rules
In short, it's important to understand that "one" does not mean the number 1. Instead these keywords are categories, and the specific numbers n that belong to each category are defined by rules in the CLDR database:
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
While there appears to be no language which uses "zero" for anything other than 0, there are languages which assign 0 to "one". There are certainly plenty of cases where "two" contains other numbers than just 2.
If Android where to allow you to do what you intended, your applications could not be properly translated into any number of languages with more complex plural rules.
If you are using data binding you can work around this with something like:
<TextView ...
android:text="#{collection.size() > 0 ? #plurals/plural_str(collection.size(), collection.size()) : #string/zero_str}"/>
I've wrote a Kotlin extension to handle all the scenarios that I can think about.
I have zeroResId as optional, so that sometimes we want to handle the zero by displaying "No Items", rather than "0 Items".
English treats zero grammatically as plural.
The selection of which string to use is made solely based on
grammatical necessity.
In English, a string for zero is 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).
https://developer.android.com/guide/topics/resources/string-resource.html#Plurals
fun Context.getQuantityStringZero(
quantity: Int,
pluralResId: Int,
zeroResId: Int? = null
): String {
return if (zeroResId != null && quantity == 0) {
resources.getString(zeroResId)
} else {
resources.getQuantityString(pluralResId, quantity, quantity)
}
}
Android's implementation seems correct unlike iOS (see details here).
The correct way to implement this should be the same are in MessageFormat, which means that for non-grammatical categories, you should add explicit rules (using numbers instead of categories). A correct implementation could look like this:
<plurals name="item_shop">
<item quantity="0">No item</item>
<item quantity="1">One item</item>
<item quantity="one">%d item</item>
<item quantity="other">%d items</item>
</plurals>
See here you are using the number 0 instead of the plural category zero which does not apply to English.
Using MessageFormat, this would translate to this (you can test here):
{messageNumber, plural, =0 {No item.} =1 {One item.} one {# item.} other {# items.}}
In English the category one is equal to 1 so in the example one should never be used but this is not true for all languages, and when spelling out a number, you are better make sure that you know which plural rule applies for that language.

Android Pluralization not working, need help

I've been attempting to utilize the plurals resource with Android but have not had any luck.
Here is my resource file for my plurals:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<plurals name="meters">
<item quantity="one">1 meter</item>
<item quantity="other">
<xliff:g id="count">%d</xliff:g>
meters
</item>
</plurals>
<plurals name="degrees">
<item quantity="one">1 degree</item>
<item quantity="other">
<xliff:g id="count">%d</xliff:g>
degrees
</item>
</plurals>
</resources>
...and then here is the code I am using when I attempt to extract the quantity string from my resources:
Resources res = this.getResources();
tTemp.setText(res.getQuantityString(R.plurals.degrees, this.mObject.temp_c.intValue()));
...but the text in the TextView remains to be %d degrees and %d meters.
Does anyone know what is happening? I've debugged the code and the res.getQuantityString(...) call is returning a String whose value is %d degrees or %d meters. Though when the quantity happens to be 1 it does correctly evalute to 1 degree or 1 meter.
Thanks in advance for any help!
Regards, celestialorb.
It appears that you need to specify the count twice, the first is used to determine the string to use, and the second is the one that is replaced into the string. e.g.
Resources res = this.getResources();
int tv = this.mObject.temp_c.intValue();
tTemp.setText(res.getQuantityString(R.plurals.degrees, tv, tv));
And at least in my testing so far, the xliff:g elements in the resource aren't needed.
Android "supports" the use of plurals by use of R.plurals which is practically undocumented. Diving into the source code reveals that you should be able to have the following possible versions of a string:
"zero"
"one"
"few" (for exactly 2)
"other" (for 3 and above)
However, I've found that only "one" and "other" actually work (despite the others being used in the android source!).
To use plurals you want to declare you pluralizable strings in a similar way to normal string resources:
<resources>
<plurals name="match">
<!-- Case of one match -->
<item quantity="one">1 match</item>
<!-- Case of several matches -->
<item quantity="other">%d matches</item>
</plurals>
</resources>
Then to actually use them in code, use code similar to what superfell suggested above:
String text = getResources().getQuantityString(R.plurals.match, myIntValue, myIntValue);
myTextView.setText(text);
Same problem here! I guess it is just a flaw in the documentation. The "pure" getQuantitiyString(int, int) method does just get a text resource, without any formatting. As superfell stated: just use the getQuantityString(int, int, Object...) method and hand over your integer value twice.
I'd hoped this worked the same way you did, but it simply doesn't!!
PS: maybe check an answer as the correct one? ;-)

Categories

Resources