Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are parameters in strings.xml possible? [duplicate]

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.

like image 611
dhesse Avatar asked Mar 07 '10 19:03

dhesse


People also ask

What is the purpose of string xml?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.

Why are strings stored in xml?

Another reason for storing strings in xml is for localization. You can store different files for each different Locale or language, and Android will grab the correct file for the phone's selected Locale or language.

Where is the strings xml file located?

Android Resources Define strings Strings are typically stored in the resource file strings. xml .

How to add special characters in string xml in Android?

How can I write character & in the strings. xml? In android studio, you can simply press Alt+Enter and it will convert for you.


1 Answers

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> 
like image 196
Christopher Orr Avatar answered Oct 10 '22 05:10

Christopher Orr