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

Allow localization of messages #1

Merged
merged 4 commits into from
Aug 5, 2013
Merged
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
35 changes: 19 additions & 16 deletions src/Humanizer.Tests/Extensions/DateExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,92 +32,95 @@ public class DateExtensionsTests
{
public void Verify(string expectedString, TimeSpan deltaFromNow)
{
Assert.Contains(expectedString, DateTime.UtcNow.Add(deltaFromNow).Humanize());
Assert.Contains(expectedString, DateTime.Now.Add(deltaFromNow).Humanize(false));
var utcNow = new DateTime(2013, 6, 20, 9, 58, 22, DateTimeKind.Utc);
var now = new DateTime(2013, 6, 20, 11, 58, 22, DateTimeKind.Local);

Assert.Equal(expectedString, utcNow.Add(deltaFromNow).Humanize(now: utcNow));
Assert.Equal(expectedString, now.Add(deltaFromNow).Humanize(false, now));
}

[Fact]
public void FutureDates()
{
Verify(DateExtensions.FutureDate, new TimeSpan(0, 0, 1, 0));
Verify("not yet", new TimeSpan(0, 0, 1, 0));
}

[Fact]
public void JustNow()
{
Verify(DateExtensions.OneSecondAgo, new TimeSpan(0, 0, 0, -1));
Verify("one second ago", new TimeSpan(0, 0, 0, -1));
}

[Fact]
public void SecondsAgo()
{
Verify(DateExtensions.SecondsAgo, new TimeSpan(0, 0, 0, -10));
Verify("10 seconds ago", new TimeSpan(0, 0, 0, -10));
}

[Fact]
public void OneMinuteAgo()
{
Verify(DateExtensions.OneMinuteAgo, new TimeSpan(0, 0, -1, 0));
Verify("a minute ago", new TimeSpan(0, 0, -1, 0));
}

[Fact]
public void AFewMinutesAgo()
{
Verify(DateExtensions.MinutesAgo, new TimeSpan(0, 0, -10, 0));
Verify("10 minutes ago", new TimeSpan(0, 0, -10, 0));
}

[Fact]
public void AnHourAgo()
{
Verify(DateExtensions.OneHourAgo, new TimeSpan(0, -1, -10, 0));
Verify("an hour ago", new TimeSpan(0, -1, -10, 0));
}

[Fact]
public void HoursAgo()
{
Verify(DateExtensions.HoursAgo, new TimeSpan(0, -10, 0, 0));
Verify("10 hours ago", new TimeSpan(0, -10, 0, 0));
}

[Fact]
public void Yesterday()
{
Verify(DateExtensions.Yesterday, new TimeSpan(-1, -10, 0, 0));
Verify("yesterday", new TimeSpan(-1, -10, 0, 0));
}

[Fact]
public void AFewDaysAgo()
{
Verify(DateExtensions.DaysAgo, new TimeSpan(-10, 0, 0, 0));
Verify("10 days ago", new TimeSpan(-10, 0, 0, 0));
}

[Fact]
public void OneMonthAgo()
{
Verify(DateExtensions.OneMonthAgo, new TimeSpan(-30, 0, 0, 0));
Verify("one month ago", new TimeSpan(-30, 0, 0, 0));
}

[Fact]
public void AFewMonthsAgo()
{
Verify(DateExtensions.MonthsAgo, new TimeSpan(-60, 0, 0, 0));
Verify("2 months ago", new TimeSpan(-60, 0, 0, 0));
}

[Fact]
public void OneYearAgoIsNotAccureate()
{
Verify(DateExtensions.OneYearAgo, new TimeSpan(-360, 0, 0, 0));
Verify("one year ago", new TimeSpan(-360, 0, 0, 0));
}

[Fact]
public void OneYearAgo()
{
Verify(DateExtensions.OneYearAgo, new TimeSpan(-400, 0, 0, 0));
Verify("one year ago", new TimeSpan(-400, 0, 0, 0));
}

[Fact]
public void FewYearsAgo()
{
Verify(DateExtensions.YearsAgo, new TimeSpan(-900, 0, 0, 0));
Verify("2 years ago", new TimeSpan(-900, 0, 0, 0));
}
}
}
98 changes: 98 additions & 0 deletions src/Humanizer.Tests/Extensions/DateExtensionsTestsDynamic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using Xunit;

namespace Humanizer.Tests.Extensions
{
public class DateExtensionsTestsDynamic
{
public void Verify(string expectedString, TimeSpan deltaFromNow)
{
Assert.Equal(expectedString, DateTime.UtcNow.Add(deltaFromNow).Humanize());
Assert.Equal(expectedString, DateTime.Now.Add(deltaFromNow).Humanize(false));
}

[Fact]
public void FutureDates()
{
Verify("not yet", new TimeSpan(0, 0, 1, 0));
}

[Fact]
public void JustNow()
{
Verify("one second ago", new TimeSpan(0, 0, 0, -1));
}

[Fact]
public void SecondsAgo()
{
Verify("10 seconds ago", new TimeSpan(0, 0, 0, -10));
}

[Fact]
public void OneMinuteAgo()
{
Verify("a minute ago", new TimeSpan(0, 0, -1, 0));
}

[Fact]
public void AFewMinutesAgo()
{
Verify("10 minutes ago", new TimeSpan(0, 0, -10, 0));
}

[Fact]
public void AnHourAgo()
{
Verify("an hour ago", new TimeSpan(0, -1, -10, 0));
}

[Fact]
public void HoursAgo()
{
Verify("10 hours ago", new TimeSpan(0, -10, 0, 0));
}

[Fact]
public void Yesterday()
{
Verify("yesterday", new TimeSpan(-1, -10, 0, 0));
}

[Fact]
public void AFewDaysAgo()
{
Verify("10 days ago", new TimeSpan(-10, 0, 0, 0));
}

[Fact]
public void OneMonthAgo()
{
Verify("one month ago", new TimeSpan(-30, 0, 0, 0));
}

[Fact]
public void AFewMonthsAgo()
{
Verify("2 months ago", new TimeSpan(-60, 0, 0, 0));
}

[Fact]
public void OneYearAgoIsNotAccureate()
{
Verify("one year ago", new TimeSpan(-360, 0, 0, 0));
}

[Fact]
public void OneYearAgo()
{
Verify("one year ago", new TimeSpan(-400, 0, 0, 0));
}

[Fact]
public void FewYearsAgo()
{
Verify("2 years ago", new TimeSpan(-900, 0, 0, 0));
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\DateExtensionsTests.cs" />
<Compile Include="Extensions\DateExtensionsTestsDynamic.cs" />
<Compile Include="Extensions\EnumExtensionsTests.cs" />
<Compile Include="Extensions\StringExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
44 changes: 18 additions & 26 deletions src/Humanizer/DateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,73 +24,65 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.using System;

using System;
using System.ComponentModel;

namespace Humanizer
{
[Localizable(true)]
public static class DateExtensions
{
public static string FutureDate = "not yet";
public static string OneSecondAgo = "one second ago";
public static string SecondsAgo = " seconds ago";
public static string OneMinuteAgo = "a minute ago";
public static string MinutesAgo = " minutes ago";
public static string OneHourAgo = "an hour ago";
public static string HoursAgo = " hours ago";
public static string Yesterday = "yesterday";
public static string DaysAgo = " days ago";
public static string OneMonthAgo = "one month ago";
public static string MonthsAgo = " months ago";
public static string OneYearAgo = "one year ago";
public static string YearsAgo = " years ago";

// http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time
public static string Humanize(this DateTime input, bool utcDate = true)
public static string Humanize(this DateTime input, bool utcDate = true, DateTime? now = null)
{
if (now == null)
{
now = DateTime.UtcNow;
}
const int second = 1;
const int minute = 60 * second;
const int hour = 60 * minute;
const int day = 24 * hour;
const int month = 30 * day;

var comparisonBase = DateTime.UtcNow;
var comparisonBase = now.Value;
if (!utcDate)
comparisonBase = comparisonBase.ToLocalTime();

if (input > comparisonBase)
return FutureDate;
return Resources.DateExtensions_FutureDate_not_yet;

var ts = new TimeSpan(comparisonBase.Ticks - input.Ticks);
double delta = Math.Abs(ts.TotalSeconds);

if (delta < 1 * minute)
return ts.Seconds == 1 ? OneSecondAgo : ts.Seconds + SecondsAgo;
return ts.Seconds == 1 ? Resources.DateExtensions_OneSecondAgo_one_second_ago : string.Format(Resources.DateExtensions_SecondsAgo__seconds_ago, ts.Seconds);

if (delta < 2 * minute)
return OneMinuteAgo;
return Resources.DateExtensions_OneMinuteAgo_a_minute_ago;

if (delta < 45 * minute)
return ts.Minutes + MinutesAgo;
return string.Format(Resources.DateExtensions_MinutesAgo__minutes_ago, ts.Minutes);

if (delta < 90 * minute)
return OneHourAgo;
return Resources.DateExtensions_OneHourAgo_an_hour_ago;

if (delta < 24 * hour)
return ts.Hours + HoursAgo;
return string.Format(Resources.DateExtensions_HoursAgo__hours_ago, ts.Hours);

if (delta < 48 * hour)
return Yesterday;
return Resources.DateExtensions_Yesterday_yesterday;

if (delta < 30 * day)
return ts.Days + DaysAgo;
return string.Format(Resources.DateExtensions_DaysAgo__days_ago, ts.Days);

if (delta < 12 * month)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? OneMonthAgo : months + MonthsAgo;
return months <= 1 ? Resources.DateExtensions_OneMonthAgo_one_month_ago : string.Format(Resources.DateExtensions_MonthsAgo__months_ago, months);
}

int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? OneYearAgo : years + YearsAgo;
return years <= 1 ? Resources.DateExtensions_OneYearAgo_one_year_ago : string.Format(Resources.DateExtensions_YearsAgo__years_ago, years);
}
}
}
2 changes: 1 addition & 1 deletion src/Humanizer/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static string Humanize(this Enum input)

if (memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), true);

if (attrs.Length > 0)
{
Expand Down
22 changes: 22 additions & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,34 @@
<Compile Include="DateExtensions.cs" />
<Compile Include="EnumExtensions.cs" />
<Compile Include="LetterCasing.cs" />
<Compile Include="Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="StringExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Humanizer.snk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources.fr.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.fr.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Resources.nl-BE.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.nl-BE.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Loading