Search code examples
androiddynamicparametersstringoption-type

Are parameters in strings.xml possible?


I'm going to implement my strings with internationalization in my Android app. I have a problem with the grammar and the way sentences build in different languages.

For example:

"5 minutes ago" - English

"vor 5 Minuten" - German

Can I do something like the following in strings.xml?

<string name="timeFormat">{0} minutes ago</string>

And then some magic like

getString(R.id.timeFormat, dynamicTimeValue)

This behaviour would solve the other problem with different word orders as well.


Solution

  • Yes, just format your strings in the standard String.format() way.

    See the method Context.getString(int, Object...) and the Android or Java Formatter documentation.

    In your case, the string definition would be:

    <string name="timeFormat">%1$d minutes ago</string>