Snippet: Android Plurals form
Android supports Plurals. Plurals are XML based resources which allow to handle different quantities.
Here you can find a simple implemetation.
Plurals can be placed in any xml-file in the values directory, but usually, it will be placed in strings.xml.
If your plural strings do not include string formatting, you don't need to pass the third parameter.
Here a simple code:
Selection works fine with one and other but not with zero and two.
Why?? It is not a issue.
Android is using the CLDR plurals system. What is CLDR? Here you can find how the system works.
The possible quantifiers are limited to these:
It's important to understand that "one" does not mean the number 1.
The specific numbers n are keywords associated with categories.These associations are defined by rules in the CLDR database.
These categories are only mnemonics -- the names don't necessarily imply the exact contents of the category. For example, for both English and French the number 1 has the category one (singular). In English, every other number has a plural form, and is given the category other.....
With debugger you can see what happens in
You can read in official doc:
The selection of which string to use is made solely based on grammatical necessity. In English, a string for zero will be ignored even if the quantity is 0, because 0 isn't grammatically different from 2, or any other number except 1 ("zero books", "one book", "two books", and so on).
Here you can find technical doc about PluralRules.
You can get code from GitHub:
Here you can find a simple implemetation.
Plurals can be placed in any xml-file in the values directory, but usually, it will be placed in strings.xml.
To get the plurals from code, you can use the- No books
- One book
- Two books
- Few books
- Many books
- %d books
getQuantityString(int id, int quantity)
.
Resources res = getResources(); String book = res.getQuantityString(R.plurals.books, count, count);The call to getQuantityString() takes the id of the plural, the quantity used to select the appropriate plural, and optionally any arguments used for substitution in the display string.
If your plural strings do not include string formatting, you don't need to pass the third parameter.
Here a simple code:
public class PluralFormsActivity extends SherlockActivity { private void retrivePlurals() { TextView tw = (TextView) findViewById(R.id.text_xml); if (tw != null) { Resources res = getResources(); String books0 = res.getQuantityString(R.plurals.books, 0); String books1 = res.getQuantityString(R.plurals.books, 1); String books2 = res.getQuantityString(R.plurals.books, 2); String booksN = res.getQuantityString(R.plurals.books, 10, 10); StringBuffer sb = new StringBuffer(); sb.append(books0); sb.append("\n"); sb.append(books1); sb.append("\n"); sb.append(books2); sb.append("\n"); sb.append(booksN); sb.append("\n"); tw.setText(sb.toString()); } }Now, if you try this code, you will find that PluralRules, in english or italian, does not handle quantity "zero".
Selection works fine with one and other but not with zero and two.
Why?? It is not a issue.
Android is using the CLDR plurals system. What is CLDR? Here you can find how the system works.
The possible quantifiers are limited to these:
- zero
- one
- two
- few
- many
- other
It's important to understand that "one" does not mean the number 1.
The specific numbers n are keywords associated with categories.These associations are defined by rules in the CLDR database.
These categories are only mnemonics -- the names don't necessarily imply the exact contents of the category. For example, for both English and French the number 1 has the category one (singular). In English, every other number has a plural form, and is given the category other.....
With debugger you can see what happens in
String books0 = res.getQuantityString(R.plurals.books, 0);
with English Locale.You can read in official doc:
The selection of which string to use is made solely based on grammatical necessity. In English, a string for zero will be ignored even if the quantity is 0, because 0 isn't grammatically different from 2, or any other number except 1 ("zero books", "one book", "two books", and so on).
Here you can find technical doc about PluralRules.
You can get code from GitHub:
Comments
Post a Comment