How to check if GetLocalizedString succeeded?

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.

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.

You could try using GetLocalizedStringAsync which includes an error message and exception in the operation.
GetLocalizedString should include the error in the string if something goes wrong. This can be configured under the No Translation Found Format field.

We also have a global event you can hook into when a translation is not found LocalizationSettings.StringDatabase.TranslationNotFound

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!

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?

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.

1 Like