android translate string with variables
Asked Answered
B

3

8

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 ?

Bischoff answered 23/5, 2016 at 7:11 Comment(3)
link NOT for translation developer.android.com/guide/topics/resources/localization.html but if you want to change language of text shown in form inputs use thisWoodsum
developer.android.com/training/basics/supporting-devices/…Hermosillo
if u have fixed number of words to be translated. make a java.util.Map<String, String> where you can fetch german value for a given english keyWoodsum
F
12

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

Floreneflorentia answered 23/5, 2016 at 7:27 Comment(1)
Can I add those vars from xml layout rather than from java?Metaphosphate
T
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.

Thrombus answered 23/5, 2016 at 7:31 Comment(0)
E
-1

This is the easiest way in google translation.

[Just copy the strings from English resource and go to google translation][1]r.com/UQBXd.png

Essy answered 18/7, 2023 at 12:13 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Fiorenze

© 2022 - 2024 — McMap. All rights reserved.