Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: I18n with parameters

I know there is support in Android for 18n an application, but can I give parameters to such a string? In Rails, I can do something like this:

en:
  hello: "Hello %{name}! You've got %{count} messages."

t("hello", name: "Klaus", count: 5)

Is there something similar in Android or do I have to do it myself?

like image 339
iGEL Avatar asked Jan 25 '12 15:01

iGEL


2 Answers

To elaborate on Heiko's answer, and to show your specific example, if you want to have more than one string you need to number them:

<string name="hello">Hello %1$s! You've got %2$d messages.</string>

This way you can switch the order of the strings in each translation. Using it would be:

String hello = getString(R.strings.hello, "Klaus", 5);
like image 173
Mark Gjøl Avatar answered Nov 16 '22 04:11

Mark Gjøl


You can do the same

In strings.xml you can put

<string name="xyz">Do you really want to report [%s] as spammer?</string>

and then in your code you put

String foo = getString(R.strings.xyz,"Joe Doe");

See Context.getString()

like image 42
Heiko Rupp Avatar answered Nov 16 '22 04:11

Heiko Rupp