Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Russian quantity strings work properly?

Tags:

android

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?

like image 320
Alexander Dunaev Avatar asked Oct 05 '12 17:10

Alexander Dunaev


2 Answers

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;
        }
    }
}
like image 111
Brigham Avatar answered Sep 22 '22 12:09

Brigham


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.

like image 45
Alexander Dunaev Avatar answered Sep 23 '22 12:09

Alexander Dunaev