-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dafb148
commit cb832cf
Showing
11 changed files
with
92 additions
and
194 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,16 +1,12 @@ | ||
namespace Humanizer | ||
namespace Humanizer; | ||
|
||
public partial class ResourceKeys | ||
{ | ||
public partial class ResourceKeys | ||
static void ValidateRange(int count) | ||
{ | ||
const string Single = "Single"; | ||
const string Multiple = "Multiple"; | ||
|
||
static void ValidateRange(int count) | ||
if (count < 0) | ||
{ | ||
if (count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,62 +1,54 @@ | ||
namespace Humanizer | ||
namespace Humanizer; | ||
|
||
public partial class ResourceKeys | ||
{ | ||
public partial class ResourceKeys | ||
/// <summary> | ||
/// Encapsulates the logic required to get the resource keys for DateTime.Humanize | ||
/// </summary> | ||
public static class DateHumanize | ||
{ | ||
/// <summary> | ||
/// Encapsulates the logic required to get the resource keys for DateTime.Humanize | ||
/// Resource key for Now. | ||
/// </summary> | ||
public static class DateHumanize | ||
public const string Now = "DateHumanize_Now"; | ||
|
||
/// <summary> | ||
/// Resource key for Never. | ||
/// </summary> | ||
public const string Never = "DateHumanize_Never"; | ||
|
||
/// <summary> | ||
/// Generates Resource Keys according to convention. | ||
/// </summary> | ||
/// <param name="timeUnit">Time unit</param> | ||
/// <param name="timeUnitTense">Is time unit in future or past</param> | ||
/// <param name="count">Number of units, default is One.</param> | ||
/// <returns>Resource key, like DateHumanize_SingleMinuteAgo</returns> | ||
public static string GetResourceKey(TimeUnit timeUnit, Tense timeUnitTense, int count = 1) | ||
{ | ||
/// <summary> | ||
/// Resource key for Now. | ||
/// </summary> | ||
public const string Now = "DateHumanize_Now"; | ||
|
||
/// <summary> | ||
/// Resource key for Never. | ||
/// </summary> | ||
public const string Never = "DateHumanize_Never"; | ||
|
||
/// <summary> | ||
/// Examples: DateHumanize_SingleMinuteAgo, DateHumanize_MultipleHoursAgo | ||
/// Note: "s" for plural served separately by third part. | ||
/// </summary> | ||
const string DateTimeFormat = "DateHumanize_{0}{1}{2}"; | ||
|
||
const string Ago = "Ago"; | ||
const string FromNow = "FromNow"; | ||
|
||
/// <summary> | ||
/// Generates Resource Keys according to convention. | ||
/// </summary> | ||
/// <param name="timeUnit">Time unit</param> | ||
/// <param name="timeUnitTense">Is time unit in future or past</param> | ||
/// <param name="count">Number of units, default is One.</param> | ||
/// <returns>Resource key, like DateHumanize_SingleMinuteAgo</returns> | ||
public static string GetResourceKey(TimeUnit timeUnit, Tense timeUnitTense, int count = 1) | ||
ValidateRange(count); | ||
|
||
if (count == 0) | ||
{ | ||
ValidateRange(count); | ||
return Now; | ||
} | ||
|
||
if (count == 0) | ||
if (count == 1) | ||
{ | ||
if (timeUnitTense == Tense.Future) | ||
{ | ||
return Now; | ||
return $"DateHumanize_Single{timeUnit}FromNow"; | ||
} | ||
|
||
string singularity; | ||
var unit = timeUnit.ToString(); | ||
if (count == 1) | ||
{ | ||
singularity = Single; | ||
} | ||
else | ||
{ | ||
unit += "s"; | ||
singularity = Multiple; | ||
} | ||
return $"DateHumanize_Single{timeUnit}Ago"; | ||
} | ||
|
||
var tense = timeUnitTense == Tense.Future ? FromNow : Ago; | ||
return DateTimeFormat.FormatWith(singularity, unit, tense); | ||
if (timeUnitTense == Tense.Future) | ||
{ | ||
return $"DateHumanize_Multiple{timeUnit}sFromNow"; | ||
} | ||
|
||
return $"DateHumanize_Multiple{timeUnit}sAgo"; | ||
} | ||
} | ||
} | ||
} |
50 changes: 24 additions & 26 deletions
50
src/Humanizer/Localisation/ResourceKeys.TimeSpanHumanize.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 |
---|---|---|
@@ -1,37 +1,35 @@ | ||
namespace Humanizer | ||
namespace Humanizer; | ||
|
||
public partial class ResourceKeys | ||
{ | ||
public partial class ResourceKeys | ||
/// <summary> | ||
/// Encapsulates the logic required to get the resource keys for TimeSpan.Humanize | ||
/// Examples: TimeSpanHumanize_SingleMinute, TimeSpanHumanize_MultipleHours. | ||
/// </summary> | ||
public static class TimeSpanHumanize | ||
{ | ||
/// <summary> | ||
/// Encapsulates the logic required to get the resource keys for TimeSpan.Humanize | ||
/// Generates Resource Keys according to convention. | ||
/// </summary> | ||
public static class TimeSpanHumanize | ||
/// <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, int count = 1, bool toWords = false) | ||
{ | ||
/// <summary> | ||
/// Examples: TimeSpanHumanize_SingleMinute, TimeSpanHumanize_MultipleHours. | ||
/// Note: "s" for plural served separately by third part. | ||
/// </summary> | ||
const string TimeSpanFormat = "TimeSpanHumanize_{0}{1}{2}"; | ||
const string Zero = "TimeSpanHumanize_Zero"; | ||
ValidateRange(count); | ||
|
||
/// <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, int count = 1, bool toWords = false) | ||
if (count == 0 && toWords) | ||
{ | ||
ValidateRange(count); | ||
|
||
if (count == 0 && toWords) | ||
{ | ||
return Zero; | ||
} | ||
return "TimeSpanHumanize_Zero"; | ||
} | ||
|
||
return TimeSpanFormat.FormatWith(count == 1 ? Single : Multiple, unit, count == 1 ? "" : "s"); | ||
if (count == 1) | ||
{ | ||
return $"TimeSpanHumanize_Single{unit}"; | ||
} | ||
|
||
return $"TimeSpanHumanize_Multiple{unit}s"; | ||
} | ||
} | ||
} | ||
} |
33 changes: 14 additions & 19 deletions
33
src/Humanizer/Localisation/ResourceKeys.TimeUnitSymbol.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 |
---|---|---|
@@ -1,24 +1,19 @@ | ||
namespace Humanizer | ||
namespace Humanizer; | ||
|
||
public partial class ResourceKeys | ||
{ | ||
public partial class ResourceKeys | ||
/// <summary> | ||
/// Encapsulates the logic required to get the resource keys for TimeUnit.ToSymbol | ||
/// </summary> | ||
public static class TimeUnitSymbol | ||
{ | ||
/// <summary> | ||
/// Encapsulates the logic required to get the resource keys for TimeUnit.ToSymbol | ||
/// Generates Resource Keys according to convention. | ||
/// Examples: TimeUnit_Minute, TimeUnit_Hour. | ||
/// </summary> | ||
public static class TimeUnitSymbol | ||
{ | ||
/// <summary> | ||
/// Examples: TimeUnit_Minute, TimeUnit_Hour. | ||
/// </summary> | ||
const string TimeUnitFormat = "TimeUnit_{0}"; | ||
|
||
/// <summary> | ||
/// Generates Resource Keys according to convention. | ||
/// </summary> | ||
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param> | ||
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns> | ||
public static string GetResourceKey(TimeUnit unit) => | ||
TimeUnitFormat.FormatWith(unit); | ||
} | ||
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param> | ||
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns> | ||
public static string GetResourceKey(TimeUnit unit) => | ||
$"TimeUnit_{unit}"; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.