using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Localization.Metadata;
using UnityEngine.Localization.Pseudo;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.SmartFormat;
using UnityEngine.Localization.SmartFormat.Core.Formatting;
using UnityEngine.Localization.SmartFormat.GlobalVariables;
namespace UnityEngine.Localization.Tables
{
///
/// A entry.
///
public class StringTableEntry : TableEntry
{
FormatCache m_FormatCache;
///
/// Used when is true and is called.
/// Contains information about the format including any that were used.
///
public FormatCache FormatCache
{
get => m_FormatCache;
set => m_FormatCache = value;
}
///
/// The raw localized value without any formatting applied.
///
public string Value
{
get => Data.Localized;
set
{
Data.Localized = value;
if (m_FormatCache != null)
{
FormatCachePool.Release(m_FormatCache);
m_FormatCache = null;
}
}
}
///
/// Is the entry marked with the ?
/// Entries that are smart will use to format the localized text.
///
public bool IsSmart
{
get => HasTagMetadata() || Data.Metadata.GetMetadata() != null;
set
{
if (value)
{
if (m_FormatCache != null)
{
FormatCachePool.Release(m_FormatCache);
m_FormatCache = null;
}
AddTagMetadata();
}
else
{
RemoveTagMetadata();
}
}
}
internal StringTableEntry()
{
}
///
/// Attempts to remove the entry from the that it belongs to.
/// If is null then a warning will be produced.
///
public void RemoveFromTable()
{
var stringTable = Table as StringTable;
if (stringTable == null)
{
Debug.LogWarning($"Failed to remove {nameof(StringTableEntry)} with id {KeyId} and value `{Value}` as it does not belong to a table.");
}
else
{
stringTable.Remove(KeyId);
}
}
internal FormatCache GetOrCreateFormatCache()
{
if (m_FormatCache == null && !string.IsNullOrEmpty(Data.Localized))
{
m_FormatCache = FormatCachePool.Get(LocalizationSettings.StringDatabase.SmartFormatter.Parser.ParseFormat(Data.Localized, LocalizationSettings.StringDatabase.SmartFormatter.GetNotEmptyFormatterExtensionNames()));
m_FormatCache.Table = Table;
}
return m_FormatCache;
}
///
/// Returns the localized text after formatting has been applied.
/// This will use SmartFormat if is true else it will return the raw unformatted value.
///
///
/// The following process is applied when generating a localized string:
/// 
///
///
public string GetLocalizedString() => GetLocalizedString(null, null, LocalizationSettings.SelectedLocaleAsync.Result as PseudoLocale);
///
/// Returns the localized text after formatting has been applied.
/// Formatting will use SmartFormat if is true else it will default to String.Format.
///
/// Arguments that will be applied to Smart Format or String.Format.
///
public string GetLocalizedString(params object[] args) => GetLocalizedString(null, args, LocalizationSettings.SelectedLocaleAsync.Result as PseudoLocale);
///
/// Returns the localized text after formatting has been applied.
/// Formatting will use SmartFormat if is true else it will default to String.Format.
///
/// Arguments that will be applied to Smart Format or String.Format.
///
public string GetLocalizedString(IList