Search Unity

WaitForCompletion Exception in WebGL when calling GetLocalizedString

Discussion in 'Localization Tools' started by Arkuni, Jul 7, 2022.

  1. Arkuni

    Arkuni

    Joined:
    May 30, 2013
    Posts:
    58
    Getting WaitForCompletion exception when trying to call LocalizedString.GetLocalizedString() after changing language.

    What is the reason for this? I am guessing it is because the file hasn't been loaded and must be loaded as an async web request.

    Is there is any quick fix that doesn't require me to go and refactor all possible places where it could go wrong?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Unfortunately WaitForCompletion is not supported on WebGl, this means if you use an synchronous call such as GetLocalizedString and that asset is not loaded then it will fail.
    There are some ways to work around this. Marking your tables as preloaded should ensure that they are all loaded however switching languages will still require you to wait for the LocalizationSettings.InitialozationOperation to finish.
    If you use the StringChanged event instead of calling GetLocalizedString then it will handle everything for you however it will become async. If all your code relies on sync then you could try switching to a loading scene when someone changes the language and waiting for the preloading of tables to complete before switching back. There's a sample in the package that shows a simple loading screen.
    In short, you want to mark the tables as preload and ensure you wait for initialization when starting and changing language.
     
  3. Arkuni

    Arkuni

    Joined:
    May 30, 2013
    Posts:
    58
    Thanks! Great start. Got a few follow-ups.

    Is LocalizationSettings.InitializationOperation for when the app starts, or every time you switch language?

    Is there a wait to know if the current language is being loaded?

    Is it possible to preload only on WebGL ?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Initialization operation is for application start and any time the selected language is changed.

    You can use the selected locale changed event combined with checking the initialization operation to know if the language is being loaded. You can check initialization operation at any time to see if IsDone is true.

    Preloading works on all platforms.