SmartStringParser Class
Utility class for parsing and formatting Smart Strings with variables, pluralization, gender, and conditionals.
Namespace: Lexis
Methods
Parse
csharp
public static string Parse(string template, params object[] args);
public static string Parse(string template, Dictionary<string, object> args);Parse a Smart String template with arguments.
Positional Arguments:
csharp
string result = SmartStringParser.Parse("{0} scored {1} points", "John", 100);
// Result: "John scored 100 points"Named Arguments:
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"Validate
csharp
public static bool Validate(string template, out string[] errors);Validate a Smart String template for syntax errors.
Example:
csharp
if (!SmartStringParser.Validate(template, out string[] errors))
{
foreach (var error in errors)
{
Debug.LogError($"Template error: {error}");
}
}ExtractVariables
csharp
public static string[] ExtractVariables(string template);Get all variable names from a template.
Example:
csharp
string[] vars = SmartStringParser.ExtractVariables("{player} scored {score} points");
// Returns: ["player", "score"]Syntax Reference
Variable Substitution
| Syntax | Example | Output |
|---|---|---|
| Positional | {0}, {1} | Value at index |
| Named | {name} | Value with key "name" |
Pluralization
Syntax: {variable:plural:category=text|category=text}
| Category | Languages |
|---|---|
zero | Arabic, Latvian, Welsh |
one | English, German, Spanish, French |
two | Arabic, Hebrew, Slovenian |
few | Russian, Czech, Polish |
many | Russian, Polish, Arabic |
other | All (required fallback) |
Use # as placeholder for the number:
csharp
SmartStringParser.Parse("{0:plural:one=# item|other=# items}", 5);
// Result: "5 items"Gender Agreement
Syntax: {variable:male=text|female=text|other=text}
csharp
SmartStringParser.Parse("{0:male=He|female=She|other=They} logged in", "female");
// Result: "She logged in"Conditional
Syntax: {variable:true=text|false=text}
csharp
SmartStringParser.Parse("Status: {0:true=Online|false=Offline}", true);
// Result: "Status: Online"Number Formatting
Uses .NET format specifiers:
| Format | Description | Example |
|---|---|---|
N0 | Number, no decimals | 1,234 |
N2 | Number, 2 decimals | 1,234.56 |
C | Currency | $19.99 |
P | Percentage | 85.50% |
F2 | Fixed-point | 3.14 |
csharp
SmartStringParser.Parse("Score: {0:N0}", 1234567);
// Result: "Score: 1,234,567"Date Formatting
| Format | Description | Example |
|---|---|---|
d | Short date | 1/15/2025 |
D | Long date | Wednesday, January 15, 2025 |
t | Short time | 3:30 PM |
T | Long time | 3:30:00 PM |
csharp
SmartStringParser.Parse("Date: {0:D}", DateTime.Now);Complex Examples
Pluralization with Russian
{0:plural:one=# яблоко|few=# яблока|many=# яблок|other=# яблока}Combined Formatting
csharp
string template = "{player} has {count:plural:one=# coin|other=# coins} worth {value:C}";
var args = new Dictionary<string, object>
{
{ "player", "Alice" },
{ "count", 42 },
{ "value", 100.50m }
};
string result = SmartStringParser.Parse(template, args);
// Result: "Alice has 42 coins worth $100.50"Best Practices
- Always include
other- Required fallback for all languages - Validate templates - Catch syntax errors before deployment
- Test with edge cases - 0, 1, and large numbers
- Consider locale - Number/date formatting varies by locale
