Skip to content

Commit

Permalink
Implement TimeUnitToSymbolExtensions.ToSymbol method
Browse files Browse the repository at this point in the history
  • Loading branch information
hangy committed Jul 3, 2021
1 parent 689802b commit 36adbce
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
Expand Up @@ -1583,6 +1583,10 @@ namespace Humanizer
public static string Humanize(this System.TimeSpan timeSpan, int precision = 1, System.Globalization.CultureInfo culture = null, Humanizer.Localisation.TimeUnit maxUnit = 5, Humanizer.Localisation.TimeUnit minUnit = 0, string collectionSeparator = ", ", bool toWords = False) { }
public static string Humanize(this System.TimeSpan timeSpan, int precision, bool countEmptyUnits, System.Globalization.CultureInfo culture = null, Humanizer.Localisation.TimeUnit maxUnit = 5, Humanizer.Localisation.TimeUnit minUnit = 0, string collectionSeparator = ", ", bool toWords = False) { }
}
public class static TimeUnitToSymbolExtensions
{
public static string ToSymbol(this Humanizer.Localisation.TimeUnit unit, System.Globalization.CultureInfo culture = null) { }
}
public class static To
{
public static Humanizer.ICulturedStringTransformer LowerCase { get; }
Expand Down Expand Up @@ -1852,6 +1856,10 @@ namespace Humanizer.Localisation
{
public static string GetResourceKey(Humanizer.Localisation.TimeUnit unit, int count = 1, bool toWords = False) { }
}
public class static TimeUnitSymbol
{
public static string GetResourceKey(Humanizer.Localisation.TimeUnit unit) { }
}
}
public class static Resources
{
Expand Down Expand Up @@ -1902,6 +1910,7 @@ namespace Humanizer.Localisation.Formatters
protected virtual string GetResourceKey(string resourceKey) { }
public virtual string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit, bool toWords = False) { }
public virtual string TimeSpanHumanize_Zero() { }
public virtual string TimeUnitHumanize(Humanizer.Localisation.TimeUnit timeUnit) { }
}
public interface IFormatter
{
Expand All @@ -1911,6 +1920,7 @@ namespace Humanizer.Localisation.Formatters
string DateHumanize_Now();
string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit, bool toWords = False);
string TimeSpanHumanize_Zero();
string TimeUnitHumanize(Humanizer.Localisation.TimeUnit timeUnit);
}
}
namespace Humanizer.Localisation.NumberToWords
Expand Down
7 changes: 7 additions & 0 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Expand Up @@ -83,6 +83,13 @@ public virtual string DataUnitHumanize(DataUnit dataUnit, double count, bool toS
return resourceValue;
}

/// <inheritdoc />
public virtual string TimeUnitHumanize(TimeUnit timeUnit)
{
var resourceKey = ResourceKeys.TimeUnitSymbol.GetResourceKey(timeUnit);
return Format(resourceKey);
}

private string GetResourceForDate(TimeUnit unit, Tense timeUnitTense, int count)
{
var resourceKey = ResourceKeys.DateHumanize.GetResourceKey(unit, timeUnitTense: timeUnitTense, count: count);
Expand Down
7 changes: 7 additions & 0 deletions src/Humanizer/Localisation/Formatters/IFormatter.cs
Expand Up @@ -51,5 +51,12 @@ public interface IFormatter
/// <param name="toSymbol">Indicates whether the data unit should be expressed as symbol or full word</param>
/// <returns>String representation of the provided DataUnit</returns>
string DataUnitHumanize(DataUnit dataUnit, double count, bool toSymbol = true);

/// <summary>
/// Returns the symbol for the given TimeUnit
/// </summary>
/// <param name="timeUnit">Time unit</param>
/// <returns>String representation of the provided TimeUnit</returns>
string TimeUnitHumanize(TimeUnit timeUnit);
}
}
28 changes: 28 additions & 0 deletions src/Humanizer/Localisation/ResourceKeys.TimeUnitSymbol.cs
@@ -0,0 +1,28 @@
namespace Humanizer.Localisation
{
public partial class ResourceKeys
{
/// <summary>
/// Encapsulates the logic required to get the resource keys for TimeUnit.ToSymbol
/// </summary>
public static class TimeUnitSymbol
{
/// <summary>
/// Examples: TimeUnit_Minute, TimeUnit_Hour.
/// </summary>
private const string TimeUnitFormat = "TimeUnit_{0}";

/// <summary>
/// Generates Resource Keys according to convention.
/// </summary>
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param>
/// <param name="count">Number of units, default is One.</param>
/// <param name="toWords">Result to words, default is false.</param>
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns>
public static string GetResourceKey(TimeUnit unit)
{
return TimeUnitFormat.FormatWith(unit);
}
}
}
}
23 changes: 23 additions & 0 deletions src/Humanizer/Properties/Resources.de.resx
Expand Up @@ -401,5 +401,28 @@
</data>
<data name="DataUnit_TerabyteSymbol" xml:space="preserve">
<value>TB</value>
<data name="TimeUnit_Millisecond" xml:space="preserve">
<value>ms</value>
</data>
<data name="TimeUnit_Second" xml:space="preserve">
<value>s</value>
</data>
<data name="TimeUnit_Minute" xml:space="preserve">
<value>min</value>
</data>
<data name="TimeUnit_Hour" xml:space="preserve">
<value>h</value>
</data>
<data name="TimeUnit_Day" xml:space="preserve">
<value>d</value>
</data>
<data name="TimeUnit_Week" xml:space="preserve">
<value>Woche</value>
</data>
<data name="TimeUnit_Month" xml:space="preserve">
<value>M</value>
</data>
<data name="TimeUnit_Year" xml:space="preserve">
<value>a</value>
</data>
</root>
24 changes: 24 additions & 0 deletions src/Humanizer/Properties/Resources.resx
Expand Up @@ -711,4 +711,28 @@
<data name="DataUnit_TerabyteSymbol" xml:space="preserve">
<value>TB</value>
</data>
<data name="TimeUnit_Millisecond" xml:space="preserve">
<value>ms</value>
</data>
<data name="TimeUnit_Second" xml:space="preserve">
<value>s</value>
</data>
<data name="TimeUnit_Minute" xml:space="preserve">
<value>min</value>
</data>
<data name="TimeUnit_Hour" xml:space="preserve">
<value>h</value>
</data>
<data name="TimeUnit_Day" xml:space="preserve">
<value>d</value>
</data>
<data name="TimeUnit_Week" xml:space="preserve">
<value>week</value>
</data>
<data name="TimeUnit_Month" xml:space="preserve">
<value>mo</value>
</data>
<data name="TimeUnit_Year" xml:space="preserve">
<value>a</value>
</data>
</root>
24 changes: 24 additions & 0 deletions src/Humanizer/TimeUnitToSymbolExtensions.cs
@@ -0,0 +1,24 @@
using System.Globalization;

using Humanizer.Localisation;
using Humanizer.Configuration;

namespace Humanizer
{
/// <summary>
/// Transform a time unit into a symbol; e.g. <see cref="TimeUnit.Year"/> => "a"
/// </summary>
public static class TimeUnitToSymbolExtensions
{
/// <summary>
/// TimeUnit.Day.ToSymbol() -> "d"
/// </summary>
/// <param name="unit">Unit of time to be turned to a symbol</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns></returns>
public static string ToSymbol(this TimeUnit unit, CultureInfo culture = null)
{
return Configurator.GetFormatter(culture).TimeUnitHumanize(unit);
}
}
}

0 comments on commit 36adbce

Please sign in to comment.