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.
Inherited Members
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 when using StringChanged. A string may not be immediately available, such as when loading the StringTable, so all string operations are wrapped with an AsyncOperationHandle. See also RefreshString()
Declaration
public AsyncOperationHandle<LocalizedDatabase<StringTable, StringTableEntry>.TableEntryResult> CurrentLoadingOperation { get; }
Property Value
Type | Description |
---|---|
AsyncOperationHandle<LocalizedDatabase.TableEntryResult<>> |
HasChangeHandler
True if StringChanged has any subscribers.
Declaration
public bool HasChangeHandler { get; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
ForceUpdate()
Declaration
protected override void ForceUpdate()
Overrides
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 StringChanged. 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 to update itself.
Declaration
public bool RefreshString()
Returns
Type | Description |
---|---|
Boolean | True if a refresh was requested or false if it could not. |
Events
StringChanged
Called whenever a localized string is available. When the first LocalizedString.ChangeHandler is added, a loading operation will automatically start and the localized string value will be sent to the event when completed. Any adding additional subscribers added after loading has completed will also be sent the latest localized string value when they are added. This ensures that a subscriber will always have the correct localized value regardless of when it was added. The string will be refreshed whenever SelectedLocaleChanged is changed and when RefreshString() is called. GetLocalizedString() when not using the event.
Declaration
public event LocalizedString.ChangeHandler StringChanged
Event Type
Type | Description |
---|---|
LocalizedString.ChangeHandler |
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.StringChanged += UpdateString;
}
void OnDisable()
{
stringRef.StringChanged -= UpdateString;
}
void UpdateString(string translatedValue)
{
m_TranslatedString = translatedValue;
Debug.Log("Translated Value Updated: " + translatedValue);
}
void OnGUI()
{
GUILayout.Label(m_TranslatedString);
}
}