Search Unity

[solved]how to change localize string reference at runtime through script?

Discussion in 'Localization Tools' started by Aokkii, Jan 23, 2020.

  1. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    118
    for example for a loading screen where it shows "Tips" or advices text meanwhile the scene is loading,


    how can I change the string reference id through script so it can change between Tip1, Tip2, Tip3, etc at runtime?

    I know how to access the localizedstring component but not the fields inside.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    Hi,

    Good question, I will make a note to add some further examples of this in the future.
    There are 2 fields in the LocalizedString that you can change

    TableReference
    The table that you want the entry from. You can assign the name of the table or the guid of the table to this field.

    TableEntryReference
    The actual entry in the table. You can assign the entry key-id or the entry name to this field.

    so for example

    Code (csharp):
    1. myLocalizedString.TableReference = "UI Text";
    2. myLocalizedString.TableEntryReference = "Tip2";
    3. //myLocalizedString.TableEntryReference = 1;

    If you are using the change handler approach then you will need to invalidate it so it can refresh as we don't currently handle an automatic update when it changes, ill make a note to add this in the future.
    Code (csharp):
    1. myLocalizedString.ClearChangeHandler();
    2. // Now re register


     
  3. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    118
    Thanks
     
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    No problem. Please do let us know any feedback you have with your experience :)
     
  5. Brijesh_Kumar_Vaishya_

    Brijesh_Kumar_Vaishya_

    Joined:
    Jan 24, 2018
    Posts:
    6
    from Inspector it shows LocalizeString and from the script, it shows LocalizedString type, I am a bit confused
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
  7. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    118
    revisiting this topic cuz i found a problem when i tried to implement it.:oops:

    when i used "public localizedstring" , I noticed that in the inspector instead of expecting me to drop in the localize string(script) component , it creates a somewhat new instance of the component.

    what i wanted to do:

    Code (CSharp):
    1.  
    2. public class Notificationmanager : monobehavior
    3. {
    4. public LocalizedString mylocalizedstring; ///the localize string component of the text object
    5.     public GameObject notification; ////the text object
    6.  
    7.  
    8.  
    9.     public  void Shownotification(string keyID)//called from another object
    10.     {
    11.         mylocalizedstring.TableEntryReference =keyID;
    12.         notification.SetActive(true);
    13.     }
    14. }
    15.  

    Please in a future release, let users reference the Localize string component of a gameobject itself. so users can reference THAT component from other gameobjects.
     
    Last edited: Feb 28, 2020
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    The LocalizedString is just a normal C# class. To reference it from another script it would need to be a MonoBehaviour or an asset. We do have `LocalizeStringBehaviour` which is a MonoBehaviour and can be referenced in a script the way you describe.
     
  9. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    118
    The thing is that it can't be referenced, at least not with using UnityEngine.Localization;

    Code (CSharp):
    1.  public LocalizeStringBehaviour mylocalizedstring;
    will throw the error that the type or namespace is not found.



    they can be referenced with "using UnityEngine.Localization.components;"

    but even then "TableEntryReference" nor "TableReference" are not part of it.
     
    Last edited: Feb 28, 2020
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    The LocalizedStringBehaviour contains a LocalizedString.

    It is the StringReference property.

    so

    Code (csharp):
    1. public LocalizedStringBehaviour myStringReference;
    2.  
    3. void foo()
    4. {
    5.     myStringReference.StringReference.TableEntryReference = "TEST";
    6. }
     
  11. Aokkii

    Aokkii

    Joined:
    Nov 3, 2013
    Posts:
    118
    thank you so much, now it works,
     
  12. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    Heres a static helper function for getting the string without using a coroutine.

    *edit*
    Updated to include arguments
    Code (CSharp):
    1.  
    2. public static async Task<string> GetLocalizedString(string table, string key, Object[] args = null)
    3. {
    4.     var localizedString = new LocalizedString
    5.     {TableReference = table, TableEntryReference = key};
    6.     if (args?.Length > 0)
    7.         localizedString.Arguments = args;
    8.            
    9.     var handle = localizedString.GetLocalizedString();
    10.     while (handle.IsDone == false)
    11.         await Task.Yield();
    12.    
    13.     return handle.Result;
    14. }
    15.  


    Code (CSharp):
    1.         public static async Task<string> GetLocalizedString(LocalizedString reference)
    2.         {
    3.             var handle = reference.GetLocalizedString();
    4.             while (handle.IsDone == false)
    5.                 await Task.Yield();
    6.        
    7.             return handle.Result;
    8.         }
     
    Last edited: Jan 23, 2021
  13. BeorGames

    BeorGames

    Joined:
    Jul 28, 2018
    Posts:
    65
    Hello Karl,

    I'm trying to change a string in runtime but it doesn't find LocalizedStringBehaviour, I've added
    Code (CSharp):
    1. using UnityEngine.Localization;
    2. using UnityEngine.Localization.Components;
    So I tried something like this:
    Code (CSharp):
    1. string sentence = sentences.Dequeue();
    2.         LocalizeStringEvent stringComponent = dialogueText.GetComponent<LocalizeStringEvent>();
    3.         LocalizedString newTranslation = new LocalizedString();
    4.         newTranslation.TableEntryReference = sentence;
    5.         stringComponent.StringReference = newTranslation;
    6.         stringComponent.RefreshString();
    7.         dialogueText.SetText(newTranslation.TableEntryReference);
    But it returns the string without translating, how should I proceed with it?

    I'm trying to implement it for a dialogue system, but I'll also need it later for a quest window and an item description window, so I need to find a way that before the text is set in my TMP component it gets translated, and I need it to be done as fast as possible, so that the player won't have to wait to get the text translated before being shown...
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    dialogueText.SetText(newTranslation.TableEntryReference);
    should be
    dialogueText.SetText(newTranslation.GetLocalizedString());
     
    BeorGames likes this.
  15. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    Hi, I came across this post as I stumbled upon a problem with the StringReference of an LocalizeStringEvent component.

    I have two TMP Text elements, which are "dynamic" elements of my HUD that are updated through script.
    The script has a reference to two LocalizeStringEvent components named announceTextLocalizeEvent and announceSmallTextLocalizeEvent.

    I have a method that any other script can call:
    Code (CSharp):
    1. void Announce(LocalizedString localizedAnnounceText, LocalizedString localizedAnnounceTextSmall = null)
    2.     {
    3.         announceTextLocalizeEvent.StringReference = localizedAnnounceText;
    4.         announceSmallTextLocalizeEvent.StringReference = localizedAnnounceTextSmall;
    5.     }
    As you can see, I'd like to have the small text as an optional parameter. However I noticed that you can't set the StringReference to null without errors thrown at you because of the ClearChangeHandler. How should one set the LocalizeEventHandler StringReference back to "None" after it has been set once?

    EDIT: My problem was not necessarily related to the LocalizeStringEvent. I noticed that if I just manually set the TMP component's text to empty string I get the result I want (which was to display an empty string if the secondary argument given to the method was null).
     
    Last edited: Dec 6, 2021
    karl_jones likes this.
  16. alexisdavidson

    alexisdavidson

    Joined:
    Apr 28, 2015
    Posts:
    29
    Hi, LocalizedStringBehaviour does not exist ... doc is outdated
     
  17. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
  18. alexisdavidson

    alexisdavidson

    Joined:
    Apr 28, 2015
    Posts:
    29
  19. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    It was renamed to LocalizeStringEvent
     
  20. alexisdavidson

    alexisdavidson

    Joined:
    Apr 28, 2015
    Posts:
    29
    That helped me a lot, thank you and merry Xmas :)
     
    karl_jones likes this.
  21. naodisseporfavor

    naodisseporfavor

    Joined:
    Jul 18, 2019
    Posts:
    6
    Code (CSharp):
    1. GetComponent<LocalizeStringEvent>().StringReference.SetReference("YourGameTranslationTable", "YourTranslationReference");
     
  22. hwibaek2827

    hwibaek2827

    Joined:
    May 2, 2022
    Posts:
    3
    Hi, I read your post and looked up LocalizeStringEvent, but it was deleted from the latest version. Is there any alternative to that?(Please understand that I used a translator because I am not good at English)
     
  23. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292