Logo

Localized Object

Component for binding data to UI elements

Overview

A component that acts as the bridge between your localization data and your scene. It attaches to a GameObject and automatically updates a target component (TMP_Text, AudioSource, or Image) based on the selected language and settings.


How does it work?

When the game starts or the language changes, the component follows these steps:

Lookup: It finds the translation using the selected Table and Key.

Fallback: If the key is missing in the current language, it checks if Use Fallback is enabled. If so, it retrieves the value from the default language.

Processing: It processes any Dynamic Strings (variables like {username}) registered to the object.

Application: It applies the final result to the target component (TMP_Text, AudioSource, or Image).


Public Properties

PropertyTypeDescription
tableLocalizationTableThe table asset containing the key.
tableKeystringThe specific key ID to localize.
objectTypeObjectTypeDefines which component to drive: TMPText, Audio, Image, or Custom.
useFallbackboolIf true, uses the default language when the key is missing in the current language.
updateModeUpdateModeDetermines when to update: OnEnable (Default), Start, or OnDemand.
lockToLanguageboolIf true, this object will ignore the global language and use lockedLanguage instead.
lockedLanguageLocalizationLanguageThe specific language to use if lockToLanguage is enabled.

Public Methods

MethodDescription
UpdateContent()Forces the object to refresh its content immediately based on the current key and language.
AddDynamicString(string keyword, string value, Object context)Registers a dynamic variable (e.g., {name}) to be replaced in the text. The context parameter is optional and can be null.
GetString(string key)Retrieves a localized string from the currently assigned table using the provided key.
GetAudio(string key)Retrieves a localized audio clip from the currently assigned table using the provided key.
GetSprite(string key)Retrieves a localized sprite from the currently assigned table using the provided key.

Events

The component provides Unity Events that trigger whenever the content is updated. This allows you to drive custom logic or components not natively supported.

EventParameterDescription
onStringChangedstringInvoked when the text content is updated.
onAudioChangedAudioClipInvoked when the audio clip is updated.
onSpriteChangedSpriteInvoked when the sprite image is updated.

Dynamic Strings

You can inject variables into your localized text using the {variable} syntax. For example, if your translation key is: "Hello, {player_name}!".

You can set the value of player_name via code:

public LocalizedObject welcomeText;
public CustomScript myScript;

void Start()
{
    // Register the dynamic variable
    welcomeText.AddDynamicString("player_name", "playerName", myScript);
    
    // Optional - Force an update to display the new text
    welcomeText.UpdateContent();
}

Scripting API

LocalizedObjcetExample.cs
using UnityEngine;
using UnityEngine.UI;
using Evo.Localization;

public class LocalizedObjectExample : MonoBehaviour
{
    public LocalizedObject targetObject;
    public Image externalImage;

    // For dynamic script
    public CustomScript myScript;

    void Start()
    {
        // Dynamic Strings
        // Binds the "{username}" placeholder in the text to "PlayerOne"
        targetObject.AddDynamicString("username", "PlayerOne", myScript);
        
        // Optional - Refresh to show the new string with the variable applied
        targetObject.UpdateContent();

        // You can add listeners to provided events that will get triggered with language changes
        targetObject.onStringChanged.AddListener(EventTest);
    }

    void EventTest(string output)
    {
        Debug.Log($"Localization returned a new output: {output}");
    }

    void UpdateText()
    {
        // Change the key at runtime
        targetObject.tableKey = "new_key_id";
        
        // Force the object to refresh immediately
        targetObject.UpdateContent();
    }

    void GetDirectData()
    {
        // Retrieve data directly from the object's assigned table
        // This is useful for getting related assets that aren't handling the main text
        string text = targetObject.GetString("other_key");
        AudioClip clip = targetObject.GetAudio("sfx_key");
        Sprite icon = targetObject.GetSprite("icon_key");

        // Alternatively, specify Table and use static variables
        string altText = LocalizedObject.GetString("Table ID", "other_key");
        AudioClip altClip = LocalizedObject.GetAudio("Table ID", "sfx_key");
        Sprite altIcon = LocalizedObject.GetSprite("Table ID", "icon_key");
    }
}

On this page