Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can plurals be translated in Android?

Tags:

android

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.

like image 784
Miguel Ortega Avatar asked Jan 11 '16 00:01

Miguel Ortega


People also ask

How do you handle plurals in Android?

Multiple quantities in one string If your display text has multiple quantities, e. g. %d match(es) found in %d file(s). , split it into three separate resources: %d match(es) ( plurals item) %d file(s) ( plurals item)

What are plurals in Android?

Note: A plurals collection is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine plurals resources with other simple resources in the one XML file, under one <resources> element.

What is plural string android?

This enables you to define different strings based on the number of items you refer to. For example, in English you would refer to "one Android" but "seven Androids". By creating a plurals resource, you can specify an alternative string for any of zero, one, multiple, few, many, or other quantities.


1 Answers

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);
like image 168
Ted Hopp Avatar answered Sep 21 '22 10:09

Ted Hopp