7

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 ?

3

2 Answers 2

9

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

1
  • Can I add those vars from xml layout rather than from java?
    – C0mpl3x
    Nov 15, 2021 at 9:23
0

"Hello User %1$s, hello world %2$s." "Hallo Benutzer %1$s, hallo welt %2$s." In code then simply call getString(R.string.your_string, yourName, yourWorld) where yourName and yourWorld are String variables.

If you need to know where to store the strings see the answer of Devendra Singh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.