1

I know how to translate the text and so on. But I don't know how to translate text from a script (textMeshPro + value), like this script of mine. I'm using unity's own localization, which is easy for me to use.

Translate the words: Coins and DEATH COUNT + value (data.coin)

`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class SaveSlot : MonoBehaviour
{
    [Header("Profile")]
    [SerializeField] private string profileId = "";

    [Header("Content")]
    [SerializeField] private GameObject noDataContent;
    [SerializeField] private GameObject hasDataContent;
    [SerializeField] private TextMeshProUGUI CoinText;
    [SerializeField] private TextMeshProUGUI deathCountText;

    [Header("Clear Data Button")]
    [SerializeField] private Button clearButton;

    public bool hasData { get; private set; } = false;

    private Button saveSlotButton;

    private void Awake()
    {
        saveSlotButton = this.GetComponent<Button>();
    }

    public void SetData(GameData data)
    {
        // there's no data for this profileId
        if (data == null)
        {
            hasData = false;
            noDataContent.SetActive(true);
            hasDataContent.SetActive(false);
            clearButton.gameObject.SetActive(false);
        }
        // there is data for this profileId
        else
        {
            hasData = true;
            noDataContent.SetActive(false);
            hasDataContent.SetActive(true);
            clearButton.gameObject.SetActive(true);
        
            CoinText.text = "Coins: " + data.coin;
            deathCountText.text = "DEATH COUNT: " + data.deathCount;
        }
    }

    public string GetProfileId()
    {
        return this.profileId;
    }

    public void SetInteractable(bool interactable)
    {
        saveSlotButton.interactable = interactable;
        clearButton.interactable = interactable;
    }
}

`

Translate string I already know more or less, but not the TextMeshPro + the value. How do I have to translate?

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Haven't worked with the localization so far but probably simplest would be to split this .. what you have a clear splitable labels + values so only translate the labels and then have separate TMP_Text for only setting the displayed amount – derHugo Nov 28 '22 at 15:33

1 Answers1

1

If you've set-up the Unity Localization package, you should already have localisation tables created with your translations.

To fetch your desired translation, you can use the asynchronous GetLocalizedString(string tableName, string key) method with the following parameters:

  • tableName - Table to search for the original string.
  • key - The string key taken from the KeyDatabase, that should be used to find the translated text.

For the "Coins" string, you could extend the parameters to include the plural type by using GetLocalizedString(string tableName, string key, int puralValue)

Alternatively, as of 2020.3+, you could easily localise the TextMeshPro component directly using Localization Scene Controls. Or if you're working on older versions via the Localize String Event

Juris
  • 407
  • 3
  • 14
  • How do you access that function `GetLocalizedString`? I need to dynamically populate the text from within the code so it's not really something like a button with a simple text... Thanks – Lam Le Jun 03 '23 at 07:07