Class LocalizedTextElement
A text element that supports localization and string formatting with variables.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class LocalizedTextElement : BaseTextElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IVisualElementScheduler, IBindable, INotifyValueChanged<string>, ITextEdition, ITextElementExperimentalFeatures, IExperimentalFeatures, ITextSelection, IContextOverrideElement, IAdditionalDataHolder
Remarks
LocalizedTextElement is a specialized text component that enables seamless localization of text content in your Unity application's UI. It extends BaseTextElement and provides built-in support for string localization and dynamic text formatting using variables.
This component is particularly useful when building applications that need to support multiple languages and require dynamic text updates based on user interactions or application state.
The component integrates with Unity's Localization system and supports both direct text display and localized string references. When using localized strings, it automatically updates the displayed text when the application's language changes.
Note: To use the localization features, make sure you have the Unity Localization package installed in your project. Without it, the component will function as a regular text element.
Examples
Basic Usage with Plain Text.
<UXML>
<ui:LocalizedTextElement text="Hello World" />
</UXML>
Using Localization Keys.
<UXML>
<ui:LocalizedTextElement text="UI.Welcome" />
</UXML>
// C#
var welcomeText = new LocalizedTextElement("UI.Welcome");
container.Add(welcomeText);
Dynamic Text with Variables.
// Assuming the localized string is "Player {0} scored {1} points!"
var scoreText = new LocalizedTextElement("UI.ScoreMessage");
scoreText.variables = new object[] { "Player1", 100 };
container.Add(scoreText);
Complete Example with Styling.
<UXML>
<ui:LocalizedTextElement
text="UI.WelcomeMessage"
class="title-text"
style="font-size: 20px;" />
</UXML>
// C#
var welcomeText = new LocalizedTextElement("UI.WelcomeMessage");
welcomeText.variables = new object[] { "User", DateTime.Now.ToString() };
welcomeText.AddToClassList("title-text");
welcomeText.style.fontSize = 20;
container.Add(welcomeText);
Constructors
LocalizedTextElement()
Default constructor.
Declaration
public LocalizedTextElement()
LocalizedTextElement(string)
Constructor with a reference text.
Declaration
public LocalizedTextElement(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The reference text to use when formatting the localized string. You can also use plain text for no translation. |
Fields
ussClassName
The main USS class name of this element.
Declaration
public const string ussClassName = "appui-localized-text"
Field Value
| Type | Description |
|---|---|
| string |
Properties
text
The reference text to use when formatting the localized string. You can also use plain text for no translation.
Declaration
[CreateProperty]
public string text { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
variables
The variables to use when formatting the localized string.
Declaration
[CreateProperty]
public IList<object> variables { get; set; }
Property Value
| Type | Description |
|---|---|
| IList<object> |