Class LocalizedString
Provides a way to reference a String
Inherited Members
Namespace: UnityEngine .Localization
Assembly: Unity.Localization.dll
Syntax
[Serializable]
public class LocalizedString : LocalizedReference, ISerializationCallbackReceiver, IVariableGroup, IVariableValueChanged, IVariable
Examples
This example shows how to localize a MonoBehaviour so that it updates automatically when the active locale changes. This example uses asynchronous loading to load the localized assets in the background.
public class ExampleMonoBehaviour1 : MonoBehaviour
{
public LocalizedString myLocalizedString = new LocalizedString("My Table", "My Entry");
public string TranslatedValue { get; private set; }
void Start()
{
// Register to get an update when the string is changed.
myLocalizedString.StringChanged += ValueChanged;
}
void ValueChanged(string value)
{
TranslatedValue = value;
Debug.Log("Value changed to: " + value);
}
}
This example shows how to localize a MonoBehaviour immediately. This example uses synchronous loading, which may cause a pause when first loading the localized assets.
public class ExampleMonoBehaviour2 : MonoBehaviour
{
public LocalizedString myLocalizedString = new LocalizedString("My Table", "My Entry");
public string TranslatedValue => myLocalizedString.GetLocalizedString();
void OnGUI()
{
GUILayout.Label(TranslatedValue);
}
}
This example shows how to localize a ScriptableObject to represent a quest in a game.
[CreateAssetMenu()]
public class GameQuest : ScriptableObject
{
[SerializeField] LocalizedString questTitle = new LocalizedString("My Quests", "QUEST_1_TITLE");
[SerializeField] LocalizedString questDescription = new LocalizedString("My Quests", "QUEST_1_DESCRIPTION");
[SerializeField] LocalizedString success = new LocalizedString("My Quests", "QUEST_1_SUCCESS");
[SerializeField] LocalizedString failure = new LocalizedString("My Quests", "QUEST_1_FAILURE");
[SerializeField] float rewardMoney = 100;
public string Title => questTitle.GetLocalizedString();
public string Description => questDescription.GetLocalizedString();
public string Failure => failure.GetLocalizedString();
// Return the success message with the reward money included.
// For example:
// "Congratulations, you saved the village! Please take this reward of {0:C} as a sign of appreciation"
// in English(GB) would become
// "Congratulations, you saved the village! Please take this reward of £100 as a sign of appreciation"
public string Success => success.GetLocalizedString(rewardMoney);
}
This example shows how to use local variables to represent a health counter.
public class HealthCounterExample : MonoBehaviour
{
public Text uiText;
public float delay = 5;
// Some example English strings could be:
// "{player-name} has {player-health} health"
// "{player-name} {player-health:cond:<100?has {} remaing health|is at full health}"
public LocalizedString myLocalizedString = new LocalizedString("My Table", "My Entry")
{
{ "player-name", new StringVariable { Value = "Player 1" } },
{ "player-health", new IntVariable { Value = 100 } }
};
IEnumerator Start()
{
// Register to get an update when the string is changed.
// This will be called every time the playerHealth variable is modified.
myLocalizedString.StringChanged += val => uiText.text = val;
var playerHealth = myLocalizedString["player-health"] as IntVariable;
var wait = new WaitForSeconds(delay);
while (playerHealth.Value > 0)
{
yield return wait;
// Changing the value triggers the LocalizedString to update itself.
playerHealth.Value -= Random.Range(1, 10);
}
}
}
This example shows how to bind to a UI Toolkit property. Note that this requires Unity 2023.2 and above.
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.UIElements;
public class LocalizedStringUIDocumentExample : MonoBehaviour
{
void Start()
{
var document = GetComponent<UIDocument>();
var label = new Label();
label.text = "Default Text";
document.rootVisualElement.Add(label);
// Add binding to the text property of the label.
var localizedString = new LocalizedString("My table", "My Entry");
label.SetBinding("text", localizedString);
}
}
This example shows how to use local variables when you bind to a UI Toolkit property. Note that this requires Unity 2023.2 and above.
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.SmartFormat.PersistentVariables;
using UnityEngine.UIElements;
[RequireComponent(typeof(UIDocument))]
public class LocalizedStringVariablesUIDocumentExample : MonoBehaviour
{
IntVariable m_Counter;
void Start()
{
var document = GetComponent<UIDocument>();
var label = new Label();
document.rootVisualElement.Add(label);
// Add binding to the text property of the label.
// Example Smart string "The current count is {counter}."
m_Counter = new IntVariable { Value = 1 };
var localizedString = new LocalizedString("My Table", "My Entry");
// Add the variable
localizedString.Add("counter", m_Counter);
label.SetBinding("text", localizedString);
// Localize the button labels
var buttonIncrement = new Button();
var buttonDecrement = new Button();
buttonIncrement.SetBinding("text", new LocalizedString("My Table", "Increment Button Label"));
buttonDecrement.SetBinding("text", new LocalizedString("My Table", "Decrement Button Label"));
buttonIncrement.clicked += () => m_Counter.Value++;
buttonDecrement.clicked += () => m_Counter.Value--;
document.rootVisualElement.Add(buttonIncrement);
document.rootVisualElement.Add(buttonDecrement);
document.rootVisualElement.Add(label);
}
}
Constructors
Name | Description |
---|---|
Localized |
Initializes and returns an empty instance of a Localized |
Localized |
Initializes and returns an instance of a Localized |
Properties
Name | Description |
---|---|
Arguments | Arguments that will be passed through to Smart Format. These arguments are not serialized and will need to be set at runtime. See Add(string, IVariable) to add persistent serialized arguments. |
Count | Returns the number of local variables inside this localized string. |
Current |
The current loading operation for the string when using String |
Current |
The current loading operation for the string when using String |
Has |
Returns true if String |
Is |
Implemented as part of the IDictionary interface but not actually used. Will always return false. |
this[string] | Gets or sets the IVariable with the specified name. |
Keys | Returns a collection containing all the unique local variable names. |
Values | Returns all the local variables for this localized string. |
Methods
Name | Description |
---|---|
Add(Key |
Adds a new Local Variable to use during formatting. |
Add(string, IVariable) | Adds a new Local Variable to use during formatting. |
Clear() | Removes all local variables. |
Clear |
|
Contains(Key |
Returns true if a local variable with the specified name exists. |
Contains |
Returns true if a local variable with the specified name exists. |
Copy |
Copies the local variables into an array starting at |
Force |
|
Get |
Returns an enumerator for all local variables in this localized string. |
Get |
Provides a translated string from a String |
Get |
Provides a translated string from a String |
Get |
Provides a translated string from a String |
Get |
Provides a translated string from a String |
Get |
Provides a translated string from a String |
Get |
Provides a translated string from a String |
Get |
The value that will be used when the smart string matches this variable. This value can then be further used by additional sources/formatters. |
Refresh |
Provides a way to force a refresh of the string when using String |
Register |
|
Remove(Key |
Removes a local variable with the specified key. |
Remove(string) | Removes a local variable with the specified name. |
Reset() | Clears the current loading operation. |
Try |
Gets the IVariable with the specified name. |
Events
Name | Description |
---|---|
String |
Provides a callback that will be invoked when the translated string has changed. The following events will trigger an update:
|
Value |
This event is sent when the global variable has changed or wishes to trigger an update to any Localized |