Class UnityUiTMP_TextLocalizationUpdaterComponent
Unity component that manages TextMeshPro text localization using Unity's Localization system.
Automatically updates localized strings based on current text content and provides manual refresh capabilities.
Integrates with Unity Localization package to provide dynamic text translation for UI elements.
Namespace: Serenity.Ui.Infrastructure.Components
Assembly: Serenity.UnityUi.Infrastructure.dll
Syntax
public class UnityUiTMP_TextLocalizationUpdaterComponent : MonoBehaviour
Constructors
UnityUiTMP_TextLocalizationUpdaterComponent()
Declaration
public UnityUiTMP_TextLocalizationUpdaterComponent()
Methods
ApplyKey(TMP_Text, string, bool)
Writes text that may be a localization key onto a TMP and keeps it translated, for a label whose content changes after the component has already started.
Declaration
public static void ApplyKey(TMP_Text text, string content, bool translate)
Parameters
| Type | Name | Description |
|---|---|---|
| TMP_Text | text | The TMP whose GameObject should host the localization updater. |
| string | content | The text to display — a localization key when |
| bool | translate | Whether the content is a localization key rather than literal text. |
Remarks
ConfigureFor(TMP_Text, bool) only arms the translation that Serenity.Ui.Infrastructure.Components.UnityUiTMP_TextLocalizationUpdaterComponent.Start performs, which is enough for a label written once and then left alone — a modal's title, a toast's message. It is not enough for a label that is rebound: the second write lands after Start has been and gone, so the raw key stays on screen. Pooled list rows are exactly that case, so they call this instead.
ConfigureFor(TMP_Text, bool)
Configures runtime localization for any TMP whose displayed text should be treated as a
localization key — the shared model behind title/body text (UnityModalComponent),
button labels (UnityUiInteractableDecorationCoordinator.ConfigureLabelLocalization),
and toast messages (UnityToastComponent). When translate is true
the assigned text is treated as a localization key. The updater component is added on demand
if missing — its [RequireComponent] + self-healing dependency resolution make runtime
AddComponent safe, so no prefab edit is required.
Declaration
public static void ConfigureFor(TMP_Text text, bool translate)
Parameters
| Type | Name | Description |
|---|---|---|
| TMP_Text | text | The TMP whose GameObject should host the localization updater. |
| bool | translate | Whether the current text should be localized. |
GetText()
Provides access to the associated TextMeshPro text component.
Returns the text component used for displaying localized content.
Declaration
public TMP_Text GetText()
Returns
| Type | Description |
|---|---|
| TMP_Text | The TMP_Text component managed by this localization updater |
SetTranslateOnStart(bool)
Sets whether to perform initial localization on component start.
Allows external configuration of translation behavior before the Start lifecycle method executes.
Must be called before Start() to affect initial localization behavior.
Declaration
public void SetTranslateOnStart(bool translateOnStart)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | translateOnStart | True to enable translation on start, false to disable |
StopTranslating()
Leaves the TMP's current text exactly as the caller wrote it and stops treating it as a key, for a reused label that has gone from translated content to literal content.
Declaration
public void StopTranslating()
TranslateKey(string)
Points this updater at a localization key and translates from it, discarding whatever it translated before. The way to re-translate a label whose key changed after the component already ran.
Declaration
public void TranslateKey(string key)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | The localization key to display and translate. Null or empty clears the text. |
Remarks
Idempotent by construction: the caller supplies the key on every call and this writes it back onto the TMP before looking it up, so a label that already shows a translation is never looked up by its translated text. That is what makes it safe for a REUSED label — a pooled list row rebound to a different achievement, say — where Serenity.Ui.Infrastructure.Components.UnityUiTMP_TextLocalizationUpdaterComponent.Start has long since run and the component's cached key belongs to whatever the row showed before.
UpdateLocalization()
Updates the localization settings and refreshes the displayed text.
Sets the current text content as the localization table entry reference and triggers string refresh.
Uses the Global localization table for text resolution.
Two paths:
- Unity Localization path (default — used whenever the
service exposes a non-empty GetStringTableReference()).
Delegates the actual string lookup to
LocalizeStringEvent.RefreshString, which goes through Unity Localization's Addressables-backed table registry. Untouched from the pre-Slice-AA behaviour. - Fallback path (Slice AA — used in
ResourcesOnly/WebGL mode, where GetStringTableReference() intentionally returns an empty string so this branch is taken). Asks the service to translate the raw key directly via TryTranslate(string, out string), which the fallback service resolves against translation JSON the WebGL exporter wrote into the consumer'sResources/Localization/tree. On a miss the original key text is left on screen — same behaviour as before Slice AA.
Declaration
public void UpdateLocalization()