Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android plurals treatment of "zero"

Tags:

android

If have the following plural ressource in my strings.xml:

   <plurals name="item_shop">         <item quantity="zero">No item</item>         <item quantity="one">One item</item>         <item quantity="other">%d items</item>    </plurals>    

I'm showing the result to the user using:

textView.setText(getQuantityString(R.plurals.item_shop, quantity, quantity)); 

It's working well with 1 and above, but if quantity is 0 then I see "0 items". Is "zero" value supported only in Arabic language as the documentation seems to indicate? Or am I missing something?

like image 647
EricLarch Avatar asked Apr 13 '11 15:04

EricLarch


People also ask

What is plural in Android?

The plural form of android is androids.

What is@ string in Android studio?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

How to add special characters in string XML in Android?

How can I write character & in the strings. xml? In android studio, you can simply press Alt+Enter and it will convert for you.


1 Answers

The Android resource method of internationalisation is quite limited. I have had much better success using the standard java.text.MessageFormat.

Basically, all you have to do is use the standard string resource like this:

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

Then, from the code all you have to do is the following:

String fmt = getResources().getText(R.string.item_shop).toString(); textView.setText(MessageFormat.format(fmt, amount)); 

You can read more about the format strings in the javadocs for MessageFormat

like image 129
Elias Mårtenson Avatar answered Sep 22 '22 23:09

Elias Mårtenson