Android quantity strings don't cover pluralization for zero well[0]. We think ExtendedMessageFormat[1] or MessageFormat[2] or a similar library may work better. This task covers the work of choosing a library, adding it to the project, and if possible, salvaging the existing translations to the new format.
It's tempting to try to write this logic ourselves, but consider the following. Android defines these quantities: zero, one, two, few, many, and other. As an alternative to quantity strings, we could write these out as simple strings individually. The English case looks like:
view_continue_reading_card_subtitle_zero = "today" view_continue_reading_card_subtitle_one = "yesterday" view_continue_reading_card_subtitle_other = "%d days ago"
But, assuming Android got it right, this isn't enough for all languages so actually it also has*:
view_continue_reading_card_subtitle_two = @string/view_continue_reading_card_subtitle_other view_continue_reading_card_subtitle_few = @string/view_continue_reading_card_subtitle_other view_continue_reading_card_subtitle_many = @string/view_continue_reading_card_subtitle_other
*I don't think translatewiki supports Android string references so these would be copied and pasted which is ok but will need to be done for every language (no defaults for plurals).
But we don't know what few or many in other languages so we also need:
quantity_few = 3 quantity_many = 6
Then I guess we write a switch:
@StringRes int getQuantityStringRes(@IntRange(from=0) int quantity) { if (quantity == 0) { return view_continue_reading_card_subtitle_zero } if (quantity == 1) { return view_continue_reading_card_subtitle_one } if (quantity == 2) { return view_continue_reading_card_subtitle_two } int few = res.getInt(R.int.quantity_few) if (quantity <= few) { return view_continue_reading_card_subtitle_few } int many = res.getInt(R.int.quantity_many) if (quantity <= many) { return view_continue_reading_card_subtitle_many } return view_continue_reading_card_subtitle_other }
And use it like so:
String msg = getString(getQuantityStringRes(Math.abs(quantity)), quantity)
This seems like it would work but imagine all the corner cases it probably misses. I think we'd be better forking the platform code as a library and adding logic to use the zero case when the quantity was zero and a zero string existed but maybe this breaks some other language? This really has to be someone else's (some other library's) problem :]
[0] https://developer.android.com/guide/topics/resources/string-resource.html#Plurals
[1] https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/org/apache/commons/lang3/text/ExtendedMessageFormat.html
[2] https://stackoverflow.com/a/5671704