Dynamic Text with Variables
This example shows how to create dynamic text with player data using format arguments.
String Table Setup
| Key | English | German |
|---|---|---|
welcome.message | Welcome, {0}! | Willkommen, {0}! |
score.format | Score: | Punkte: |
level.format | Level | Stufe |
time.remaining | Time: {0:mm}: | Zeit: {0:mm}: |
Basic Usage
csharp
using Lexis;
using UnityEngine;
public class PlayerHUD : MonoBehaviour
{
[SerializeField] private LocalizedString welcomeFormat;
[SerializeField] private LocalizedString scoreFormat;
[SerializeField] private LocalizedString levelFormat;
public void UpdateHUD(string playerName, int score, int level)
{
// Using GetValue with format arguments
welcomeLabel.text = welcomeFormat.GetValue(playerName);
scoreLabel.text = scoreFormat.GetValue(score);
levelLabel.text = levelFormat.GetValue(level);
}
}Using Localization.Get Directly
csharp
// Positional arguments
string welcome = Localization.Get("welcome.message", playerName);
string score = Localization.Get("score.format", currentScore);
// Multiple arguments
string stats = Localization.Get("player.stats", playerName, level, score);
// Template: "{0} is level {1} with {2:N0} points"Number Formatting
Number formatting is locale-aware:
| Format | en-US | de-DE |
|---|---|---|
{0:N0} | 1,234,567 | 1.234.567 |
{0:N2} | 1,234.56 | 1.234,56 |
{0:C} | $19.99 | 19,99 € |
csharp
// Score with thousands separator
string score = Localization.Get("score.format", 1234567);
// English: "Score: 1,234,567"
// German: "Punkte: 1.234.567"
// Price with currency
string price = Localization.Get("price.format", 19.99m);
// English: "$19.99"
// German: "19,99 €"Date and Time
csharp
// Time remaining
TimeSpan remaining = TimeSpan.FromSeconds(125);
string time = Localization.Get("time.remaining", remaining);
// Result: "Time: 02:05"
// Date formatting
DateTime eventDate = new DateTime(2025, 12, 25);
string date = Localization.Get("event.date", eventDate);
// Template: "Event on {0:d}"
// en-US: "Event on 12/25/2025"
// de-DE: "Event on 25.12.2025"Using LocalizedText Component
csharp
public class DynamicHUD : MonoBehaviour
{
[SerializeField] private LocalizedText welcomeText;
[SerializeField] private LocalizedText scoreText;
public void SetPlayer(string name)
{
welcomeText.SetArguments(name);
}
public void UpdateScore(int score)
{
scoreText.SetArguments(score);
}
}Complex Example
csharp
using Lexis;
using UnityEngine;
public class GameUI : MonoBehaviour
{
void UpdateStats(PlayerData player)
{
// Multiple format arguments
// Template: "{0} has collected {1:N0} coins in {2:F1} minutes"
statsLabel.text = Localization.Get("stats.summary",
player.Name,
player.Coins,
player.PlayTime.TotalMinutes);
// With percentage
// Template: "Progress: {0:P0}"
progressLabel.text = Localization.Get("progress.format",
player.CompletionRatio);
// Result: "Progress: 75%"
}
}