using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Localization.SmartFormat.Core.Extensions;
using UnityEngine.Localization.SmartFormat.Core.Formatting;
using UnityEngine.Localization.SmartFormat.Core.Output;
using UnityEngine.Localization.SmartFormat.Core.Parsing;
using UnityEngine.Localization.SmartFormat.Core.Settings;
namespace UnityEngine.Localization.SmartFormat
{
///
/// This class contains the Format method that constructs
/// the composite string by invoking each extension.
///
[Serializable]
public class SmartFormatter : ISerializationCallbackReceiver
{
[SerializeReference]
SmartSettings m_Settings;
[SerializeReference]
Parser m_Parser;
[SerializeReference]
List m_Sources;
[SerializeReference]
List m_Formatters;
List m_NotEmptyFormatterExtensionNames;
static readonly object[] k_Empty = { null };
///
/// Event raising, if an error occurs during formatting.
///
public event EventHandler OnFormattingFailure;
///
/// Gets the list of source extensions.
///
public List SourceExtensions { get => m_Sources; }
///
/// Gets the list of formatter extensions.
///
public List FormatterExtensions => m_Formatters;
///
/// Creates a new instance of SmartFormatter.
///
public SmartFormatter()
{
m_Settings = new SmartSettings();
m_Parser = new Parser(m_Settings);
m_Sources = new List();
m_Formatters = new List();
}
///
/// Gets all names of registered formatter extensions which are not empty.
///
///
public List GetNotEmptyFormatterExtensionNames()
{
if (m_NotEmptyFormatterExtensionNames != null)
return m_NotEmptyFormatterExtensionNames;
m_NotEmptyFormatterExtensionNames = new List();
foreach (var extension in FormatterExtensions)
{
if (extension?.Names != null)
{
foreach (var name in extension.Names)
{
if (!string.IsNullOrEmpty(name))
m_NotEmptyFormatterExtensionNames.Add(name);
}
}
}
return m_NotEmptyFormatterExtensionNames;
}
///
/// Adds each extensions to this formatter.
/// Each extension must implement ISource.
///
///
public void AddExtensions(params ISource[] sourceExtensions)
{
SourceExtensions.InsertRange(0, sourceExtensions);
}
///
/// Adds each extensions to this formatter.
/// Each extension must implement IFormatter.
///
///
public void AddExtensions(params IFormatter[] formatterExtensions)
{
m_NotEmptyFormatterExtensionNames = null;
FormatterExtensions.InsertRange(0, formatterExtensions);
}
///
/// Searches for a Source Extension of the given type, and returns it.
/// This can be used to easily find and configure extensions.
/// Returns null if the type cannot be found.
///
///
///
public T GetSourceExtension() where T : class, ISource
{
return SourceExtensions.OfType().FirstOrDefault();
}
///
/// Searches for a Formatter Extension of the given type, and returns it.
/// This can be used to easily find and configure extensions.
/// Returns null if the type cannot be found.
///
///
///
public T GetFormatterExtension() where T : class, IFormatter
{
return FormatterExtensions.OfType().FirstOrDefault();
}
///
/// Gets or set the instance of the
///
public Parser Parser
{
get => m_Parser;
set => m_Parser = value;
}
///
/// Get the for Smart.Format
///
public SmartSettings Settings
{
get => m_Settings;
set => m_Settings = value;
}
///
/// Replaces one or more format items in a specified string with the string representation of a specific object.
///
/// A composite format string.
/// The object to format.
/// Returns the formatted input with items replaced with their string representation.
public string Format(string format, params object[] args)
{
return Format(null, args, format);
}
///
/// Replaces one or more format items in a specified string with the string representation of a specific object.
///
/// The list of objects to format.
/// A composite format string.
/// Returns the formatted input with items replaced with their string representation.
public string Format(IList