Skip to content

Pluralization

This example shows how to handle pluralized messages correctly across different languages.

String Table Setup

KeyEnglishGermanRussian
inventory.items{0:plural:one=# item|other=# items}{0:plural:one=# Gegenstand|other=# Gegenstände}{0:plural:one=# предмет|few=# предмета|many=# предметов|other=# предмета}

Basic Usage

csharp
using Lexis;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public void UpdateItemCount(int count)
    {
        string text = Localization.Get("inventory.items", count);
        itemCountLabel.text = text;

        // Results:
        // count = 1: "1 item" / "1 Gegenstand" / "1 предмет"
        // count = 5: "5 items" / "5 Gegenstände" / "5 предметов"
    }
}

CLDR Plural Categories

Different languages use different plural categories:

English (simple)

  • one: 1
  • other: 0, 2, 3, 4, 5...

German (same as English)

  • one: 1
  • other: 0, 2, 3, 4, 5...

Russian (complex)

  • one: 1, 21, 31, 41...
  • few: 2-4, 22-24, 32-34...
  • many: 0, 5-20, 25-30...
  • other: fractional numbers

Arabic (most complex)

  • zero: 0
  • one: 1
  • two: 2
  • few: 3-10, 103-110...
  • many: 11-99, 111-199...
  • other: 100-102, 200-202...

Common Patterns

Simple Singular/Plural

{0:plural:one=# message|other=# messages}

With Text Before/After

You have {0:plural:one=# new notification|other=# new notifications}!

Zero Special Case

{0:plural:zero=No items|one=# item|other=# items}

Full Example

csharp
using Lexis;
using UnityEngine;

public class NotificationUI : MonoBehaviour
{
    [SerializeField] private LocalizedString messageFormat;
    // Template: "You have {0:plural:zero=no new messages|one=# new message|other=# new messages}"

    public void UpdateNotifications(int count)
    {
        notificationLabel.text = messageFormat.GetValue(count);
    }
}

Testing Pluralization

Test with values that trigger each category:

csharp
void TestPluralization()
{
    // Test each category for the current locale
    int[] testValues = { 0, 1, 2, 5, 11, 21, 22, 25, 100 };

    foreach (int value in testValues)
    {
        string result = Localization.Get("inventory.items", value);
        Debug.Log($"Count {value}: {result}");
    }
}

Best Practices

  1. Always include other - It's the required fallback
  2. Test with Russian - It exercises most plural categories
  3. Use # placeholder - Automatically inserts the number
  4. Consider zero - "No items" often reads better than "0 items"
  5. Provide translator notes - Explain the context for each plural form

Professional Unity Development Tools