Android Plural Strings with multiple Parameters

Andreas Gohr picture Andreas Gohr · Dec 21, 2015 · Viewed 7.1k times · Source

In Android strings, you can define plurals to handle translations depending on the actual number supplied to the string as described here. Strings also allow for specifying multiple positional parameters similar to what sprintf does in many languages.

However, consider the following string:

<resources>
    <string name="remaining">%1$d hours and %2$d minutes remaining.</string>
</resources>

It contains two numbers, how would I transform this to a plural in Android? All examples always work with a single parameter only. Is this even possible?

Answer

jj2005 picture jj2005 · May 4, 2016

Previous answer uses string concatenation which is incorrect from an i18n point of view. For the original string "%1$d hours and %2$d minutes remaining." using string concatenation would force the translation of "remaining" to the end which mightn't be appropriate for some languages.

My solution would be:

<resources>
     <string name="remaining">Time remaining: Hours:%1$d Minutes:%2$d.</string>
</resources>

Or perhaps with "Time remaining" as the heading.

This solution is mentioned in http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

It's often possible to avoid quantity strings by using quantity-neutral formulations such as "Books: 1"