Thanks to the answers I am writing 2 Android-based solutions. The first one I used was plural. At first glance, when checking Plurals documents / examples, you might think that there is a number = "several" (for 2-4 plurals) that check sources only works for locale 'cs'. For other locales only "one" and "another" work. So, in your strings.xml files:
<plurals name ="years"> <item quantity="one">1 year</item> <item quantity="other"><xliff:g id="number">%d</xliff:g> years</item> </plurals>
So, for polishing I would:
<plurals name ="years"> <item quantity="one">1 rok</item> <item quantity="other"><xliff:g id="number">%d</xliff:g> lat</item> </plurals>
Then I would have the code:
int n = getYears(...); if (Locale.getDefault().getLanguage().equalsIgnoreCase("pl") && n >= 2 && n <= 4) { return getString(R.string.years_pl, n); } else { return getResources().getQuantityString(R.plurals.years, n, n); }
And in my strings.xml file to localize polish, I would add the missing line:
<string name="years_pl"><xliff:g id="number">%d</xliff:g> lata</string>
My second solution has a plural element for English, Spanish, and other languages that have too many plural variations. Then for other languages that have such changes, I would use ChoiceFormat. So in my code:
... private static final int LANG_PL = 0; // add more languages here if needed ... String formats[] = { "{0,number} {0,choice,1#" + getString(R.string.year_1) + "|2#" + getString(R.string.years_2_4) + "|4<" + getString(R.string.years_lots) +"}", // polish // more to come }; ... // Here I would add more for certain languages if (Locale.getDefault().getLanguage().equalsIgnoreCase("pl")) { return MessageFormat.format(formats[LANG_PL], n); } else { return getResources().getQuantityString(R.plurals.years, n, n); }
I don't know if these methods are the best, but for now, or until Google does something better, this works for me.