Pluralize Values

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to use string resources to deal with the different ways languages handle plural values.

Recipe

  • Create a new Xamarin.Android project called Plurals.
  • Modify Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <Button
      android:id="@+id/MyButton"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Hello"
    />
</LinearLayout>
  • Edit the file Resource/Strings/values.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <plurals name="numberOfSongsAvailable">
    <item quantity="one">One song found.</item>
    <item quantity="other">%d songs found.</item>
  </plurals>
</resources>
  • Modify the OnCreate method Activity1.cs:
protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    var button = FindViewById<Button>(Resource.Id.MyButton);
    button.Click += delegate
                       {
                           count++;
                            var buttonText = Resources.GetQuantityString(Resource.Plurals.numberOfSongsAvailable, count, count);
                            button.Text = buttonText;
                        };
}

Because the strings for quantities have formatting elements in them, we need to pass the value of count in twice. The first parameter is what Android will use to select the correct quantity string. The second value of count is the actual value that will be provided.

  • Run the application:

  • Add a new resource file for Polish, Layout/values-pl/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <plurals name="numberOfSongsAvailable">
    <item quantity="one">Znaleziono jedną piosenkę.</item>
    <item quantity="few">Znaleziono %d piosenki.</item>
    <item quantity="other">Znaleziono %d piosenek.</item>
  </plurals>
</resources>
  • Change the locale to Polish (Polski) (Settings > Language & Input).
  • Run the application:

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.