string - Plurals not working as intended in android -


i using plural strings provided android-sdk. have used following code create plural string:

 <plurals name="valuestr">         <item quantity="zero">choose value.</item>         <item quantity="one">%d unit.</item>         <item quantity="other">%d units.</item>     </plurals> 

java code:

textview.settext(getresources().getquantitystring(r.plurals.valuestr,0,0)); 

when setting value other '0', working fine when setting '0' showing '0 unit.'.

please help!

update

while searching more on internet came across workaround uses java.text.messageformat class:

<resources>     <string name="item_shop">{0,choice,0#no items|1#one item|1&lt;{0} items}</string> </resources> 

then, code have following:

string fmt = resources.gettext(r.string.item_shop); textview.settext(messageformat.format(fmt, amount)); 

you can read more format strings in javadocs messageformat

a post made on g+ this. in short, because not pick closest match integer ( 0 = zero), because best grammatical pick.

in example, use units. correct usage be; 0 units 1 unit 2 units

making, 0 equal pretty other quantity above 1

read full story here; https://plus.google.com/116539451797396019960/posts/vycxa1jugno


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -