How to use Android quantity strings (plurals)?

Andy picture Andy · Jan 31, 2017 · Viewed 24.1k times · Source

I am trying to use getQuantityString method in Resources to retrieve Quantity Strings (Plurals) based on android developer guidelines Quantity String (Plurals)

The error I am getting is

Error:(604) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
Error:(604) Found tag </item> where </plurals> is expected

when I set up plurals as below

<plurals name="productCount">
    <item quantity="one" formatted="true">%1$d of %2$d product</item>
    <item quantity="other" formatted="true">%1$d of %2$d products</item>
</plurals>

and trying to read it as below

productIndexCountText.setText(getResources().getQuantityString(R.plurals.productCount, position, size));

One workaround is to break the string up to use plural only for the last part of the string and concatenate the two parts. But I am trying to avoid doing that if possible.

Answer

LWChris picture LWChris · Mar 19, 2017

You don't need to set the "formatted" attribute for any of those items. When using quantity strings, there are only three possibilities:

  1. the resource string is plain text and does not contain any parameters
  2. the resource string contains only one parameter (most likely the quantity); use %d or whatever format you need
  3. the resource string contains multiple parameters; all parameters have to be explicitly accessed by their position, for example %1$d

As for the getQuantityString method, there are two overloads: one with only the ressource id and the quantity, and one with an additional Object... formatArgs parameter.

For case 1., you can use the getQuantityString(@PluralsRes int id, int quantity) method.

For all other cases, i. e. if you have any parameters, you need the getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs) overload. Note: all parameters have to be present in the param array. That means, if the resource string displays the quantity, the quantity variable will be passed twice to the function.

So if these are your resources

<resources>
    <plurals name="test0">
        <item quantity="one">Test ok</item>
        <item quantity="other">Tests ok</item>
    </plurals>
    <plurals name="test1">
        <item quantity="one">%d test ok</item>
        <item quantity="other">%d tests ok</item>
    </plurals>
    <plurals name="test2">
        <item quantity="one">%2$s: %1$d test ok</item>
        <item quantity="other">%2$s: %1$d tests ok</item>
    </plurals>
    <plurals name="test3">
        <item quantity="one">%3$s: %1$d test out of %2$d ok</item>
        <item quantity="other">%3$s: %1$d tests out of %2$d ok</item>
    </plurals>
</resources>

then the appropiate calls to getQuantityString are:

int success = 1;
int total = 10;
String group = "Group name";

getResources().getQuantityString(R.plurals.test0, success)
// Test ok
getResources().getQuantityString(R.plurals.test1, success, success)
// 1 test ok
getResources().getQuantityString(R.plurals.test2, success, success, group)
// Group name: 1 test ok
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// Group name: 1 test out of 10 ok

success = 5;
getResources().getQuantityString(R.plurals.test0, success)
// Tests ok
getResources().getQuantityString(R.plurals.test1, success, success)
// 5 tests ok
getResources().getQuantityString(R.plurals.test2, success, success, group)
// Group name: 5 tests ok
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// Group name: 5 tests out of 10 ok