Search Unity

Question How to shutdown/initialize the Localization system?

Discussion in 'Localization Tools' started by Peter77, Jul 20, 2021.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Unity 2019.4.20f1, Addressables 1.18.11, Localization 1.0.0-pre.9

    I'm working on content updates with Addressables:
    1. Check for content update
    2. Load scene that contains no Addressable references, except for Localization
    3. Fetch all localized strings and cache them for usage during the content update
    4. Hack to unload all AsyncOperationHandle's that are left-over, this also unloads the Localization StringTables (yes, I'm desperate. I'm working towards a release)
    5. Perform content update
    6. <now i need to initialize and preload localization>
    7. Load main menu for example where it now needs to display localized strings (which are null right now)
    Step 6 is the missing piece in the puzzle.

    Questions:
    1. I don't know how to initialize the Localization, how would I do that?
    2. Is there an option to avoid that the Localization package automagically initializes itself during application start? I would like to have control over it and do the initialization at a specific point during my game startup.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    You should be able to initialize the system by calling LocalizationSettings.InitializationOperation. You can also trigger it by switching languages.

    We add the LocalizationSettings into the PlayerSettings preloaded assets during build time. You could remove this build step by editing the script LocalizationBuildPlayer.cs. If you did not change anything in the settings then you can just call the initialization operation at any point and it will use the default settings in the player. If you did make a change then you will need to make sure it is included in the build, maybe add it to the Resources folder or hold a reference in a script.

    We are very close to our next release, hopefully, this week. It includes the fixes I mentioned to allow you to unload and reload to support your use case.
     
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Thank you for the quick help! :)

    Calling
    InitializationOperation
    didn't cause the Localization to be (re)initialized again, but while reading through the Localization code I found
    LocalizationSettings.ResetState
    which makes the magic happen.

    The following code finally makes content updates work in my game:

    Code (CSharp):
    1. if (UnityEngine.Localization.Settings.LocalizationSettings.HasSettings)
    2.    UnityEngine.Localization.Settings.LocalizationSettings.Instance.ResetState();
    3.  
    4. // Do content update ...
    5.  
    6. yield return UnityEngine.Localization.Settings.LocalizationSettings.InitializationOperation;
    7. yield return UnityEngine.Localization.Settings.LocalizationSettings.StringDatabase.PreloadOperation;  
    Even downloading new StringTables works now. Crossing fingers that all of this isn't going to fall apart with an Addressables or Localization package update :)
     
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Ah great, that was originally intended for the fast enter and exit play mode support ;)
    You should not need to call StringDatabase.PreloadOperation, the Initialization operation will do that for you. No harm in also calling it though, it should just already be completed.