Reputation: 17392
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?
Upvotes: 10
Views: 3100
Reputation: 266
Phrase seems to do exactly what you're looking for. Here's a article about it's implementation. Below sample from the article.
<string name="greeting"> Hello {name}, today\'s cook yielded {yield} {unit}.</string>
and then...
// Call put(...) in any order
CharSequence greeting = Phrase.from(context, R.string.greeting)
.put("unit", unit)
.put("name", name)
.put("yield", yield)
.format();
Upvotes: 0
Reputation: 1882
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);
Upvotes: 13
Reputation: 30934
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");
Upvotes: 4