Are parameters in strings.xml possible?

dhesse picture dhesse · Mar 7, 2010 · Viewed 159k times · Source

In my Android app I'am going to implement my strings with internationalization. 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.

Answer

Christopher Orr picture Christopher Orr · Mar 7, 2010

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>