Plurals are a type of resource, we use it very often to deal with plurality of Strings, what does plurality of Strings means? Suppose you wants to denote the stock items and you want to print out the result, you will have to write at least 2 Strings like:
<string name="zero"> No Items on the Stall!</string>
<string name="one">Only One Item on the Stall</string>
<string name="moreThanOne">There are %d Items on the Stall</string>
and then you will add some logic to your code
if (ItemsCount ==0)
else if (ItemsCount ==1)
else if (ItemsCount >1)
result= getString(R.string.moreThanOne, ItemsCount);
It is not a best practice
to keep languages in logic inside our code, imagine you have many internationalized text and every languages have its own kind of formatting rules !
here plurals comes to solve this problem:
We are going to use “Plurals”
tag rather than “String”
<item quantity="one">Only %d Item</item>
<item quantity= "other">%d Items!</item>
Note that each language have limited quantity values based on grammar of each language, for English there are only two values of quantity “one”
and “other”, “zero” is not allowed in english, The plurals have the following types but it get differs to the languages.
When the language requires special treatment of the number 0 (as in Arabic).
When the language requires special treatment of numbers like one (as with the number 1 in English and most other languages; in Russian, any number ending in 1 but not ending in 11 is in this class).
When the language requires special treatment of numbers like two (as with 2 in Welsh, or 102 in Slovenian).
When the language requires special treatment of "small" numbers (as with 2, 3, and 4 in
Czech; or numbers ending 2, 3, or 4 but not 12, 13, or 14 in Polish).
When the language requires special treatment of "large" numbers (as with numbers ending 11-99 in Maltese).
When the language does not require special treatment of the given quantity (as with all
numbers in Chinese, or 42 in English).
List of Languages for Plurals Rules:
We are suppose to use getQuantityString method:
int ItemsCount= 20;
String result = getResources().getQuantityString(R.plurals.numberOfItems, ItemsCount,ItemsCount);
When using the getQuantityString() method, you need to pass the ItemsCount twice if your string includes string formatting with a number. For example
, for the string %d Items found, the first ItemsCount parameter selects the appropriate plural string and the second ItemsCount parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don't need to pass the third parameter to
Android Plurals Documentation: