Pluralization
This example shows how to handle pluralized messages correctly across different languages.
String Table Setup
| Key | English | German | Russian |
|---|---|---|---|
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: 1other: 0, 2, 3, 4, 5...
German (same as English)
one: 1other: 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: 0one: 1two: 2few: 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}");
}
}Ordinal Numbers
Added in v1.1.0
Ordinal numbers follow CLDR ordinal rules, which differ from cardinal rules.
English Ordinals
{0:ordinal:one=#st|two=#nd|few=#rd|other=#th}| Value | Category | Result |
|---|---|---|
| 1 | one | 1st |
| 2 | two | 2nd |
| 3 | few | 3rd |
| 4 | other | 4th |
| 11 | other | 11th |
| 12 | other | 12th |
| 13 | other | 13th |
| 21 | one | 21st |
| 22 | two | 22nd |
| 23 | few | 23rd |
French Ordinals
French only distinguishes 1st from all others:
{0:ordinal:one=#er|other=#e}| Value | Result |
|---|---|
| 1 | 1er |
| 2 | 2e |
| 10 | 10e |
German Ordinals
German ordinals are always the same form:
{0:ordinal:other=#.}All values produce 1., 2., 3., etc.
Combined Example
csharp
// Leaderboard: "1st place with 5 points"
string template = "{0:ordinal:one=#st|two=#nd|few=#rd|other=#th} place with {1:plural:one=# point|other=# points}";
SmartStringParser.Parse(template, 1, 5);
// Result: "1st place with 5 points"Best Practices
- Always include
other- It's the required fallback - Test with Russian - It exercises most plural categories
- Use
#placeholder - Automatically inserts the number - Consider zero - "No items" often reads better than "0 items"
- Provide translator notes - Explain the context for each plural form
