How to translate text from a script using unity localization?

1.5k Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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