Search Unity

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

Question How to check if GetLocalizedString succeeded?

Discussion in 'Localization Tools' started by Orion, Nov 15, 2023.

  1. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    257
    When GetLocalizedString fails, for example, because the data provided wasn't what the string expected, it returns null. This is the same result as when the string is set to None.

    How can I check if it failed because something went wrong versus it not being set?

    It quietly eats up the exception that occurs when it tries to resolve itself, which would be very useful to have to debug strings that don't work.

    I assume there's some secret hack to get the output of the exception into the console - so that would be useful too.

    But I want to be able to check that all my strings (that are not None) work, preferably at runtime, before trying to display them. But also at edit time, so I can create a tool to find broken localizations.

    How can this be done? Normally, I'd try/catch GetLocalizedString, but yeah, it doesn't let the exception reach that level.
     
  2. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    257
    Okay, found the solution: By checking localizedString.IsEmpty I can see what's set to None. If that's false and the result is null, it failed to resolve it.

    However the issue of getting the debug information still stands.
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,147
  4. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    257
    Ah, great. The async solution works. I just hope it'll never not instantly complete, because I'm running this in the editor and didn't make my own code async (that'd be a lot of work now). So I'm just hoping "IsDone" is true, fingers crossed.

    The "No Translation Found Format" doesn't seem to affect the null strings I'm getting when it fails with GetLocalizedString. Maybe it only works at runtime.

    Thanks for the speedy response!
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,147
    Call WaitForCompletion on the operation and it will make sure its completed.

    This should work at all times, maybe its because you have IsEmpty true?
     
  6. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    257
    Ah, great. That's useful.

    Possible. But it's not really an issue now that I have the other check. I'm more worried it might randomly spring on me and change results from null to something unexpected sometime in the future.
     
    karl_jones likes this.