Skip to content

Smart Strings

Smart Strings provide dynamic text formatting with variables, pluralization, gender agreement, and locale-aware formatting.

PatternExampleResult
PositionalHello {0}!Hello World!
NamedHello {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:

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

FormatExampleResult (en-US)Result (de-DE)
N0{0:N0}1,234,5671.234.567
N2{0:N2}1,234.561.234,56
C{0:C}$19.9919,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)
FormatDescriptionExample (en-US)
dShort date1/15/2025
DLong dateWednesday, 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"]

Professional Unity Development Tools