Class LocalizedStringDatabase
Handles loading strings and their tables for the selected locale.
Inherited Members
Namespace: UnityEngine.Localization.Settings
Syntax
[Serializable]
public class LocalizedStringDatabase : LocalizedDatabase<StringTable, StringTableEntry>, IPreloadRequired, IReset, IDisposable
Properties
MissingTranslationState
Controls how Unity will handle missing translation values.
Declaration
public MissingTranslationBehavior MissingTranslationState { get; set; }
Property Value
Type | Description |
---|---|
MissingTranslationBehavior |
NoTranslationFoundMessage
The message to display when a string can not be localized. This is a Smart String which has access to the following named placeholders:
Placeholder | Description |
---|---|
{key} | The name of the key. |
{keyId} | The numeric Id of the key. |
{table} | The table object, this can be further queried, for example {table.TableCollectionName}. |
{locale} | The locale asset, this can be further queried, for example {locale.name}. |
Declaration
public string NoTranslationFoundMessage { get; set; }
Property Value
Type | Description |
---|---|
String |
SmartFormatter
The SmartFormatter that will be used for all smart string operations.
Declaration
public SmartFormatter SmartFormatter { get; set; }
Property Value
Type | Description |
---|---|
SmartFormatter |
Methods
GenerateLocalizedString(StringTable, StringTableEntry, TableReference, TableEntryReference, Locale, IList<Object>)
Declaration
protected virtual string GenerateLocalizedString(StringTable table, StringTableEntry entry, TableReference tableReference, TableEntryReference tableEntryReference, Locale locale, IList<object> arguments)
Parameters
Type | Name | Description |
---|---|---|
StringTable | table | |
StringTableEntry | entry | |
TableReference | tableReference | |
TableEntryReference | tableEntryReference | |
Locale | locale | |
IList<Object> | arguments |
Returns
Type | Description |
---|---|
String |
GetLocalizedString(TableEntryReference, IList<Object>, Locale, FallbackBehavior)
Attempts to retrieve a string from the requested table. Uses WaitForCompletion to force the loading to complete synchronously. Please note that WaitForCompletion is not supported on WebGL.
Declaration
public string GetLocalizedString(TableEntryReference tableEntryReference, IList<object> arguments, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings)
Parameters
Type | Name | Description |
---|---|---|
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
IList<Object> | arguments | Arguments passed to SmartFormat or String.Format. |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Returns
Type | Description |
---|---|
String |
Examples
This example shows how to get a localized string from the DefaultTable which uses formatting arguments and use the Completed event to display it.
// Example string: "The value is {0}"
var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My String", new object[] { 123 });
stringOperation.Completed += s =>
{
// Example output: "The value is 123"
Debug.Log(s.Result);
};
GetLocalizedString(TableEntryReference, Locale, FallbackBehavior, Object[])
Attempts to retrieve a string from the requested table. Uses WaitForCompletion to force the loading to complete synchronously. Please note that WaitForCompletion is not supported on WebGL.
Declaration
public string GetLocalizedString(TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, params object[] arguments)
Parameters
Type | Name | Description |
---|---|---|
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Object[] | arguments | Arguments passed to SmartFormat or String.Format. |
Returns
Type | Description |
---|---|
String |
GetLocalizedString(TableReference, TableEntryReference, IList<Object>, Locale, FallbackBehavior)
Attempts to retrieve a string from the requested table. The string will first be formatted with UnityEngine.Localization.SmartFormat if IsSmart is enabled otherwise it will use String.Format. Uses WaitForCompletion to force the loading to complete synchronously. Please note that WaitForCompletion is not supported on WebGL.
Declaration
public virtual string GetLocalizedString(TableReference tableReference, TableEntryReference tableEntryReference, IList<object> arguments, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings)
Parameters
Type | Name | Description |
---|---|---|
TableReference | tableReference | A reference to the table to check for the string. |
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
IList<Object> | arguments | Arguments passed to SmartFormat or String.Format. |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Returns
Type | Description |
---|---|
String |
Examples
This example shows how to get a localized string which uses Smart String for formatting.
// Prepare Smart String Arguments.
var dictionary = new Dictionary<string, string>();
dictionary.Add("title", "General");
dictionary.Add("name", "Radahn");
// Example string: "I am {title} {name}!"
var localizedString = LocalizationSettings.StringDatabase.GetLocalizedString("Main Characters", "General_Radahn_Intro", new object[] { dictionary });
// Example output: "I am General Radahn!"
Debug.Log(localizedString);
GetLocalizedString(TableReference, TableEntryReference, Locale, FallbackBehavior, Object[])
Attempts to retrieve a string from the requested table. The string will first be formatted with UnityEngine.Localization.SmartFormat if IsSmart is enabled otherwise it will use String.Format. Uses WaitForCompletion to force the loading to complete synchronously. Please note that WaitForCompletion is not supported on WebGL.
Declaration
public virtual string GetLocalizedString(TableReference tableReference, TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, params object[] arguments)
Parameters
Type | Name | Description |
---|---|---|
TableReference | tableReference | A reference to the table to check for the string. |
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Object[] | arguments | Arguments passed to SmartFormat or String.Format. |
Returns
Type | Description |
---|---|
String |
Examples
This example shows how to get a localized string from a specified table and entry.
var localizedString = LocalizationSettings.StringDatabase.GetLocalizedString("My Table", "My String");
Debug.Log(localizedString);
GetLocalizedStringAsync(TableEntryReference, IList<Object>, Locale, FallbackBehavior)
Attempts to retrieve a string from the requested table. This method is asynchronous and may not have an immediate result. Check IsDone to see if the data is available, if it is false then you can use the Completed event to get a callback when it is finished, yield on the operation or call WaitForCompletion to force the operation to complete.
Declaration
public AsyncOperationHandle<string> GetLocalizedStringAsync(TableEntryReference tableEntryReference, IList<object> arguments, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings)
Parameters
Type | Name | Description |
---|---|---|
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
IList<Object> | arguments | Arguments passed to SmartFormat or String.Format. |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Returns
Type | Description |
---|---|
AsyncOperationHandle<String> |
Examples
This example shows how to get a localized string from the DefaultTable and use the Completed event to display it.
var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My String");
stringOperation.Completed += s =>
{
Debug.Log("String loaded: " + s);
};
GetLocalizedStringAsync(TableEntryReference, Locale, FallbackBehavior, Object[])
Attempts to retrieve a string from the requested table. This method is asynchronous and may not have an immediate result. Check IsDone to see if the data is available, if it is false then you can use the Completed event to get a callback when it is finished, yield on the operation or call WaitForCompletion to force the operation to complete.
Declaration
public AsyncOperationHandle<string> GetLocalizedStringAsync(TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, params object[] arguments)
Parameters
Type | Name | Description |
---|---|---|
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Object[] | arguments | Arguments passed to SmartFormat or String.Format. |
Returns
Type | Description |
---|---|
AsyncOperationHandle<String> |
Examples
This example shows how to get a localized string from the DefaultTable of a custom locale (not the currently selected locale) and use WaitForCompletion to force it to complete.
var customLocale = LocalizationSettings.AvailableLocales.GetLocale("fr");
var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My String", customLocale);
// We can force the operation to complete.
if (!stringOperation.IsDone)
stringOperation.WaitForCompletion();
Debug.Log(stringOperation.Result);
This example shows how to get a localized string from the DefaultTable of a custom locale (not the currently selected locale) and use a coroutine to wait for it to complete.
IEnumerator LoadStringWithCoroutine()
{
var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My String");
// We can skip waiting if the operation is already done.
if (!stringOperation.IsDone)
yield return stringOperation;
Debug.Log(stringOperation.Result);
}
GetLocalizedStringAsync(TableReference, TableEntryReference, IList<Object>, Locale, FallbackBehavior, IVariableGroup)
Attempts to retrieve a string from the requested table. The string will first be formatted with UnityEngine.Localization.SmartFormat if IsSmart is enabled otherwise it will use String.Format. This method is asynchronous and may not have an immediate result. Check IsDone to see if the data is available, if it is false then you can use the Completed event to get a callback when it is finished, yield on the operation or call WaitForCompletion to force the operation to complete.
Declaration
public virtual AsyncOperationHandle<string> GetLocalizedStringAsync(TableReference tableReference, TableEntryReference tableEntryReference, IList<object> arguments, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, IVariableGroup localVariables = null)
Parameters
Type | Name | Description |
---|---|---|
TableReference | tableReference | A reference to the table to check for the string. |
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
IList<Object> | arguments | Arguments passed to SmartFormat or String.Format. |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
IVariableGroup | localVariables | Optional IVariableGroup which can be used to add additional named variables. |
Returns
Type | Description |
---|---|
AsyncOperationHandle<String> |
Examples
This example shows how to get a localized string which uses Smart String for formatting.
// Prepare Smart String Arguments.
var dictionary = new Dictionary<string, string>();
dictionary.Add("title", "General");
dictionary.Add("name", "Radahn");
// Example string: "I am {title} {name}!"
var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("Main Characters", "General_Radahn_Intro", new object[] { dictionary });
stringOperation.Completed += s =>
{
// Example output: "I am General Radahn!"
Debug.Log(s.Result);
};
GetLocalizedStringAsync(TableReference, TableEntryReference, Locale, FallbackBehavior, Object[])
Attempts to retrieve a string from the requested table. The string will first be formatted with UnityEngine.Localization.SmartFormat if IsSmart is enabled otherwise it will use String.Format. This method is asynchronous and may not have an immediate result. Check IsDone to see if the data is available, if it is false then you can use the Completed event to get a callback when it is finished, yield on the operation or call WaitForCompletion to force the operation to complete.
Declaration
public virtual AsyncOperationHandle<string> GetLocalizedStringAsync(TableReference tableReference, TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, params object[] arguments)
Parameters
Type | Name | Description |
---|---|---|
TableReference | tableReference | A reference to the table to check for the string. |
TableEntryReference | tableEntryReference | A reference to the entry in the StringTable |
Locale | locale | The Locale to use instead of the default SelectedLocale |
FallbackBehavior | fallbackBehavior | A Enum which determines if a Fallback should be used when no value could be found for the Locale. |
Object[] | arguments | Arguments passed to SmartFormat or String.Format. |
Returns
Type | Description |
---|---|
AsyncOperationHandle<String> |
Examples
This example shows how to get a localized string from a specified table and entry.
var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My Table", "My String");
stringOperation.Completed += s =>
{
Debug.Log("String loaded: " + s);
};
Events
TranslationNotFound
Event is sent when a Table does not have a translation for a specified Locale.
Declaration
public event LocalizedStringDatabase.MissingTranslation TranslationNotFound
Event Type
Type | Description |
---|---|
LocalizedStringDatabase.MissingTranslation |
Examples
This example shows how to listen for missing translation event notifications.
public void SubscribeToTranslationNotFound()
{
LocalizationSettings.StringDatabase.TranslationNotFound += StringDatabase_TranslationNotFound;
}
void StringDatabase_TranslationNotFound(string key, long keyId, TableReference tableReference, StringTable table, UnityEngine.Localization.Locale locale, string noTranslationFoundMessage)
{
Debug.Log($"Translation Not Found for {key} in {table?.TableCollectionName} for {locale}");
}