Skip to content

Commit

Permalink
Use resources to specify age & pre-hyphenated age formats
Browse files Browse the repository at this point in the history
- Define default resource values (in Resources.resx) as "{0}" and "", respectively
- Add Resources.en.resx and Resources.en-US.resx, to override the defaults
- Add/modify nuspec files accordingly
- Simplify default (en-US) tests and move tests for other English-based cultures to another file
  • Loading branch information
louis-z committed Jul 17, 2021
1 parent 2f8c025 commit 9ae7082
Show file tree
Hide file tree
Showing 14 changed files with 429 additions and 63 deletions.
26 changes: 26 additions & 0 deletions NuSpecs/Humanizer.Core.en-US.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata minClientVersion="2.12">
<id>Humanizer.Core.en-US</id>
<version>$version$</version>
<title>Humanizer Locale (en-US)</title>
<authors>Mehdi Khalili, Claire Novotny</authors>
<projectUrl>https://github.com/Humanizr/Humanizer</projectUrl>
<icon>logo.png</icon>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Humanizer Locale English (United States) (en-US)</description>
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
<license type="expression">MIT</license>
<repository type="$RepositoryType$" url="$RepositoryUrl$" commit="$RepositoryCommit$" />
<language>en-US</language>
<dependencies>
<dependency id="Humanizer.Core" version="[$version$]" />
</dependencies>
</metadata>
<files>
<file src="Humanizer\bin\Release\netstandard1.0\en-US\*.*" target="lib\netstandard1.0\en-US" />
<file src="Humanizer\bin\Release\netstandard2.0\en-US\*.*" target="lib\netstandard2.0\en-US" />
<file src="Humanizer\bin\Release\net6.0\en-US\*.*" target="lib\net6.0\en-US" />
<file src="..\logo.png" target="logo.png" />
</files>
</package>
26 changes: 26 additions & 0 deletions NuSpecs/Humanizer.Core.en.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata minClientVersion="2.12">
<id>Humanizer.Core.en</id>
<version>$version$</version>
<title>Humanizer Locale (en)</title>
<authors>Mehdi Khalili, Claire Novotny</authors>
<projectUrl>https://github.com/Humanizr/Humanizer</projectUrl>
<icon>logo.png</icon>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Humanizer Locale English (en)</description>
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
<license type="expression">MIT</license>
<repository type="$RepositoryType$" url="$RepositoryUrl$" commit="$RepositoryCommit$" />
<language>en</language>
<dependencies>
<dependency id="Humanizer.Core" version="[$version$]" />
</dependencies>
</metadata>
<files>
<file src="Humanizer\bin\Release\netstandard1.0\en\*.*" target="lib\netstandard1.0\en" />
<file src="Humanizer\bin\Release\netstandard2.0\en\*.*" target="lib\netstandard2.0\en" />
<file src="Humanizer\bin\Release\net6.0\en\*.*" target="lib\net6.0\en" />
<file src="..\logo.png" target="logo.png" />
</files>
</package>
2 changes: 2 additions & 0 deletions NuSpecs/Humanizer.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<dependency id="Humanizer.Core.da" version="$version$" />
<dependency id="Humanizer.Core.de" version="$version$" />
<dependency id="Humanizer.Core.el" version="$version$" />
<dependency id="Humanizer.Core.en-US" version="$version$" />
<dependency id="Humanizer.Core.en" version="$version$" />
<dependency id="Humanizer.Core.es" version="$version$" />
<dependency id="Humanizer.Core.fa" version="$version$" />
<dependency id="Humanizer.Core.fi-FI" version="$version$" />
Expand Down
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 TimeSpanHyphenatedAgeTests
{
[UseCulture("en-CA")]
[Fact]
public void HyphenatedAgeCa()
{
Assert.Equal("5-day-old", TimeSpan.FromDays(5).ToHyphenatedAge());
}

[UseCulture("en-GB")]
[Fact]
public void HyphenatedAgeGb()
{
Assert.Equal("three-week-old", TimeSpan.FromDays(21).ToHyphenatedAge(toWords: true));
}
}
}
30 changes: 15 additions & 15 deletions src/Humanizer.Tests.Shared/Localisation/fr/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,26 @@ public void MillisecondsToWords(int ms, string expected)
}

[Theory]
[InlineData(4, "4 jours")]
[InlineData(23, "3 semaines")]
[InlineData(64, "2 mois")]
[InlineData(367, "1 an")]
[InlineData(750, "2 ans")]
public void Age(int days, string expected)
{
var actual = TimeSpan.FromDays(days).ToAge();
[InlineData(4, false, "4 jours")]
[InlineData(23, false, "3 semaines")]
[InlineData(64, false, "2 mois")]
[InlineData(367, true, "un an")]
[InlineData(750, true, "deux ans")]
public void Age(int days, bool toWords, string expected)
{
var actual = TimeSpan.FromDays(days).ToAge(toWords: toWords);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(4, "")]
[InlineData(23, "")]
[InlineData(64, "")]
[InlineData(367, "")]
[InlineData(750, "")]
public void HyphenatedAge(int days, string expected)
[InlineData(4, true, "")]
[InlineData(23, true, "")]
[InlineData(64, true, "")]
[InlineData(367, false, "")]
[InlineData(750, false, "")]
public void HyphenatedAge(int days, bool toWords, string expected)
{
var actual = TimeSpan.FromDays(days).ToHyphenatedAge();
var actual = TimeSpan.FromDays(days).ToHyphenatedAge(toWords: toWords);
Assert.Equal(expected, actual);
}

Expand Down
48 changes: 19 additions & 29 deletions src/Humanizer.Tests.Shared/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,27 @@ public void Milliseconds(int ms, string expected)
}

[Theory]
[InlineData(4, "4 days old", null, "en", "en-CA", "en-GB")]
[InlineData(23, "3 weeks old", null, "en", "en-CA", "en-GB")]
[InlineData(64, "2 months old", null, "en", "en-CA", "en-GB")]
[InlineData(367, "1 year old", null, "en", "en-CA", "en-GB")]
[InlineData(750, "2 years old", null, "en", "en-CA", "en-GB")]
public void Age(int days, string expected, params string[] englishCultureNames)
[InlineData(4, false, "4 days old")]
[InlineData(23, false, "3 weeks old")]
[InlineData(64, false, "2 months old")]
[InlineData(367, true, "one year old")]
[InlineData(750, true, "two years old")]
public void Age(int days, bool toWords, string expected)
{
foreach (var cultureName in englishCultureNames)
{
var actual = cultureName is null ?
TimeSpan.FromDays(days).ToAge() :
TimeSpan.FromDays(days).ToAge(new CultureInfo(cultureName));
Assert.Equal(expected, actual);
}
var actual = TimeSpan.FromDays(days).ToAge(toWords: toWords);
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(4, "4-day-old", null, "en", "en-CA", "en-GB")]
[InlineData(23, "3-week-old", null, "en", "en-CA", "en-GB")]
[InlineData(64, "2-month-old", null, "en", "en-CA", "en-GB")]
[InlineData(367, "1-year-old", null, "en", "en-CA", "en-GB")]
[InlineData(750, "2-year-old", null, "en", "en-CA", "en-GB")]
public void HyphenatedAge(int days, string expected, params string[] englishCultureNames)
[InlineData(4, true, "four-day-old")]
[InlineData(23, true, "three-week-old")]
[InlineData(64, true, "two-month-old")]
[InlineData(367, false, "1-year-old")]
[InlineData(750, false, "2-year-old")]
public void HyphenatedAge(int days, bool toWords, string expected)
{
foreach (var cultureName in englishCultureNames)
{
var actual = cultureName is null ?
TimeSpan.FromDays(days).ToHyphenatedAge() :
TimeSpan.FromDays(days).ToHyphenatedAge(new CultureInfo(cultureName));
Assert.Equal(expected, actual);
}
var actual = TimeSpan.FromDays(days).ToHyphenatedAge(toWords: toWords);
Assert.Equal(expected, actual);
}

[Theory]
Expand Down Expand Up @@ -479,9 +469,9 @@ public void CanSpecifyCultureExplicitly(int ms, int precision, string culture, s
}
[Theory]
[InlineData(31 * 4, 1, "en-US", "four months")]
[InlineData(236,2,"ar", "سبعة أشهر, اثنان و عشرون يوم")]
[InlineData(321, 2,"es", "diez meses, dieciséis días")]
public void CanSpecifyCultureExplicitlyToWords(int days, int precision,string culture, string expected)
[InlineData(236, 2, "ar", "سبعة أشهر, اثنان و عشرون يوم")]
[InlineData(321, 2, "es", "diez meses, dieciséis días")]
public void CanSpecifyCultureExplicitlyToWords(int days, int precision, string culture, string expected)
{
var timeSpan = new TimeSpan(days, 0, 0, 0);
var actual = timeSpan.Humanize(precision: precision, culture: new CultureInfo(culture), maxUnit: TimeUnit.Year, toWords: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,8 @@ namespace Humanizer
{
public static string Humanize(this System.TimeSpan timeSpan, int precision = 1, System.Globalization.CultureInfo culture = null, Humanizer.Localisation.TimeUnit maxUnit = 5, Humanizer.Localisation.TimeUnit minUnit = 0, string collectionSeparator = ", ", bool toWords = False) { }
public static string Humanize(this System.TimeSpan timeSpan, int precision, bool countEmptyUnits, System.Globalization.CultureInfo culture = null, Humanizer.Localisation.TimeUnit maxUnit = 5, Humanizer.Localisation.TimeUnit minUnit = 0, string collectionSeparator = ", ", bool toWords = False) { }
public static string ToAge(this System.TimeSpan timeSpan, System.Globalization.CultureInfo culture = null, bool toWords = False) { }
public static string ToHyphenatedAge(this System.TimeSpan timeSpan, System.Globalization.CultureInfo culture = null, bool toWords = False) { }
}
public class static To
{
Expand Down Expand Up @@ -1901,6 +1903,8 @@ namespace Humanizer.Localisation.Formatters
protected virtual string GetResourceKey(string resourceKey, int number) { }
protected virtual string GetResourceKey(string resourceKey) { }
public virtual string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit, bool toWords = False) { }
public virtual string TimeSpanHumanize_Age() { }
public virtual string TimeSpanHumanize_PreHyphenatedAge() { }
public virtual string TimeSpanHumanize_Zero() { }
}
public interface IFormatter
Expand All @@ -1910,6 +1914,8 @@ namespace Humanizer.Localisation.Formatters
string DateHumanize_Never();
string DateHumanize_Now();
string TimeSpanHumanize(Humanizer.Localisation.TimeUnit timeUnit, int unit, bool toWords = False);
string TimeSpanHumanize_Age();
string TimeSpanHumanize_PreHyphenatedAge();
string TimeSpanHumanize_Zero();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Humanizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NuSpecs", "NuSpecs", "{AA44
..\NuSpecs\Humanizer.Core.da.nuspec = ..\NuSpecs\Humanizer.Core.da.nuspec
..\NuSpecs\Humanizer.Core.de.nuspec = ..\NuSpecs\Humanizer.Core.de.nuspec
..\NuSpecs\Humanizer.Core.el.nuspec = ..\NuSpecs\Humanizer.Core.el.nuspec
..\NuSpecs\Humanizer.Core.en-US.nuspec = ..\NuSpecs\Humanizer.Core.en-US.nuspec
..\NuSpecs\Humanizer.Core.en.nuspec = ..\NuSpecs\Humanizer.Core.en.nuspec
..\NuSpecs\Humanizer.Core.es.nuspec = ..\NuSpecs\Humanizer.Core.es.nuspec
..\NuSpecs\Humanizer.Core.fa.nuspec = ..\NuSpecs\Humanizer.Core.fa.nuspec
..\NuSpecs\Humanizer.Core.fi-FI.nuspec = ..\NuSpecs\Humanizer.Core.fi-FI.nuspec
Expand Down
12 changes: 12 additions & 0 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public virtual string TimeSpanHumanize(TimeUnit timeUnit, int unit, bool toWords
return GetResourceForTimeSpan(timeUnit, unit, toWords);
}

/// <inheritdoc/>
public virtual string TimeSpanHumanize_Age()
{
return Resources.GetResource("TimeSpanHumanize_Age", _culture);
}

/// <inheritdoc/>
public virtual string TimeSpanHumanize_PreHyphenatedAge()
{
return Resources.GetResource("TimeSpanHumanize_PreHyphenatedAge", _culture);
}

/// <inheritdoc cref="IFormatter.DataUnitHumanize(DataUnit, double, bool)"/>
public virtual string DataUnitHumanize(DataUnit dataUnit, double count, bool toSymbol = true)
{
Expand Down
17 changes: 17 additions & 0 deletions src/Humanizer/Localisation/Formatters/IFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ public interface IFormatter
/// <returns></returns>
string TimeSpanHumanize(TimeUnit timeUnit, int unit, bool toWords = false);

/// <summary>
/// Returns the age format that converts a humanized TimeSpan string to an age expression.
/// For instance, in English that format adds the " old" suffix, so that "40 years" becomes "40 years old".
/// </summary>
/// <returns>Age format</returns>
string TimeSpanHumanize_Age();

/// <summary>
/// Returns the age format that converts a humanized TimeSpan string to an hyphenated age expression,
/// before spaces are actually replaced by hyphens.
/// </summary>
/// <returns>Pre-hyphenated age format</returns>
/// <remarks>
/// In all languages but English an empty string is returned, as the notion of hyphenated age does not exist.
/// </remarks>
string TimeSpanHumanize_PreHyphenatedAge();

/// <summary>
/// Returns the string representation of the provided DataUnit, either as a symbol or full word
/// </summary>
Expand Down
Loading

0 comments on commit 9ae7082

Please sign in to comment.