Skip to content

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

SyntaxExampleOutput
Positional{0}, {1}Value at index
Named{name}Value with key "name"

Pluralization

Syntax: {variable:plural:category=text|category=text}

CategoryLanguages
zeroArabic, Latvian, Welsh
oneEnglish, German, Spanish, French
twoArabic, Hebrew, Slovenian
fewRussian, Czech, Polish
manyRussian, Polish, Arabic
otherAll (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:

FormatDescriptionExample
N0Number, no decimals1,234
N2Number, 2 decimals1,234.56
CCurrency$19.99
PPercentage85.50%
F2Fixed-point3.14
csharp
SmartStringParser.Parse("Score: {0:N0}", 1234567);
// Result: "Score: 1,234,567"

Date Formatting

FormatDescriptionExample
dShort date1/15/2025
DLong dateWednesday, January 15, 2025
tShort time3:30 PM
TLong time3: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

  1. Always include other - Required fallback for all languages
  2. Validate templates - Catch syntax errors before deployment
  3. Test with edge cases - 0, 1, and large numbers
  4. Consider locale - Number/date formatting varies by locale

Professional Unity Development Tools