How can I localize text for subtitles.

Hi,

I’m developing a game that has subtitles for the spoken dialogues. A dialogue is a list of strings, and I’m using a coroutine to change to the next string after some seconds.

For static texts, the localization it works pretty well, but for dynamic texts like subtitles don’t… So when I click to change the language instead of the subtitle text change the text right away as occur in static strings, the current dialogue stays with the former language, and it just changes to the selected language when the next string is shown. I have already tried to use StringChanged event, but it didn’t work, but I’m not sure if I used it correctly.

Here is the logic of changing strings.

TextMeshProUGUI subtitletUI;
DialogueList dialogueList;

IEnumerator StartDialogue()
{
  foreach (var subtitle in dialogueList)
  {
    subtitleUI.text = subtitle.text;
    yield return new WaitForSeconds(subtitle.delayTime);
  }
}

So how can I change the localization after I click to change the language?

What’s dialogueList look like?
I would recommend using a list of LocalizedString for your list of subtitles.

Hello @karl_jones ,

Just for testing the localization package, I’m using simple structures, so a dialogue list is a structure that contains two elements. The first is the text key it is the same that are in the localization tables. The second element is a delay time, that is the time that the subtitle will be shown in the screen.

I will show the code for the subtitles manager, it’s responsible to iterate each subtitle for a specific time.

[Serializable]
public struct DialogueList
{
    public string textKey;
    public float time;
}
public class SubtitlesManager : MonoBehaviour
{
    [SerializeField] TextMeshProUGUI subtitleTextUI;
    [SerializeField] List<DialogueList> dialogueList = new();
    [SerializeField] GameObject subtitleTextObject;

    public void StartDialogue()
    {
        StartCoroutine(StartSubtitleCorroutine());
    }

    IEnumerator StartSubtitleCorroutine()
    {
        yield return LocalizationSettings.InitializationOperation;
        Debug.Log("Start Corroutine");
        subtitleTextObject.SetActive(true);
        foreach (var subtitle in dialogueList)
        {
            subtitleTextUI.text = GetString(subtitle.textKey);
            yield return new WaitForSeconds(subtitle.time);
        }
        subtitleTextObject.SetActive(false);
    }

    private string GetString(string subtitleKey)
    {
        return LocalizationSettings.StringDatabase.GetLocalizedString("NewTable", subtitleKey);
    }
}

I’m changing the language based in this tutorial:

public class LanguageSelector : MonoBehaviour
{
    private bool active = false;

    public void ChangeLanguage(int localizationId)
    {
        if (active)
            return;
        StartCoroutine(SetLanguage(localizationId));
    }

    IEnumerator SetLanguage(int localizationId)
    {
        active = true;
        yield return LocalizationSettings.InitializationOperation;
        LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[localizationId];
        active = false;
    }
}

I uploaded a filte to show how is working now:

When I choose a different language the static text changes to the select language instantly, but the text in the subtitltes just change when the next subtitle is activated.

9743599--1394110--Localization_Bug.gif

The SubtitlesManager will need to respond to the language change and refresh the text.
It should subscribe to LocalizationSettings.SelectedLocaleChanged and then clear and regenerate the text when it gets the event.

It worked!! Thanks a lot @karl_jones ! :smile:

I will show the rest of the code in case anyone has the same problem as I.

private void UpdateSubtitleLocale(Locale locale)
    {
        subtitleTextUI.text = "";
        subtitleTextUI.text = GetString(currentSubtitleKey);
    }

    private void OnEnable()
    {
        LocalizationSettings.SelectedLocaleChanged += UpdateSubtitleLocale;
    }

    private void OnDisable()
    {
        LocalizationSettings.SelectedLocaleChanged -= UpdateSubtitleLocale;
    }
3 Likes