Class LocalizedString | Localization | 0.7.1-preview
docs.unity3d.com
    Show / Hide Table of Contents

    Class LocalizedString

    A Localized String contains a reference to a StringTableEntry inside of a specific StringTable. This provides a centralized way to work with localized strings.

    Inheritance
    Object
    LocalizedReference
    LocalizedString
    Inherited Members
    LocalizedReference.TableReference
    LocalizedReference.TableEntryReference
    LocalizedReference.IsEmpty
    LocalizedReference.SetReference(TableReference, TableEntryReference)
    LocalizedReference.ToString()
    Namespace: UnityEngine.Localization
    Syntax
    [Serializable]
    public class LocalizedString : LocalizedReference

    Properties

    Arguments

    Arguments that will be passed through to Smart Format. These arguments are not serialized and will need to be set during play mode.

    Declaration
    public object[] Arguments { get; set; }
    Property Value
    Type Description
    Object[]

    CurrentLoadingOperation

    The current loading operation for the string. A string may not be immediately available, such as when loading the StringTable, so all string operations are wrapped with an . See also RefreshString()

    Declaration
    public AsyncOperationHandle<LocalizedDatabase<StringTable, StringTableEntry>.TableEntryResult>? CurrentLoadingOperation { get; }
    Property Value
    Type Description
    Nullable<AsyncOperationHandle<LocalizedDatabase.TableEntryResult<>>>

    Methods

    ClearChangeHandler()

    Removes the handler and stops listening to changes to SelectedLocaleChanged.

    Declaration
    public void ClearChangeHandler()

    ForceUpdate()

    Declaration
    protected override void ForceUpdate()
    Overrides
    LocalizedReference.ForceUpdate()

    GetLocalizedString()

    Declaration
    public AsyncOperationHandle<string> GetLocalizedString()
    Returns
    Type Description
    AsyncOperationHandle<String>

    GetLocalizedString(Object[])

    Loads the requested string table and return the translated string after being formatted using the provided arguments. The Completed event will provide notification once the operation has finished and the string has been found or an error has occurred, this will be called during LateUpdate. It is possible that a string table may have already been loaded, such as during a previous operation or when using Preload mode, the IsDone property can be checked as it is possible the translated string is immediately available.

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

    Arguments that will be passed to Smart Format or String.Format.

    Returns
    Type Description
    AsyncOperationHandle<String>

    RefreshString()

    Forces a refresh of the string when using a LocalizedString.ChangeHandler. Note, this will only only force the refresh if there is currently no loading operation, if one is still being executed then it will be ignored and false will be returned. If a string is not static and will change during game play, such as when using format arguments, then this can be used to force the string update itself.

    Declaration
    public bool RefreshString()
    Returns
    Type Description
    Boolean

    True if a refresh was requested or false if it could not.

    RegisterChangeHandler(LocalizedString.ChangeHandler)

    Register a handler that will be called whenever a localized string is available. When a handler is registered, the string will then be automatically loaded whenever the SelectedLocaleChanged is changed, during initialization and if RefreshString() is called. when not using a change handler.

    Declaration
    public void RegisterChangeHandler(LocalizedString.ChangeHandler handler)
    Parameters
    Type Name Description
    LocalizedString.ChangeHandler handler
    Examples

    This example shows how we can fetch and update a single string value.

    public class LocalizedStringWithChangeHandlerExample : MonoBehaviour
    {
        // A LocalizedString provides an interface to retrieving translated strings.
        // This example assumes a String Table Collection with the name "My String Table" and an entry with the Key "Hello World" exists.
        // You can change the Table Collection and Entry target in the inspector.
        public LocalizedString stringRef = new LocalizedString() { TableReference = "My String Table", TableEntryReference = "Hello World" };
        string m_TranslatedString;
    
        void OnEnable()
        {
            stringRef.RegisterChangeHandler(UpdateString);
        }
    
        void OnDisable()
        {
            stringRef.ClearChangeHandler();
        }
    
        void UpdateString(string translatedValue)
        {
            m_TranslatedString = translatedValue;
            Debug.Log("Translated Value Updated: " + translatedValue);
        }
    
        void OnGUI()
        {
            GUILayout.Label(m_TranslatedString);
        }
    }
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023