Reputation: 109
Is there any way to translate plurals in Android?
My app pushes notifications like "The task will expire in X minutes", but i need to do this in a few languages.
Upvotes: 6
Views: 3457
Reputation: 234857
Should be no problem. Just define the plurals in language-specific resource folders (e.g., res/values-es/plurals.xml
). The Android documentation on plurals shows exactly this kind of thing:
res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numberOfSongsAvailable">
<!--
As a developer, you should always supply "one" and "other"
strings. Your translators will know which strings are actually
needed for their language. Always include %d in "one" because
translators will need to use %d for languages where "one"
doesn't mean 1 (as explained above).
-->
<item quantity="one">%d song found.</item>
<item quantity="other">%d songs found.</item>
</plurals>
</resources>
res/values-pl/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="numberOfSongsAvailable">
<item quantity="one">Znaleziono %d piosenkę.</item>
<item quantity="few">Znaleziono %d piosenki.</item>
<item quantity="other">Znaleziono %d piosenek.</item>
</plurals>
</resources>
In code, you would use it like this:
int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);
Upvotes: 13