Android plurals


http://stackoverflow.com/questions/5651902/android-plurals-treatment-of-zero

https://code.google.com/p/android/issues/detail?id=8287

plurals

Android中plurals表示单复数,但是对于0是不起作用的,比如

  <plurals name="leaderboard_community_hero_free">
    <item quantity="zero">Free</item>
    <item quantity="other">Free(%d)</item>
  </plurals>

  getQuantityString(R.plurals.leaderboard_community_hero_free, quantity, quantity)


对于quantity为0,结果是不正确的。解决方式是:

<string name="leaderboard_community_hero_free">{0,choice,0#Free|0<Free({0})}</string>
MessageFormat.format(getSherlockActivity().getString(R.string.leaderboard_community_hero_free), number)


测试例子:

String fmt = "{0,choice,0#No items|1#One item|1<{0} items}";
System.out.println(MessageFormat.format(fmt, 0));
System.out.println(MessageFormat.format(fmt, 1));
System.out.println(MessageFormat.format(fmt, 2));
System.out.println(MessageFormat.format(fmt, 6));
System.out.println("=============");
fmt = "{0,choice,0#items|0<item({0})}";
System.out.println(MessageFormat.format(fmt, 0));
System.out.println(MessageFormat.format(fmt, 1));
System.out.println(MessageFormat.format(fmt, 2));
System.out.println(MessageFormat.format(fmt, 6));
System.out.println("=============");
ChoiceFormat form = new ChoiceFormat(
      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
System.out.println(form.format(0));
System.out.println(form.format(1));
System.out.println(form.format(2));
System.out.println(form.format(3));
System.out.println("=============");
fmt = "0#No items|1< {0} items";
form = new ChoiceFormat(fmt);
System.out.println(form.format(0));
System.out.println(form.format(1));
System.out.println(form.format(2));

输出结果:

No items
One item
2 items
6 items
=============
items
item(1)
item(2)
item(6)
=============
is zero or fraction 
is one 
is two 
is more than 2.
=============
No items
No items
 {0} items


文档

http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html

http://docs.oracle.com/javase/6/docs/api/java/text/ChoiceFormat.html


更多的例子

Here is a more complex example, with a pattern format:

 double[] filelimits = {0,1,2};
 String[] filepart = {"are no files","is one file","are {2} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
 MessageFormat pattform = new MessageFormat("There {0} on {1}");
 pattform.setFormats(testFormats);
 Object[] testArgs = {null, "ADisk", null};
 for (int i = 0; i < 4; ++i) {
     testArgs[0] = new Integer(i);
     testArgs[2] = testArgs[0];
     System.out.println(pattform.format(testArgs));
 }
 

Specifying a pattern for ChoiceFormat objects is fairly straightforward. For example:

 ChoiceFormat fmt = new ChoiceFormat(
      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
 System.out.println("Formatter Pattern : " + fmt.toPattern());

 System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
 System.out.println("Format with -1.0 : " + fmt.format(-1.0));
 System.out.println("Format with 0 : " + fmt.format(0));
 System.out.println("Format with 0.9 : " + fmt.format(0.9));
 System.out.println("Format with 1.0 : " + fmt.format(1));
 System.out.println("Format with 1.5 : " + fmt.format(1.5));
 System.out.println("Format with 2 : " + fmt.format(2));
 System.out.println("Format with 2.1 : " + fmt.format(2.1));
 System.out.println("Format with NaN : " + fmt.format(Double.NaN));
 System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
 
And the output result would be like the following:
  
  
  
  
Format with -INF : is negative Format with -1.0 : is negative Format with 0 : is zero or fraction Format with 0.9 : is zero or fraction Format with 1.0 : is one Format with 1.5 : is 1+ Format with 2 : is two Format with 2.1 : is more than 2. Format with NaN : is negative Format with +INF : is more than 2.










你可能感兴趣的:(Android plurals)