Search Unity

Resolved Load a localized text file at runtime

Discussion in 'Localization Tools' started by JulianNeil, May 11, 2023.

  1. JulianNeil

    JulianNeil

    Joined:
    Jun 27, 2022
    Posts:
    78
    I am trying to load a dictionary stored in a .txt file at runtime. The dictionary needs to be localized.

    It is straightforward to store this txt file in my Assets directory, mark it as an Addressable, and load and process it at runtime via an AssetReference . This I understand.

    But once I try to add localization into the mix I get a bit lost and confused.
    I have created a LocalizationTable for this TextAsset.
    I dont understand how to link to this localised asset in a MonoBehaviour in a way that allows me to load it at runtime, and prevents it from being loaded automatically.

    I am new to both Addressables and Localization - so if there is an example someone can point me to I'm happy to experiment further. I have searched without much luck.

    Jules.

    I should mention this is a mobile game and that I'm using Unity Editor 2021.3
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    You want to create a LocalizedAsset for the text asset type.
    https://docs.unity3d.com/Packages/c...calization.LocalizedAsset-1.html#constructors

    Something like this

    Code (csharp):
    1. public class LocalizedTextExample : MonoBehaviour
    2. {
    3.     [Serializable]
    4.     public class LocalizedTextAsset : LocalizedAsset<TextAsset>{}
    5.  
    6.     public LocalizedTextAsset localizedText;
    7.  
    8.     void OnEnable()
    9.     {
    10.         localizedText.AssetChanged += UpdatePrefab;
    11.     }
    12.  
    13.     void OnDisable()
    14.     {
    15.         localizedText.AssetChanged -= UpdatePrefab;
    16.     }
    17.  
    18.     void UpdatePrefab(TextAsset value)
    19.     {
    20.         Debug.Log(text);
    21.     }
    22. }
     
  3. JulianNeil

    JulianNeil

    Joined:
    Jun 27, 2022
    Posts:
    78
    Hi @karl_jones. Does that gives me control over loading and unloading the asset from memory? I need to process it and unload it to save memory.

    I can control loading from Resources.. and I can control loading from an AssetReference.. but I seem to lose that control if I use a LocalizedTextAsset like the one you suggested
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    No, this example will keep the item in memory until the language changes.
    Here is an example to control loading:

    Code (csharp):
    1. public class LocalizedTextExample : MonoBehaviour
    2. {
    3.     [Serializable]
    4.     public class LocalizedTextAsset : LocalizedAsset<TextAsset>{}
    5.  
    6.     public LocalizedTextAsset localizedText;
    7.  
    8.     IEnumerator Start()
    9.     {
    10.         // Load the asset table
    11.         var tableOperation = LocalizationSettings.AssetDatabase.GetTableAsync(localizedText.TableReference);
    12.         yield return tableOperation;
    13.  
    14.         // Load the asset
    15.         var assetOperation = tableOperation.GetAssetAsync(localizedText.TableEntryReference);
    16.         yield return assetOperation;
    17.  
    18.         Debug.Log(assetOperation.Result.text);
    19.  
    20.         // Release the asset
    21.         table.ReleaseAsset(localizedText.TableEntryReference);
    22.     }
    23. }
     
    JulianNeil likes this.
  5. JulianNeil

    JulianNeil

    Joined:
    Jun 27, 2022
    Posts:
    78
    Ok. that gives me something to work with and some APIs to read up on. Thanks @karl_jones .

    What exactly in your second example stops the asset from being loaded automatically?
    The fact that the AssetChanged is not subscribed?

    Is there an event that I can hook into to know when the locale has changed so I can asynchronously load the asset ( rather than doing it from an asychronous Start )? One that doesn't automatically load the assets?

    Apologies if this should be obvious. The whole Localization system seems powerful - but a bit opaque.

    Jules
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,293
    Yes, subscribing to it will trigger the loading.

    https://docs.unity3d.com/Packages/c...ne_Localization_LocalizedAsset_1_AssetChanged

    LocalizationSettings.SelectedLocaleChanged
     
    JulianNeil likes this.
  7. JulianNeil

    JulianNeil

    Joined:
    Jun 27, 2022
    Posts:
    78
    Thanks heaps @karl_jones . you've saved me a lot of time.
     
    karl_jones likes this.