Display localized and dynamic text from scripts and components.
A LocalizedString references one entry in a table collection and provides its value for the selected locale. Add it as a serialized field, pick the entry in the Inspector window, and subscribe to its value.
Before you start, make sure you have the following:
Add a LocalizedString field to a component:
public class ScoreDisplay : MonoBehaviour
{
public LocalizedString scoreText = new();
IntVariable m_Score = new();
void OnEnable()
{
scoreText.LocalVariables.Add(m_Score, "score");
scoreText.StringChanged += OnStringChanged;
}
void OnDisable()
{
scoreText.StringChanged -= OnStringChanged;
scoreText.LocalVariables.Remove("score");
}
void OnStringChanged(string value) => GetComponent<TextMesh>().text = value;
public void AddPoints(int points)
{
m_Score.Value += points;
scoreText.RefreshString();
}
}
The Inspector window drawer for the field lets you pick a collection and an entry, or create a new entry inline. Once an entry is selected, the drawer shows a rail of the collection’s locales with a preview of each locale’s value, and a detail pane where you edit the value for the selected locale.
For a Smart String entry, the detail pane adds three tabs that help you author and debug the format:
Subscribe to StringChanged. The reference raises it with the current value when the string first resolves, and again whenever the value changes, including when the user switches language. Event subscription is the recommended path for UI text: subscribe once, assign the value in the handler, and the text updates on every language change.
To retrieve a value on demand instead, call GetLocalizedString or its asynchronous counterpart GetLocalizedStringAsync. Refer to Synchronous and asynchronous loading.
Enable the key’s Smart string toggle in the table editor, and its value becomes a Smart String format, such as Score: {score}. Supply values either as arguments to GetLocalizedString, or through the reference’s LocalVariables group. After you change a variable, call RefreshString to re-raise StringChanged with the new value.
For a complete example, refer to Display dynamic values in a localized string.
A LocalizedString can also act as a variable inside another Smart String. For example, if a notification’s Smart String reads New quest: {questTitle} and questTitle is another LocalizedString, the nested title resolves in the same locale as the notification.
To localize text with a component instead of a script:
LocalizeStringEvent) to a GameObject.text property. When the entry’s value updates, the event sets that property.The component keeps the target updated on language changes.