Smart Strings
Smart Strings provide dynamic text formatting with variables, pluralization, gender agreement, and locale-aware formatting.
| Pattern | Example | Result |
|---|---|---|
| Positional | Hello {0}! | Hello World! |
| Named | Hello {name}! | Hello John! |
Positional Arguments:
csharp
// Template: "{0} scored {1} points"
string result = Localization.Get("score.message", "John", 100);
// Result: "John scored 100 points"Named Arguments (via Dictionary):
csharp
var args = new Dictionary<string, object>
{
{ "player", "John" },
{ "score", 100 }
};
string result = SmartStringParser.Parse("{player} scored {score} points", args);
// Result: "John scored 100 points"Syntax: {variable:plural:category=text|category=text}
Use # as placeholder for the number.
csharp
// Template: "{0:plural:one=# item|other=# items}"
SmartStringParser.Parse("{0:plural:one=# item|other=# items}", 1);
// Result: "1 item"
SmartStringParser.Parse("{0:plural:one=# item|other=# items}", 5);
// Result: "5 items"CLDR Plural Categories:
| Category | Used By |
|---|---|
zero | Arabic, Latvian, Welsh |
one | English, German, Spanish, French |
two | Arabic, Hebrew, Slovenian |
few | Russian, Czech, Polish |
many | Russian, Polish, Arabic |
other | All languages (required fallback) |
Complex Example (Russian):
{0:plural:one=# яблоко|few=# яблока|many=# яблок|other=# яблока}Syntax: {variable:male=text|female=text|other=text}
csharp
// Template: "{0:male=He|female=She|other=They} logged in"
SmartStringParser.Parse("{0:male=He|female=She|other=They} logged in", "female");
// Result: "She logged in"Syntax: {variable:true=text|false=text}
csharp
// Template: "Status: {0:true=Online|false=Offline}"
SmartStringParser.Parse("Status: {0:true=Online|false=Offline}", true);
// Result: "Status: Online"Uses .NET format specifiers with locale-aware formatting.
| Format | Example | Result (en-US) | Result (de-DE) |
|---|---|---|---|
N0 | {0:N0} | 1,234,567 | 1.234.567 |
N2 | {0:N2} | 1,234.56 | 1.234,56 |
C | {0:C} | $19.99 | 19,99 € |
P | {0:P} | 85.50% | 85,50 % |
csharp
// Template: "Score: {0:N0}"
SmartStringParser.Parse("Score: {0:N0}", 1234567);
// Result: "Score: 1,234,567"
// Template: "Price: {0:C}"
SmartStringParser.Parse("Price: {0:C}", 19.99m);
// Result: "Price: $19.99" (or locale equivalent)| Format | Description | Example (en-US) |
|---|---|---|
d | Short date | 1/15/2025 |
D | Long date | Wednesday, January 15, 2025 |
csharp
// Template: "Date: {0:d}"
SmartStringParser.Parse("Date: {0:d}", DateTime.Now);
// Result: "Date: 1/15/2025"Validate templates before deployment:
csharp
bool isValid = SmartStringParser.Validate(template, out string[] errors);
if (!isValid)
{
foreach (var error in errors)
{
Debug.LogError($"Template error: {error}");
}
}Get all variable names from a template:
csharp
string[] vars = SmartStringParser.ExtractVariables("{player} scored {score} points");
// Returns: ["player", "score"]