Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Using a string array to get the localization key

Discussion in 'Localization Tools' started by itsfly, Nov 13, 2023.

  1. itsfly

    itsfly

    Joined:
    Mar 19, 2014
    Posts:
    5
    Hello! Been adding some localization to my current project but I've hit a snag last minute. Is there a way to read localization keys from an array of strings? Currently have the project set up to read through a list of strings and display the text through text mesh pro.

    From what I've gathered online the intended use of Unity's localization strings is to have them 'isolated' to specific text like "quit game" and read the localization strings like that. (Feel free to correct me if I'm wrong!)

    I've attached the two functions which are displaying the text just curious if in the string array (whatStrings) I can input the localization key like {TextOne} (etc.) If I've been unclear with anything please let me know! Thanks.

    Here's the two functions I'm currently using to display the text:

    Code (CSharp):
    1.     void NextSentence()
    2.     {
    3.         //this is to get the current string in the array
    4.         if (whichStringCurrently <= whatStrings.Length - 1)
    5.         {
    6.             initialTimer = 1;
    7.             talkingText.text = "";
    8.  
    9.             //this is the function I use to write the words written in the string
    10.             StartCoroutine(WriteWords());
    11.         }
    12.         nextString = true;
    13.  
    14.         //this increases the int to move onto the next string
    15.         whichStringCurrently++;
    16.     }
    17.  
    18.     IEnumerator WriteWords()
    19.     {
    20.         foreach (char Character in whatStrings[whichStringCurrently].ToCharArray())
    21.         {
    22.             talkingText.text += Character;
    23.             yield return new WaitForSeconds(dialSpeed);
    24.         }
    25.     }
    How it looks in the inspector:
    Editor.png

    Variable on the local string event:
    text.png
     
    Last edited: Nov 13, 2023
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,147
    Could you use an array of LocalizedString instead? Then you can just query each one with GetLocalizedString.
     
  3. itsfly

    itsfly

    Joined:
    Mar 19, 2014
    Posts:
    5
    Hey Karl! Wasn't aware that you could make an array of localized strings. What would be the correct way to go about it?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,147
  5. itsfly

    itsfly

    Joined:
    Mar 19, 2014
    Posts:
    5
    Cheers! Managed to get it working.

    For anyone with a similar issue the updated code looks like this:
    Code (CSharp):
    1.     void NextSentence()
    2.     {
    3.         if (whichStringCurrently <= myStrings.Length - 1)
    4.         {
    5.             initialTimer = 1;
    6.             talkingText.text = "";
    7.             StartCoroutine(WriteWords());
    8.         }
    9.         nextString = true;
    10.         whichStringCurrently++;
    11.     }
    12.  
    13.     IEnumerator WriteWords()
    14.     {
    15.         var sentence = myStrings[whichStringCurrently].GetLocalizedString();
    16.         foreach (char Character in sentence.ToCharArray())
    17.         {
    18.             talkingText.text += Character;
    19.             yield return new WaitForSeconds(dialSpeed);
    20.         }
    21.     }
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,147
    Looks great. Since you are already inside a coroutine. It may be worth doing GetLocalizedStringAsync and yielding on the returned operation. This will help with performance and prevent blocking. Although if it's working and you are happy then keep it as is :)
     
  7. itsfly

    itsfly

    Joined:
    Mar 19, 2014
    Posts:
    5
    As it stands not seeing a massive performance drop with GetLocalizedString. When I switch it over to GetLocalizedStringAsync it seems to throw an error relating to the ToCharArray. Not too sure if I've missed something in the documentation relating to it?

    Error:
    Assets\Scriptas\QuestionFrog.cs(138,45): error CS1061: 'AsyncOperationHandle<string>' does not contain a definition for 'ToCharArray' and no accessible extension method 'ToCharArray' accepting a first argument of type 'AsyncOperationHandle<string>' could be found (are you missing a using directive or an assembly reference?)
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,147
    You need to get the Result property.
    E.g operation.Result Will get you the string.
     
  9. itsfly

    itsfly

    Joined:
    Mar 19, 2014
    Posts:
    5
    Thanks for the response! Will have a look into it for the next round of optimization.
     
    karl_jones likes this.