mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Robust.Shared.Localization.Macros
|
|
{
|
|
public class MacroFormatter : ICustomFormatter
|
|
{
|
|
private readonly IDictionary<string, ITextMacro> Macros;
|
|
|
|
public MacroFormatter(IDictionary<string, ITextMacro> macros)
|
|
{
|
|
Macros = macros;
|
|
}
|
|
|
|
public string Format(string? format, object? arg, IFormatProvider? formatProvider)
|
|
{
|
|
IFormatProvider? fallbackProvider = GetFallbackFormatProvider(formatProvider);
|
|
|
|
if (format == null || format == "")
|
|
return string.Format(fallbackProvider, "{0}", arg);
|
|
|
|
bool capitalized = char.IsUpper(format[0]);
|
|
string lowerCasedFunctionName = format.ToLower();
|
|
|
|
if (!Macros.TryGetValue(lowerCasedFunctionName, out var grammarFunction))
|
|
return string.Format(fallbackProvider, "{0:" + format + '}', arg);
|
|
|
|
return capitalized
|
|
? grammarFunction.CapitalizedFormat(arg)
|
|
: grammarFunction.Format(arg);
|
|
}
|
|
|
|
private static IFormatProvider? GetFallbackFormatProvider(IFormatProvider? formatProvider)
|
|
{
|
|
if (formatProvider is MacroFormatProvider macroFormatProvider)
|
|
return macroFormatProvider.CultureInfo;
|
|
else
|
|
return formatProvider;
|
|
}
|
|
}
|
|
}
|