Search Unity

Bug Issue with LocalizedString returning null in some instances.

Discussion in 'Localization Tools' started by zecbmno1, Nov 26, 2022.

  1. zecbmno1

    zecbmno1

    Joined:
    Jan 3, 2016
    Posts:
    6
    I have the Localization Tables preloaded and have a validation function on the localized string (see below).

    Code (CSharp):
    1.    
    2. public static string GetValidatedLocalisedText(this LocalizedString localizedString)
    3. {
    4. #if UNITY_EDITOR
    5.         if (localizedString.IsEmpty)
    6.         {
    7.             return "Missing Loc";
    8.         }
    9. #endif
    10.         if (localizedString.GetLocalizedStringAsync().Result == null)
    11.         {          
    12.             return "Loc is Null";
    13.         }
    14.  
    15.         return localizedString.IsEmpty ? string.Empty : localizedString.GetLocalizedStringAsync().Result;
    16. }
    In some instances, the localizationString is returning a null value. This is unexpected behaviour as the LocalizationString has a valid table reference. I have verified this by modifying the code to include:

    Code (CSharp):
    1.  
    2. if (localizedString.GetLocalizedStringAsync().Result == null)
    3. {
    4.             var table = LocalizationSettings.StringDatabase.GetTable("GENERATED", LocalizationSettings.ProjectLocale);
    5.  
    6.             if (table)
    7.             {
    8.                 var entry = table.GetEntryFromReference(localizedString.TableEntryReference);
    9.  
    10.                 if (entry != null)
    11.                 {
    12.                     return entry.Value;
    13.                 }
    14.             }
    15.             return "Loc is Null";
    16. }
    The table entry is value and returns the expected value. What also is odd behaviour is that with the above code added, after calling GetTableEntryFromReference on the first LocalizedString that's result was null, all others have seemed to fix themselves.

    Note* I have a custom class for wrapping the LocalizationString to be easier to edit and update with just a TextArea for the english value. This has not caused issue and I can see the LocalizationString is set, and has the correct table entry as above.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Are you able to share the project or a project that has the same problem?
     
  3. zecbmno1

    zecbmno1

    Joined:
    Jan 3, 2016
    Posts:
    6
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Hi,
    The problem is you are not waiting for initialization to complete. Preloading of string tables occurs during the initialization operation, by default it is asynchronous. At the start you are grabbing the values in Awake, if you check the status of GetLocalizedStringAsync you will see that IsDone is false. You need to wait for the initialization operation. In your example this can be done by changing the awake method to a start method with a coroutine like so:

    Code (csharp):
    1.  
    2.     IEnumerator Start()
    3.     {
    4.         yield return LocalizationSettings.InitializationOperation;
    5.  
    6.         TMP_Text text = GetComponent<TMP_Text>();
    7.  
    8.         if (text == null)
    9.         {
    10.             Debug.LogError("This text object should have TMP_Text item attacher: " + gameObject.name);
    11.         }
    12.         else
    13.         {
    14.             text.text = localizedText.GetValidatedLocalisedText();
    15.         }
    16.     }
    Alternatively, you can also enable the Initialize Synchronously option

    upload_2022-11-28_10-25-14.png
     
  5. zecbmno1

    zecbmno1

    Joined:
    Jan 3, 2016
    Posts:
    6
    So it turns out, I was the bug. Thank you. I was thrown off by the table entry returning the value fine.
     
    karl_jones likes this.