I've faced a problem with quantity strings (plurals).
The manual says, I may provide quantity strings that are specific for each localization, and there are several common cases: "zero", "one", "two", "few", "many", and "other". I don't know if all possible cases for all languages in the world were covered; anyway, it is more than enough for Russian that I'm trying to make a localization for.
In Russian, numbers from 2 to 4 should be treated like "few" (the rule is actually more complex but I only need numbers below ten).
However, when I request a quantity string for 2, the system takes the "other" string. It doesn't take neither "two" string nor "few" one (I have provided them in my resources). If I removed the "other" string, I get the exception:
android.content.res.Resources$NotFoundException:
Plural resource ID #0x7f080000 quantity=2 item=other
I tried this both on emulator (Android 2.1) and on a real device (Android 2.3), the behaviour is the same wrong in both cases. Obviously, there is a mistake somewhere—the system does not recognize locale-specific quantities for my language. Could it be that Android has some bug here?
I believe this is currently broken in Android.
http://code.google.com/p/android/issues/detail?id=8287
Specifically, the following code in PluralRules.java shows that most languages only use the one
or other
strings, but Czech will use the few
string:
static final PluralRules ruleForLocale(Locale locale) {
String lang = locale.getLanguage();
if ("cs".equals(lang)) {
if (cs == null) cs = new cs();
return cs;
}
else {
if (en == null) en = new en();
return en;
}
}
private static PluralRules cs;
private static class cs extends PluralRules {
int quantityForNumber(int n) {
if (n == 1) {
return QUANTITY_ONE;
}
else if (n >= 2 && n <= 4) {
return QUANTITY_FEW;
}
else {
return QUANTITY_OTHER;
}
}
}
private static PluralRules en;
private static class en extends PluralRules {
int quantityForNumber(int n) {
if (n == 1) {
return QUANTITY_ONE;
}
else {
return QUANTITY_OTHER;
}
}
}
Thanks to Brigham who has pointed to the issue where the problem is explained; that explanation first rised more questions but now they seem to be solved. Indeed, the quantity strings don't work properly (at least prior to API 11, which is Android 3.x), so you have to use an alternative.
The solution for APIs prior to version 11 is mentioned in the comment 15 that contains a link to the project for alternative handling of quantity strings. That project is a program that simply does what the system was supposed to do. It can be converted to a library easily, so you just add it to your project, import the class and go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With