Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android translate string with variables

good morning,

i have an android app with a german and english language xml file. now i would like to set and text view like this:

"Hello User x, hello world y." "Hallo Benutzer x, hallo welt y."

for x and y i would like set an variable. how can i translate this text for both languages with dynamic variables ?

like image 206
Stack108 Avatar asked May 23 '16 07:05

Stack108


People also ask

How to add special characters in string XML in Android?

To include special characters inside XML files you must use the numeric character reference instead of that character. The numeric character reference must be UTF-8 because the supported encoding for XML files is defined in the prolog as encoding="UTF-8" and should not be changed.

What is@ string in Android studio?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

What is strings 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.


2 Answers

Your question seems unclear but from my understanding I am suggesting you the solution.

If you want to pass a dynamic argument to string you can do this by bellow example.

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

If you wish more look at: http://developer.android.com/intl/pt-br/guide/topics/resources/string-resource.html#FormattingAndStyling

like image 61
kunal Avatar answered Nov 08 '22 03:11

kunal


By default android considers English as primary language and loads the string resources from res ⇒ values ⇒ strings.xml. When you want to add support for another language, you need to create a values folder by appending an Hyphen and the ISO language code. For example if you want to add support for French, you should create a values folder named values-fr and keep a strings.xml file in it with all the strings translated into French language. enter image description here

like image 3
Devendra Singh Avatar answered Nov 08 '22 05:11

Devendra Singh