using System;
using System.Collections.Generic;
using UnityEngine.Localization.Operations;
using UnityEngine.Localization.Pseudo;
using UnityEngine.Localization.SmartFormat;
using UnityEngine.Localization.SmartFormat.PersistentVariables;
using UnityEngine.Localization.Tables;
using UnityEngine.Pool;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace UnityEngine.Localization.Settings
{
///
/// Handles loading strings and their tables for the selected locale.
///
[Serializable]
public class LocalizedStringDatabase : LocalizedDatabase
{
[SerializeField]
MissingTranslationBehavior m_MissingTranslationState = MissingTranslationBehavior.ShowMissingTranslationMessage;
///
///
///
///
///
///
///
///
public delegate void MissingTranslation(string key, long keyId, TableReference tableReference, StringTable table, Locale locale, string noTranslationFoundMessage);
///
/// Event is sent when a Table does not have a translation for a specified Locale.
///
///
/// This example shows how to listen for missing translation event notifications.
///
///
public event MissingTranslation TranslationNotFound;
const string k_DefaultNoTranslationMessage = "No translation found for '{key}' in {table.TableCollectionName}";
[SerializeField]
[Tooltip("The string that will be used when a localized value is missing. This is a Smart String which has access to the following placeholders:\n" +
"\t{key}: The name of the key\n" +
"\t{keyId}: The numeric Id of the key\n" +
"\t{table}: The table object, this can be further queried, for example {table.TableCollectionName}\n" +
"\t{locale}: The locale asset, this can be further queried, for example {locale.name}")]
string m_NoTranslationFoundMessage = k_DefaultNoTranslationMessage;
[SerializeReference]
SmartFormatter m_SmartFormat = Smart.CreateDefaultSmartFormat();
StringTable m_MissingTranslationTable;
///
/// The message to display when a string can not be localized.
/// This is a [Smart String](../manual/Smart/SmartStrings.html) 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}.
///
///
///
public string NoTranslationFoundMessage
{
get => m_NoTranslationFoundMessage;
set => m_NoTranslationFoundMessage = value;
}
///
/// Controls how Unity will handle missing translation values.
///
public MissingTranslationBehavior MissingTranslationState
{
get => m_MissingTranslationState;
set => m_MissingTranslationState = value;
}
///
/// The that will be used for all smart string operations.
///
public SmartFormatter SmartFormatter
{
get => m_SmartFormat;
set => m_SmartFormat = value;
}
///
/// Attempts to retrieve a string from the requested table.
/// This method is asynchronous and may not have an immediate result.
/// Check [IsDone](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.IsDone) to see if the data is available,
/// if it is false then you can use the [Completed](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.Completed) event to get a callback when it is finished,
/// yield on the operation or call [WaitForCompletion](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion)
/// to force the operation to complete.
///
/// A reference to the entry in the
/// The to use instead of the default
/// A Enum which determines if a Fallback should be used when no value could be found for the Locale.
/// Arguments passed to SmartFormat or String.Format.
///
///
/// This example shows how to get a localized string from the of a custom locale (not the currently selected locale) and use WaitForCompletion to force it to complete.
///
///
///
/// This example shows how to get a localized string from the of a custom locale (not the currently selected locale) and use a coroutine to wait for it to complete.
///
///
public AsyncOperationHandle GetLocalizedStringAsync(TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, params object[] arguments)
{
return GetLocalizedStringAsyncInternal(GetDefaultTable(), tableEntryReference, arguments, locale, fallbackBehavior, null);
}
///
/// Attempts to retrieve a string from the requested table.
/// Uses [WaitForCompletion](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion) to force the loading to complete synchronously.
/// Please note that [WaitForCompletion](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion) is not supported on
/// [WebGL](https://docs.unity3d.com/Packages/com.unity.addressables@latest/index.html?subfolder=/manual/SynchronousAddressables.html#webgl).
///
/// A reference to the entry in the
/// The to use instead of the default
/// A Enum which determines if a Fallback should be used when no value could be found for the Locale.
/// Arguments passed to SmartFormat or String.Format.
///
public string GetLocalizedString(TableEntryReference tableEntryReference, Locale locale = null, FallbackBehavior fallbackBehavior = FallbackBehavior.UseProjectSettings, params object[] arguments)
{
return GetLocalizedString(GetDefaultTable(), tableEntryReference, arguments, locale, fallbackBehavior);
}
///
/// Attempts to retrieve a string from the requested table.
/// This method is asynchronous and may not have an immediate result.
/// Check [IsDone](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.IsDone) to see if the data is available,
/// if it is false then you can use the [Completed](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.Completed) event to get a callback when it is finished,
/// yield on the operation or call [WaitForCompletion](xref:UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle.WaitForCompletion)
/// to force the operation to complete.
///
/// A reference to the entry in the
/// Arguments passed to SmartFormat or String.Format.
/// The to use instead of the default
/// A Enum which determines if a Fallback should be used when no value could be found for the Locale.
///
///
/// This example shows how to get a localized string from the and use the Completed event to display it.
///
///
public AsyncOperationHandle GetLocalizedStringAsync(TableEntryReference tableEntryReference, IList