How to use localization on lines from a script?

Hello, I have a problem with localization, here is the picture:

What I need to do is to translate every line (element) from Dialogue script in english to portuguese, but I don’t have any idea I can do to make this work, I appreciate any help.

Here is the code from script Dialogue if necessary:

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

public class Dialogue : MonoBehaviour
{
    public TextMeshProUGUI textComponent;
    public string[] lines;
    public float textSpeed;

    private int index;

    // Start is called before the first frame update
    void Start()
    {
        textComponent.text = string.Empty;
        StartDialogue();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Action"))
        {
            if (textComponent.text == lines[index])
            {
                NextLine();
            }
            else
            {
                StopAllCoroutines();
                textComponent.text = lines[index];
            }
        }
    }

    void StartDialogue()
    {
        index = 0;
        StartCoroutine(TypeLine());
    }

    IEnumerator TypeLine()
    {
        //Type each character 1 by 1
        foreach (char c in lines[index].ToCharArray())
        {
            textComponent.text += c;
            yield return new WaitForSeconds(textSpeed);
        }
    }

    void NextLine()
    {
        if (index < lines.Length - 1)
        {
            index++;
            textComponent.text = string.Empty;
            StartCoroutine(TypeLine());
        }
        else
        {  
            gameObject.SetActive(false);

            index = 0;
        }   
    }
}

You could make each line a separate entry in the table instead of using a single entry.
Or you could get the value and then split it by new line characters.
e.g var lines = translatedString.Split(“\n”);
Is it possible to change the key bindings? You may want to make the values dynamic. Something like

“Use {key.Jump} to Jump” etc

1 Like

Oh yes! I forgot to mention if using key bindings are acceptable, thanks for that! :wink: This will work with gamepad buttons as well?

Sorry for being a noob but I still don’t get it how to split into lines, could you give an example like a picture? or this: e.g var lines = translatedString.Split(“\n”); where I can put this and then what I do, thank you for replying.

You will need to implement it. We don’t have a direct binding to Input at the moment. So you can get the Actions and then pass them into the GetStringAsync as arguments.

something like:

IEnumerator StartDialogue()
{
    var stringOp = LocalizationSettings.StringDatabase.GetStringAsync("lv1_1", "Dialog 01");
    yield return stringOp;
    lines = stringOp.Result.Split("\n");
    StartCoroutine(TypeLine());
}

Now just use your class like normal. This will assign the value to your lines variable.

1 Like

I’m sorry Karl, I still don’t get it the code you gave me, if you don’t mind could you show me where I can put this on the code? I tried to put and used:
using UnityEngine.Localization.Settings; but later GetStringAsync shows a error that I don’t get it.

Show me and explain step by step how to do it and preferably with images for better understanding. I’m not lazy, I just want to learn and dealing with this in programming still is is very difficult for me.

I hope you understand me.

Change your Start method to be

IEnumerator Start()
{
    var stringOp = LocalizationSettings.StringDatabase.GetStringAsync("lv1_1", "Dialog 01");
    yield return stringOp;
    lines = stringOp.Result.Split("\n");
    textComponent.text = string.Empty;
    StartDialogue();
}
1 Like

Well, I tried before and I’m having the same issue:

Mention something about referency on assembly? I’m using the: using UnityEngine.Localization.Settings; Maybe I need one for the GetStringAsync too?

It should be GetLocalizedStringAsync. I’m writing it on my phone so typos Wil happen :wink:

1 Like

No problem Karl! that solved my problem, by the way in “/n” I changed to ‘/n’ and the worked! :slight_smile:

Just one thing, I will use this code for every dialog, then I will need to change the “lvl_1” and “Dialog_01”. I created a public string for doing this but I failed, could you help me one last time with this?


I’ve made it! sorry for bothering you with somehow simple things. I removed the “” from the strings and worked!

Thank you very much Karl! :slight_smile:

2 Likes

Thank you so much! I could use that too!:slight_smile:

1 Like