Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToOrdinalWords extension with gender overload #500

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Humanizer meets all your .NET needs for manipulating and displaying strings, enu
- [Fluent date](#fluent-date)
- [Number to words](#number-to-words)
- [Number to ordinal words](#number-to-ordinal-words)
- [DateTime to ordinal words](#date-time-to-ordinal-words)
- [Roman numerals](#roman-numerals)
- [Metric numerals](#metric-numerals)
- [ByteSize](#bytesize)
Expand Down Expand Up @@ -727,6 +728,29 @@ Also, culture to use can be specified explicitly. If it is not, current thread's
1.ToOrdinalWords(GrammaticalGender.Masculine, new CulureInfo("pt-BR")) => "primeiro"
```


###<a id="date-time-to-ordinal-words">DateTime to ordinal words</a>
This is kind of an extension of Ordinalize
```C#
// for English UK locale
new DateTime(2015, 1, 1).ToOrdinalWords() => "1st January 2015"
new DateTime(2015, 2, 12).ToOrdinalWords() => "12th February 2015"
new DateTime(2015, 3, 22).ToOrdinalWords() => "22nd March 2015"
// for English US locale
new DateTime(2015, 1, 1).ToOrdinalWords() => "January 1st, 2015"
new DateTime(2015, 2, 12).ToOrdinalWords() => "February 12th, 2015"
new DateTime(2015, 3, 22).ToOrdinalWords() => "March 22nd, 2015"
```

`ToOrdinalWords` also supports grammatical case.
You can pass a second argument to `ToOrdinalWords` to specify the case of the output.
The possible values are `GrammaticalCase.Nominative`, `GrammaticalCase.Genitive`, `GrammaticalCase.Dative`, `GrammaticalCase.Accusative`, `GrammaticalCase.Instrumental` and `GrammaticalGender.Prepositional`:

```C#
```

Obviously this only applies to some cultures. For others passing case in doesn't make any difference in the result.

###<a id="roman-numerals">Roman numerals</a>
Humanizer can change numbers to Roman numerals using the `ToRoman` extension. The numbers 1 to 10 can be expressed in Roman numerals as follows:

Expand Down
2 changes: 2 additions & 0 deletions src/Humanizer.Tests.Shared/Humanizer.Tests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\CollectionFormatterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\DateHumanizeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\NumberToWordsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\DateToOrdinalWordsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\OrdinalizeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\TimeSpanHumanizeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\en\DateToOrdinalWordsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\es\DateHumanizeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\es\NumberToWordsFeminineTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\es\NumberToWordsTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Xunit;

namespace Humanizer.Tests.Localisation.de
{
[UseCulture("de")]
public class DateToOrdinalWordsTests
{
[Fact]
public void OrdinalizeString()
{
Assert.Equal("1. Januar 2015", new DateTime(2015, 1, 1).ToOrdinalWords());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Xunit;

namespace Humanizer.Tests.Localisation.en
{
public class DateToOrdinalWordsTests
{
[UseCulture("en-GB")]
[Fact]
public void OrdinalizeStringGb()
{
Assert.Equal("1st January 2015", new DateTime(2015, 1, 1).ToOrdinalWords());
}

[UseCulture("en-US")]
[Fact]
public void OrdinalizeStringUs()
{
Assert.Equal("January 1st, 2015", new DateTime(2015, 1, 1).ToOrdinalWords());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ namespace Humanizer
public static string Humanize(this System.DateTime input, bool utcDate = True, System.Nullable<System.DateTime> dateToCompareAgainst = null, System.Globalization.CultureInfo culture = null) { }
public static string Humanize(this System.DateTimeOffset input, System.Nullable<System.DateTimeOffset> dateToCompareAgainst = null, System.Globalization.CultureInfo culture = null) { }
}
public class static DateToOrdinalWordsExtensions
{
public static string ToOrdinalWords(this System.DateTime input) { }
public static string ToOrdinalWords(this System.DateTime input, Humanizer.GrammaticalCase grammaticalCase) { }
}
public class static EnumDehumanizeExtensions
{
public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input)
Expand Down Expand Up @@ -989,6 +994,7 @@ namespace Humanizer.Configuration
public static Humanizer.Configuration.LocaliserRegistry<Humanizer.Localisation.CollectionFormatters.ICollectionFormatter> CollectionFormatters { get; }
public static Humanizer.DateTimeHumanizeStrategy.IDateTimeHumanizeStrategy DateTimeHumanizeStrategy { get; set; }
public static Humanizer.DateTimeHumanizeStrategy.IDateTimeOffsetHumanizeStrategy DateTimeOffsetHumanizeStrategy { get; set; }
public static Humanizer.Configuration.LocaliserRegistry<Humanizer.Localisation.DateToOrdinalWords.IDateToOrdinalWordConverter> DateToOrdinalWordsConverters { get; }
public static System.Func<System.Reflection.PropertyInfo, bool> EnumDescriptionPropertyLocator { get; set; }
public static Humanizer.Configuration.LocaliserRegistry<Humanizer.Localisation.Formatters.IFormatter> Formatters { get; }
public static Humanizer.Configuration.LocaliserRegistry<Humanizer.Localisation.NumberToWords.INumberToWordsConverter> NumberToWordsConverters { get; }
Expand Down Expand Up @@ -1071,6 +1077,15 @@ namespace Humanizer.Localisation.CollectionFormatters
string Humanize<T>(System.Collections.Generic.IEnumerable<T> collection, System.Func<T, string> objectFormatter, string separator);
}
}
namespace Humanizer.Localisation.DateToOrdinalWords
{

public interface IDateToOrdinalWordConverter
{
string Convert(System.DateTime date);
string Convert(System.DateTime date, Humanizer.GrammaticalCase grammaticalCase);
}
}
namespace Humanizer.Localisation.Formatters
{

Expand Down
21 changes: 21 additions & 0 deletions src/Humanizer/Configuration/Configurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Humanizer.Localisation.NumberToWords;
using Humanizer.Localisation.Ordinalizers;
using Humanizer.Localisation.CollectionFormatters;
using Humanizer.Localisation.DateToOrdinalWords;

namespace Humanizer.Configuration
{
Expand Down Expand Up @@ -52,6 +53,15 @@ public static LocaliserRegistry<IOrdinalizer> Ordinalizers
get { return _ordinalizers; }
}

private static readonly LocaliserRegistry<IDateToOrdinalWordConverter> _dateToOrdinalWordConverters = new DateToOrdinalWordsConverterRegistry();
/// <summary>
/// A registry of ordinalizers used to localise Ordinalize method
/// </summary>
public static LocaliserRegistry<IDateToOrdinalWordConverter> DateToOrdinalWordsConverters
{
get { return _dateToOrdinalWordConverters; }
}

internal static ICollectionFormatter CollectionFormatter
{
get
Expand Down Expand Up @@ -89,6 +99,17 @@ internal static IOrdinalizer Ordinalizer
}
}

/// <summary>
/// The ordinalizer to be used
/// </summary>
internal static IDateToOrdinalWordConverter DateToOrdinalWordsConverter
{
get
{
return DateToOrdinalWordsConverters.ResolveForUiCulture();
}
}

private static IDateTimeHumanizeStrategy _dateTimeHumanizeStrategy = new DefaultDateTimeHumanizeStrategy();
/// <summary>
/// The strategy to be used for DateTime.Humanize
Expand Down
13 changes: 13 additions & 0 deletions src/Humanizer/Configuration/DateToOrdinalWordsConverterRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Humanizer.Localisation.DateToOrdinalWords;
namespace Humanizer.Configuration
{
internal class DateToOrdinalWordsConverterRegistry : LocaliserRegistry<IDateToOrdinalWordConverter>
{
public DateToOrdinalWordsConverterRegistry() : base(new DefaultDateToOrdinalWordConverter())
{
Register("en-UK", new DefaultDateToOrdinalWordConverter());
Register("de", new DefaultDateToOrdinalWordConverter());
Register("en-US", new UsDateToOrdinalWordsConverter());
}
}
}
32 changes: 32 additions & 0 deletions src/Humanizer/DateToOrdinalWordsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Globalization;
using Humanizer.Configuration;

namespace Humanizer
{
/// <summary>
/// Humanizes DateTime into human readable sentence
/// </summary>
public static class DateToOrdinalWordsExtensions
{
/// <summary>
/// Turns the provided date into ordinal words
/// </summary>
/// <param name="input">The date to be made into ordinal words</param>
/// <returns>The date in ordinal words</returns>
public static string ToOrdinalWords(this DateTime input)
{
return Configurator.DateToOrdinalWordsConverter.Convert(input);
}
/// <summary>
/// Turns the provided date into ordinal words
/// </summary>
/// <param name="input">The date to be made into ordinal words</param>
/// <param name="grammaticalCase">The grammatical case to use for output words</param>
/// <returns>The date in ordinal words</returns>
public static string ToOrdinalWords(this DateTime input, GrammaticalCase grammaticalCase)
{
return Configurator.DateToOrdinalWordsConverter.Convert(input, grammaticalCase);
}
}
}
13 changes: 9 additions & 4 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@
<ItemGroup>
<!-- example NuSpec file that must be specified -->
<NuSpecFile Include="$(SolutionDir)..\NuSpecs\Humanizer.Core.nuspec">
<Visible>False</Visible>
</NuSpecFile>
</ItemGroup>
<Visible>False</Visible>
</NuSpecFile>
</ItemGroup>
<ItemGroup>
<Compile Include="Bytes\ByteRate.cs" />
<Compile Include="CollectionHumanizeExtensions.cs" />
<Compile Include="Configuration\CollectionFormatterRegistry.cs" />
<Compile Include="Configuration\DateToOrdinalWordsConverterRegistry.cs" />
<Compile Include="DateToOrdinalWordsExtensions.cs" />
<Compile Include="DateTimeHumanizeStrategy\DateTimeHumanizeAlgorithms.cs" />
<Compile Include="DateTimeHumanizeStrategy\DefaultDateTimeHumanizeStrategy.cs" />
<Compile Include="DateTimeHumanizeStrategy\DefaultDateTimeOffsetHumanizeStrategy.cs" />
Expand All @@ -65,6 +67,8 @@
<Compile Include="Localisation\CollectionFormatters\DefaultCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\ICollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\OxfordStyleCollectionFormatter.cs" />
<Compile Include="Localisation\DateToOrdinalWords\UsDateToOrdinalWordsConverter.cs" />
<Compile Include="Localisation\DateToOrdinalWords\DefaultDateToOrdinalWordConverter.cs" />
<Compile Include="Localisation\Formatters\CroatianFormatter.cs" />
<Compile Include="Localisation\Formatters\SerbianFormatter.cs" />
<Compile Include="Localisation\Formatters\SlovenianFormatter.cs" />
Expand All @@ -87,6 +91,7 @@
<Compile Include="Localisation\NumberToWords\RomanianNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\Romanian\RomanianCardinalNumberConverter.cs" />
<Compile Include="Localisation\NumberToWords\Romanian\RomanianOrdinalNumberConverter.cs" />
<Compile Include="Localisation\DateToOrdinalWords\IDateToOrdinalWordConverter.cs" />
<Compile Include="Localisation\Ordinalizers\RomanianOrdinalizer.cs" />
<Compile Include="Localisation\NumberToWords\SerbianCyrlNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\SerbianNumberToWordsConverter.cs" />
Expand Down Expand Up @@ -263,4 +268,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes to this file should be reverted, as they only formatting and add no value to the feature.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Humanizer.Localisation.DateToOrdinalWords
{
internal class DefaultDateToOrdinalWordConverter : IDateToOrdinalWordConverter
{

public virtual string Convert(DateTime date)
{
return date.Day.Ordinalize() + date.ToString(" MMMM yyyy");
}

public virtual string Convert(DateTime date, GrammaticalCase grammaticalCase)
{
return Convert(date);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Humanizer.Localisation.DateToOrdinalWords
{
/// <summary>
/// The interface used to localise the ToOrdinalWords method.
/// </summary>
public interface IDateToOrdinalWordConverter
{
/// <summary>
/// Converts the date to Ordinal Words
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
string Convert(DateTime date);

/// <summary>
/// Converts the date to Ordinal Words using the provided grammatical case
/// </summary>
/// <param name="date"></param>
/// <param name="grammaticalCase"></param>
/// <returns></returns>
string Convert(DateTime date, GrammaticalCase grammaticalCase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Humanizer.Localisation.DateToOrdinalWords
{
internal class UsDateToOrdinalWordsConverter : DefaultDateToOrdinalWordConverter
{
public override string Convert(DateTime date)
{
return date.ToString("MMMM ") + date.Day.Ordinalize() + date.ToString(", yyyy");
}
}
}