Performance issue with localization - GetLocalizedString

Hello! I am facing a performance issue in my project using localization…

I have created this function to obtain the text async from a localizedString, however I find that when I run it my game suffers a performance spike especially on the first execution and I don’t understand why…

 public async Task<string> GetText() 
{
    AsyncOperationHandle<string> operationAsync = localizedString.GetLocalizedStringAsync();
    Addressables.ResourceManager.Acquire(operationAsync);

    await operationAsync.Task;

    if (operationAsync.IsDone) 
    {
        if (operationAsync.Result.Length == 0) 
        {
            Dev.LogWarning("No text found for localizedString. Table: " + localizedString.TableReference.TableCollectionName + " key: " + localizedString.TableEntryReference.Key);
        }
    } 
    else 
    {
        Debug.LogError("Cannot get localized string");
    }

    return operationAsync.Result;
}

Am I missing some localization? I have tried preloading the different tables but I hardly notice any differences… Any thoughts on this?

That is in the editor but in my android compilation the peaks reach up to 2000ms!! :dizzy_face:

Since GetLocalizedStringAsync() relies on Addressables to load stuff asynchronously, it can cause that initial lag while it fetches the asset from disk or memory. Even though you said you tried preloading, make sure you’re preloading all the relevant tables before you call GetLocalizedStringAsync(). Or instead of calling GetLocalizedStringAsync() every time, try caching the result after the first load. So, once a string is fetched, store it and reuse it to avoid the performance hit next time.

The preloading occurs when Localization initializes itself which tends to be the first time you use it, so its possible that the spike is the initialization.
You can force initialziation to occur earlier, such as in a loading screen, by calling LocalizationSettings.InitializationOperation