docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Method GetLocalizedStringAsync

    GetLocalizedStringAsync()

    Provides a translated string from a StringTable with the TableReference and the translated string that matches TableEntryReference.

    Declaration
    public AsyncOperationHandle<string> GetLocalizedStringAsync()
    Returns
    Type Description
    AsyncOperationHandle<string>

    Returns the loading operation for the request.

    Remarks

    The UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.Completed event provides a notification once the operation has finished and the string has been found or an error has occurred. A string table may have already been loaded during a previous operation or when using Preload mode. Check the UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.IsDone property to see if the string table has already been loaded and the translated string is immediately available. See Async operation handling for further details. To force the operation to complete immediately, call UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion().

    Examples

    This example shows how GetLocalizedStringAsync() can be used to request an updated string when the SelectedLocale changes.

    public class LocalizedStringGetExample : MonoBehaviour
    {
        public Text myText;
    
        public LocalizedString myString = new LocalizedString
        {
            TableReference = "My String Table",
            TableEntryReference = "My Game Text"
        };
    
        void OnEnable()
        {
            LocalizationSettings.SelectedLocaleChanged += LocaleChanged;
            LoadString();
        }
    
        void OnDisable()
        {
            LocalizationSettings.SelectedLocaleChanged -= LocaleChanged;
        }
    
        void LocaleChanged(Locale locale)
        {
            LoadString();
        }
    
        void LoadString()
        {
            var operation = myString.GetLocalizedStringAsync();
            UpdateString(operation);
        }
    
        void UpdateString(AsyncOperationHandle<string> value)
        {
            if (!value.IsDone)
            {
                // Defer the callback until the operation is finished
                value.Completed += UpdateString;
                return;
            }
    
            myText.text = value.Result;
        }
    }

    This example shows how GetLocalizedStringAsync() can be forced to complete immediately using UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion().

    public class LocalizedStringSynchronousGetExample : MonoBehaviour
    {
        public Text myText;
    
        public LocalizedString myString = new LocalizedString
        {
            TableReference = "My String Table",
            TableEntryReference = "My Game Text"
        };
    
        void OnEnable()
        {
            LocalizationSettings.SelectedLocaleChanged += LocaleChanged;
            LoadString();
        }
    
        void OnDisable()
        {
            LocalizationSettings.SelectedLocaleChanged -= LocaleChanged;
        }
    
        void LocaleChanged(Locale locale)
        {
            LoadString();
        }
    
        void LoadString()
        {
            var operation = myString.GetLocalizedStringAsync();
            operation.WaitForCompletion(); // Force synchronous loading
            myText.text = operation.Result;
        }
    }

    GetLocalizedStringAsync(params object[])

    Provides a translated string from a StringTable with the TableReference and the translated string that matches TableEntryReference.

    Declaration
    public AsyncOperationHandle<string> GetLocalizedStringAsync(params object[] arguments)
    Parameters
    Type Name Description
    object[] arguments

    The arguments to pass into the Smart String formatter or String.Format.

    Returns
    Type Description
    AsyncOperationHandle<string>

    Returns the loading operation for the request.

    GetLocalizedStringAsync(IList<object>)

    Provides a translated string from a StringTable with the TableReference and the translated string that matches TableEntryReference.

    Declaration
    public AsyncOperationHandle<string> GetLocalizedStringAsync(IList<object> arguments)
    Parameters
    Type Name Description
    IList<object> arguments

    The arguments to pass into the Smart String formatter or String.Format.

    Returns
    Type Description
    AsyncOperationHandle<string>

    Returns the loading operation for the request.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)