-
Notifications
You must be signed in to change notification settings - Fork 947
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add date to words converter feature. Support for en, en-US and es language.
- Loading branch information
1 parent
606e958
commit 7fd79ad
Showing
15 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/Humanizer.Tests.Shared/Localisation/en/DateToWordsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation.en | ||
{ | ||
public class DateToWordsTests | ||
{ | ||
[UseCulture("en-GB")] | ||
[Fact] | ||
public void ConvertDateToWordsGbString() | ||
{ | ||
Assert.Equal("the first of January two thousand and twenty-two", new DateTime(2022, 1, 1).ToWords()); | ||
} | ||
|
||
[UseCulture("en-US")] | ||
[Fact] | ||
public void ConvertDateToWordsUsString() | ||
{ | ||
Assert.Equal("January first, two thousand and twenty-two", new DateTime(2022, 1, 1).ToWords()); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
[UseCulture("en-GB")] | ||
[Fact] | ||
public void ConvertDateOnlyToWordsGbString() | ||
{ | ||
Assert.Equal("the first of January two thousand and twenty-two", new DateOnly(2022, 1, 1).ToWords()); | ||
} | ||
|
||
[UseCulture("en-US")] | ||
[Fact] | ||
public void ConvertDateOnlyToWordsUsString() | ||
{ | ||
Assert.Equal("January first, two thousand and twenty-two", new DateOnly(2022, 1, 1).ToWords()); | ||
} | ||
#endif | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Humanizer.Tests.Shared/Localisation/es/DateToWordsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation.es | ||
{ | ||
[UseCulture("es-419")] | ||
public class DateToWordsTests | ||
{ | ||
[Fact] | ||
public void ConvertDateToWordsString() | ||
{ | ||
Assert.Equal("dos de enero de dos mil veintidós", new DateTime(2022, 1, 2).ToWords()); | ||
Assert.Equal("diez de junio de dos mil veinte", new DateTime(2020, 6, 10).ToWords()); | ||
Assert.Equal("veinticinco de septiembre de dos mil diecisiete", new DateTime(2017, 9, 25).ToWords()); | ||
Assert.Equal("treinta y uno de diciembre de mil novecientos noventa y nueve", new DateTime(1999, 12, 31).ToWords()); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
[Fact] | ||
public void ConvertDateOnlyToWordsString() | ||
{ | ||
Assert.Equal("dos de enero de dos mil veintidós", new DateOnly(2022, 1, 2).ToWords()); | ||
Assert.Equal("diez de junio de dos mil veinte", new DateOnly(2020, 6, 10).ToWords()); | ||
Assert.Equal("veinticinco de septiembre de dos mil diecisiete", new DateOnly(2017, 9, 25).ToWords()); | ||
Assert.Equal("treinta y uno de diciembre de mil novecientos noventa y nueve", new DateOnly(1999, 12, 31).ToWords()); | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Humanizer/Configuration/DateOnlyToWordsConverterRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#if NET6_0_OR_GREATER | ||
using Humanizer.Localisation.DateToWords; | ||
|
||
namespace Humanizer.Configuration | ||
{ | ||
internal class DateOnlyToWordsConverterRegistry : LocaliserRegistry<IDateOnlyToWordConverter> | ||
{ | ||
public DateOnlyToWordsConverterRegistry() : base(new DefaultDateOnlyToWordConverter()) | ||
{ | ||
Register("en-US", new UsDateOnlyToWordsConverter()); | ||
Register("es", new EsDateOnlyToWordsConverter()); | ||
} | ||
} | ||
} | ||
#endif |
13 changes: 13 additions & 0 deletions
13
src/Humanizer/Configuration/DateToWordsConverterRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Humanizer.Localisation.DateToWords; | ||
|
||
namespace Humanizer.Configuration | ||
{ | ||
internal class DateToWordsConverterRegistry : LocaliserRegistry<IDateToWordConverter> | ||
{ | ||
public DateToWordsConverterRegistry() : base(new DefaultDateToWordConverter()) | ||
{ | ||
Register("en-US", new UsDateToWordsConverter()); | ||
Register("es", new EsDateToWordsConverter()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using Humanizer.Configuration; | ||
|
||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Humanizes DateTime into human readable sentence | ||
/// </summary> | ||
public static class DateToWordsExtensions | ||
{ | ||
/// <summary> | ||
/// Turns the provided date into words | ||
/// </summary> | ||
/// <param name="input">The date to be made into words</param> | ||
/// <returns>The date in words</returns> | ||
public static string ToWords(this DateTime input) | ||
{ | ||
return Configurator.DateToWordsConverter.Convert(input); | ||
} | ||
/// <summary> | ||
/// Turns the provided date into words | ||
/// </summary> | ||
/// <param name="input">The date to be made into words</param> | ||
/// <param name="grammaticalCase">The grammatical case to use for output words</param> | ||
/// <returns>The date in words</returns> | ||
public static string ToWords(this DateTime input, GrammaticalCase grammaticalCase) | ||
{ | ||
return Configurator.DateToWordsConverter.Convert(input, grammaticalCase); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
/// <summary> | ||
/// Turns the provided date into words | ||
/// </summary> | ||
/// <param name="input">The date to be made into words</param> | ||
/// <returns>The date in words</returns> | ||
public static string ToWords(this DateOnly input) | ||
{ | ||
return Configurator.DateOnlyToWordsConverter.Convert(input); | ||
} | ||
/// <summary> | ||
/// Turns the provided date into words | ||
/// </summary> | ||
/// <param name="input">The date to be made into words</param> | ||
/// <param name="grammaticalCase">The grammatical case to use for output words</param> | ||
/// <returns>The date in words</returns> | ||
public static string ToWords(this DateOnly input, GrammaticalCase grammaticalCase) | ||
{ | ||
return Configurator.DateOnlyToWordsConverter.Convert(input, grammaticalCase); | ||
} | ||
#endif | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Humanizer/Localisation/DateToWords/DefaultDateOnlyToWordConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
|
||
namespace Humanizer.Localisation.DateToWords | ||
{ | ||
internal class DefaultDateOnlyToWordConverter : IDateOnlyToWordConverter | ||
{ | ||
|
||
public virtual string Convert(DateOnly date) | ||
{ | ||
return "the " + date.Day.ToOrdinalWords() + date.ToString(" 'of' MMMM ") + date.Year.ToWords(); | ||
} | ||
|
||
public virtual string Convert(DateOnly date, GrammaticalCase grammaticalCase) | ||
{ | ||
return Convert(date); | ||
} | ||
|
||
} | ||
} | ||
|
||
#endif |
19 changes: 19 additions & 0 deletions
19
src/Humanizer/Localisation/DateToWords/DefaultDateToWordConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace Humanizer.Localisation.DateToWords | ||
{ | ||
internal class DefaultDateToWordConverter : IDateToWordConverter | ||
{ | ||
|
||
public virtual string Convert(DateTime date) | ||
{ | ||
return "the " + date.Day.ToOrdinalWords() + date.ToString(" 'of' MMMM ") + date.Year.ToWords(); | ||
} | ||
|
||
public virtual string Convert(DateTime date, GrammaticalCase grammaticalCase) | ||
{ | ||
return Convert(date); | ||
} | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Humanizer/Localisation/DateToWords/EsDateOnlyToWordsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
using Humanizer.Configuration; | ||
|
||
namespace Humanizer.Localisation.DateToWords | ||
{ | ||
internal class EsDateOnlyToWordsConverter : DefaultDateOnlyToWordConverter | ||
{ | ||
public override string Convert(DateOnly date) | ||
{ | ||
var equivalentDateTime = date.ToDateTime(TimeOnly.MinValue); | ||
return Configurator.DateToWordsConverter.Convert(equivalentDateTime); | ||
} | ||
} | ||
} | ||
|
||
#endif |
12 changes: 12 additions & 0 deletions
12
src/Humanizer/Localisation/DateToWords/EsDateToWordsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Humanizer.Localisation.DateToWords | ||
{ | ||
internal class EsDateToWordsConverter : DefaultDateToWordConverter | ||
{ | ||
public override string Convert(DateTime date) | ||
{ | ||
return date.Day.ToWords() + date.ToString(" 'de' MMMM 'de' ") + date.Year.ToWords(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Humanizer/Localisation/DateToWords/IDateOnlyToWordConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
using System; | ||
|
||
namespace Humanizer.Localisation.DateToWords | ||
{ | ||
/// <summary> | ||
/// The interface used to localise the ToWords method. | ||
/// </summary> | ||
public interface IDateOnlyToWordConverter | ||
{ | ||
/// <summary> | ||
/// Converts the date to Words | ||
/// </summary> | ||
/// <param name="date"></param> | ||
/// <returns></returns> | ||
string Convert(DateOnly date); | ||
|
||
/// <summary> | ||
/// Converts the date to Words using the provided grammatical case | ||
/// </summary> | ||
/// <param name="date"></param> | ||
/// <param name="grammaticalCase"></param> | ||
/// <returns></returns> | ||
string Convert(DateOnly date, GrammaticalCase grammaticalCase); | ||
} | ||
} | ||
#endif |
Oops, something went wrong.