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
Convert TimeUnit to symbols #1106
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
689802b
Add TimeUnitToSymbolTests that should be fulfilled
hangy 6b3eb53
Implement TimeUnitToSymbolExtensions.ToSymbol method
hangy 630b78c
Add example for TimeUnitToSymbolExtensions.ToSymbol
hangy 18138ed
Use 'y' instead of 'a' for `TimeUnit.Year`
hangy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 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
27 changes: 27 additions & 0 deletions
27
src/Humanizer.Tests.Shared/Localisation/de/TimeUnitToSymbolTests.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,27 @@ | ||
using System; | ||
|
||
using Humanizer.Localisation; | ||
|
||
using Xunit; | ||
|
||
namespace Humanizer.Tests.Localisation.de | ||
{ | ||
[UseCulture("de-DE")] | ||
public class TimeUnitToSymbolTests | ||
{ | ||
[Theory] | ||
[Trait("Translation", "Native speaker")] | ||
[InlineData(TimeUnit.Millisecond, "ms")] | ||
[InlineData(TimeUnit.Second, "s")] | ||
[InlineData(TimeUnit.Minute, "min")] | ||
[InlineData(TimeUnit.Hour, "h")] | ||
[InlineData(TimeUnit.Day, "d")] | ||
[InlineData(TimeUnit.Week, "Woche")] | ||
[InlineData(TimeUnit.Month, "M")] | ||
[InlineData(TimeUnit.Year, "a")] | ||
public void ToSymbol(TimeUnit unit, string expected) | ||
{ | ||
Assert.Equal(expected, unit.ToSymbol()); | ||
} | ||
} | ||
} |
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,24 @@ | ||
using Humanizer.Localisation; | ||
|
||
using Xunit; | ||
|
||
namespace Humanizer.Tests | ||
{ | ||
[UseCulture("en-US")] | ||
public class TimeUnitToSymbolTests | ||
{ | ||
[Theory] | ||
[InlineData(TimeUnit.Millisecond, "ms")] | ||
[InlineData(TimeUnit.Second, "s")] | ||
[InlineData(TimeUnit.Minute, "min")] | ||
[InlineData(TimeUnit.Hour, "h")] | ||
[InlineData(TimeUnit.Day, "d")] | ||
[InlineData(TimeUnit.Week, "week")] | ||
[InlineData(TimeUnit.Month, "mo")] | ||
[InlineData(TimeUnit.Year, "a")] | ||
public void ToSymbol(TimeUnit unit, string expected) | ||
{ | ||
Assert.Equal(expected, unit.ToSymbol()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the a mean here? Not sure I've seen this before, is there an example you can point to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took it from Wikipedia, which listed both "a" (per annum?) and "y" (per year) as possible versions in English. Will fix as discussed. 👍🏻