Caching Translation before using them in UI?

Hey all,

I am trying to find a way to automatically update a scriptable object once a LocalizedString Key is changed, StringChanged does not apply here.

I use this to cache the result of the translation.

using System;
using UnityEngine;
using UnityEngine.Localization;

[Serializable]
[CreateAssetMenu(fileName = "New Basic Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
    [Header("DEVELOPER PROPERTIES")]
    [SerializeField, Tooltip("This is a flag for the developers to know what is ready to be used.")]
    public bool markAsImplemented;

    [Header("VISUAL REPRESENTATION")]
    [SerializeField, Tooltip("The icon of the item that will be displayed in the inspector ui.")]
    public Sprite icon;

    [SerializeField, Tooltip("2D or 3D object to spawn in the world and interacted with.")]
    public GameObject pickupObject;

    [SerializeField, Tooltip("2D or 3D object to display in the UI.")]
    public GameObject displayObject;

    [Header("DETAILS (Localized Fields)")]
    [SerializeField, Tooltip("The name of the Localisation table. It is advisable to use one table for all game objects.")]
    public String localizationTableName;

    [Header("TITLE")]
    [SerializeField, Tooltip("Key used to look up the title in the localization table.")]
    public String titleLocalizationKey;

    [SerializeField, Tooltip("The localized string object that will be used to display the title of the item.")]
    public LocalizedString titleLocalized;

    public string Title
    {
        get;
        private set;
    }

    [Header("DESCRIPTION")]
    [SerializeField, Tooltip("Key used to look up the description in the localization table.")]
    public String descriptionLocalizationKey;

    [SerializeField, Tooltip("The localized string object that will be used to display the description of the item.")]
    public LocalizedString descriptionLocalized;

    public string Description
    {
        get;
        private set;
    }

    [Header("OTHER PROPERTIES")]
    [SerializeField, Tooltip("A single instance of this item can exist in the game.")]
    public bool unique;

    [SerializeField, Tooltip("A single instance of this item can exist on a player.")]
    public bool uniquePerPlayer;

    [SerializeField, Tooltip("The item can be equipped by the player.")]
    public bool equipable;

    [SerializeField, Tooltip("The item can be upgraded using crafting or any other gameplay means")]
    public bool upgradeable;

    [SerializeField, Tooltip("The item can be consumed by the player")]
    public bool consumable;

    [SerializeField, Tooltip("The weight of this item in game units (kg or lbs)")]
    public float weight;

    [SerializeField, Tooltip("The value of this item in the game currency")]
    public float value;

    private void Start()
    {
        titleLocalized = new LocalizedString(localizationTableName, titleLocalizationKey);
        titleLocalized.TableEntryReference = titleLocalizationKey;
        titleLocalized.StringChanged += OnTitleStringChanged;

        descriptionLocalized = new LocalizedString(localizationTableName, descriptionLocalizationKey);
        descriptionLocalized.TableEntryReference = descriptionLocalizationKey;
        descriptionLocalized.StringChanged += OnDescriptionStringChanged;
    }

    private void OnDescriptionStringChanged(string updatedString)
    {
        Title = updatedString;
    }

    private void OnTitleStringChanged(string updatedString)
    {
        Description = updatedString;
    }
}

Any elegant suggestions to solve that?

Title and Description are the fields I want to update each time titleLocalizationKey or descriptionLocalizationKey change.

What sort of change are you looking for? When someone renames an entry in the Editor?
I can see you are using a LocalizedString for some of your fields, is there a reasons you cant use a LocalizedString for the fields you want to update? The editor will store a reference to the entry via its KeyId which is unique and wont change, so no need to update when renaming.

When the key field is changed, I need to translate and cache.
This is to avoid having to Translate in the UI update.

So the fundamenal issue is that, I need to cache when anything changes on the LocalizedString or their key field (Special Field)

Is this in the Editor?
You can use the EditorEvents in LocalizationEditorSettings.EditorEvents.

Sweeet! That’s it! Was not aware of that API!

Trying.

1 Like

What package is that in…

My asmdef:
“references”: [“Unity.Localization”],
My package.json:

This looks also ok:

I’m getting

Do you have a reference to Unity.Localization.Editor in the asmdef?

OH! That Unity.Localization Reference is not recursive!

Trying!

That fixed it. Thank you Karl.

1 Like