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:
and then you will add some logic to your code
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” tag
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.
Zero When the language requires special treatment of the number 0 (as in Arabic).
One 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).
Two When the language requires special treatment of numbers like two (as with 2 in Welsh, or 102 in Slovenian).
Few 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).
Many When the language requires special treatment of "large" numbers (as with numbers ending 11-99 in Maltese).
Other 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:
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
We are suppose to use getQuantityString method:
Output:
“20 Items!”
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
getQuantityString.
Android Plurals Documentation:
http://developer.android.com/guide/topics/resources/string-resource.html#Plurals
Enjoy!!!!!!!!
1 2 3 4 5 | <resources> <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> </resources> |
and then you will add some logic to your code
1 2 3 4 5 6 7 | if (ItemsCount ==0) result= getString(R.string.zero); else if (ItemsCount ==1) result= getString(R.string.one); else if (ItemsCount >1) result= getString(R.string.moreThanOne, ItemsCount); textView.setText(result); |
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” tag
1 2 3 4 5 6 | <resources> <plurals name="numberOfItems"> <item quantity="one">Only %d Item</item> <item quantity= "other">%d Items!</item> </plurals> </resources> |
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.
Zero When the language requires special treatment of the number 0 (as in Arabic).
One 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).
Two When the language requires special treatment of numbers like two (as with 2 in Welsh, or 102 in Slovenian).
Few 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).
Many When the language requires special treatment of "large" numbers (as with numbers ending 11-99 in Maltese).
Other 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:
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
We are suppose to use getQuantityString method:
1 2 3 | int ItemsCount= 20; String result = getResources().getQuantityString(R.plurals.numberOfItems, ItemsCount,ItemsCount); textView.setText(result); |
Output:
“20 Items!”
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
getQuantityString.
Android Plurals Documentation:
http://developer.android.com/guide/topics/resources/string-resource.html#Plurals
Enjoy!!!!!!!!
No comments:
Post a Comment