using UnityEngine;
using UnityEngine.Localization;
using TMPro;
using UnityEngine.UI;
using UnityEngine.Localization.Tables;
using UnityEngine.Localization.Settings;

public class LocalizedText : MonoBehaviour
{
    public LocalizedString localizedString;
    private TextMeshProUGUI textComponent;
    private Text text;
    void OnDestroy()
    {
        localizedString.StringChanged -= UpdateText;
        
    }

    public void changeKey(string key)
    {
        localizedString.StringChanged -= UpdateText;
        localizedString.StringChanged += UpdateText;
        localizedString.TableEntryReference = key;
        localizedString.RefreshString();

    }

    void Start()
    {  
        textComponent = GetComponent<TextMeshProUGUI>();
        text = GetComponent<Text>();
        if(localizedString!=null)
        changeKey(localizedString.TableEntryReference);

    }

    private void UpdateText(string value)
    {
        if(textComponent)
        textComponent.text = value;
        if(text)
        text.text = value;
    }
}