Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Translate Localize in Array

Discussion in 'Localization Tools' started by djovercome, Jul 11, 2022.

  1. djovercome

    djovercome

    Joined:
    Jun 4, 2022
    Posts:
    3
    Hello how are you?! My name is Diego and I have a problem, I'm using Localize to translate my game, but I have a dialog system and my conversations are in arrays and I'm not able to translate them, could you help me please?


    my code


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


    public class DialogueControl : MonoBehaviour
    {
    [Header("Components")]
    public GameObject dialogueObj;
    public Image profile;
    public Text speechText;
    public Text actorNameText;

    [Header("Settings")]
    public float typingSpeed;
    private string[] sentences;
    private int index;

    public Dialogue d;

    private void Start()
    {

    d = FindObjectOfType<Dialogue>();
    }



    public void Speech(Sprite p, string[] txt, string actorName)
    {
    dialogueObj.SetActive(true);
    profile.sprite = p;
    sentences = txt;
    actorNameText.text = actorName;
    StartCoroutine(TypeSentence());

    }

    IEnumerator TypeSentence()
    {
    foreach (char letter in sentences[index].ToCharArray())
    {
    speechText.text += letter;
    yield return new WaitForSeconds(typingSpeed);
    }
    }

    public void NextSentence()
    {
    if(speechText.text == sentences[index])
    {
    if(index < sentences.Length -1)
    {
    index++;
    speechText.text = "";
    StartCoroutine(TypeSentence());
    }
    else
    {
    speechText.text = "";
    index = 0;
    dialogueObj.SetActive(false);

    }
    }

    }


    }
     

    Attached Files:

  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    You can change from using a string array to a LocalizedString array.
    Something like this:

    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4. using UnityEngine.UI;
    5.  
    6. public class DialogueControl : MonoBehaviour
    7. {
    8.     [Header("Components")]
    9.     public GameObject dialogueObj;
    10.  
    11.     public Image profile;
    12.  
    13.     public Text speechText;
    14.  
    15.     public Text actorNameText;
    16.  
    17.     [Header("Settings")]
    18.     public float typingSpeed;
    19.  
    20.     private LocalizedString[] sentences;
    21.  
    22.     private int index;
    23.  
    24.     public Dialogue d;
    25.  
    26.     private void Start()
    27.     {
    28.         d = FindObjectOfType<Dialogue>();
    29.     }
    30.  
    31.     public void Speech(Sprite p, LocalizedString[] txt, string actorName)
    32.     {
    33.         dialogueObj.SetActive(true);
    34.         profile.sprite = p;
    35.         sentences = txt;
    36.         actorNameText.text = actorName;
    37.         StartCoroutine(TypeSentence());
    38.     }
    39.  
    40.     IEnumerator TypeSentence()
    41.     {
    42.         var sentence = sentences[index].GetLocalizedString();
    43.         foreach (char letter in sentence.ToCharArray())
    44.         {
    45.             speechText.text += letter;
    46.             yield return new WaitForSeconds(typingSpeed);
    47.         }
    48.     }
    49.  
    50.     public void NextSentence()
    51.     {
    52.         if (index < sentences.Length - 1)
    53.         {
    54.             index++;
    55.             speechText.text = "";
    56.             StartCoroutine(TypeSentence());
    57.         }
    58.         else
    59.         {
    60.             speechText.text = "";
    61.             index = 0;
    62.             dialogueObj.SetActive(false);
    63.         }
    64.     }
    65. }
    Alternatively, you could call directly and use the sentence as the key when calling into GetLocalizedString.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Localization.Settings;
    5. using UnityEngine.UI;
    6.  
    7. public class DialogueControl : MonoBehaviour
    8. {
    9.     [Header("Components")]
    10.     public GameObject dialogueObj;
    11.  
    12.     public Image profile;
    13.  
    14.     public Text speechText;
    15.  
    16.     public Text actorNameText;
    17.  
    18.     [Header("Settings")]
    19.     public float typingSpeed;
    20.  
    21.     private string[] sentences;
    22.  
    23.     private int index;
    24.  
    25.     public Dialogue d;
    26.  
    27.     private void Start()
    28.     {
    29.         d = FindObjectOfType<Dialogue>();
    30.     }
    31.  
    32.     public void Speech(Sprite p, string[] txt, string actorName)
    33.     {
    34.         dialogueObj.SetActive(true);
    35.         profile.sprite = p;
    36.         sentences = txt;
    37.         actorNameText.text = actorName;
    38.         StartCoroutine(TypeSentence());
    39.     }
    40.  
    41.     IEnumerator TypeSentence()
    42.     {
    43.         var translated = LocalizationSettings.StringDatabase.GetLocalizedString("String Table Name", sentences[index]);
    44.         foreach (char letter in translated.ToCharArray())
    45.         {
    46.             speechText.text += letter;
    47.             yield return new WaitForSeconds(typingSpeed);
    48.         }
    49.     }
    50.  
    51.     public void NextSentence()
    52.     {
    53.         if (speechText.text == sentences[index])
    54.         {
    55.             if (index < sentences.Length - 1)
    56.             {
    57.                 index++;
    58.                 speechText.text = "";
    59.                 StartCoroutine(TypeSentence());
    60.             }
    61.             else
    62.             {
    63.                 speechText.text = "";
    64.                 index = 0;
    65.                 dialogueObj.SetActive(false);
    66.             }
    67.         }
    68.     }
    69. }
     
  3. djovercome

    djovercome

    Joined:
    Jun 4, 2022
    Posts:
    3
    Thank you very much, it worked just fine! :):):):):)
     
    karl_jones likes this.