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

Fix #950 Timespan.Humanize( toWords: true) incorrect culture #952

Merged
merged 2 commits into from
Aug 31, 2020
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
12 changes: 11 additions & 1 deletion src/Humanizer.Tests.Shared/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public void TimeSpanWithPrecisionAndCountingEmptyUnits(int milliseconds, int pre
[InlineData(1299630020, 3, "2 weeks, 1 day, and 1 hour")]
[InlineData(1299630020, 4, "2 weeks, 1 day, 1 hour, and 30 seconds")]
[InlineData(1299630020, 5, "2 weeks, 1 day, 1 hour, 30 seconds, and 20 milliseconds")]
public void TimeSpanWithPrecisionAndAlternativeCollectionFormatter(int milliseconds, int precision,
public void TimeSpanWithPrecisionAndAlternativeCollectionFormatter(int milliseconds, int precision,
string expected, bool toWords = false)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precision, collectionSeparator: null, toWords: toWords);
Expand Down Expand Up @@ -433,5 +433,15 @@ public void CanSpecifyCultureExplicitly(int ms, int precision, string culture, s
var actual = TimeSpan.FromMilliseconds(ms).Humanize(precision: precision, culture: new CultureInfo(culture), collectionSeparator: collectionSeparator);
Assert.Equal(expected, actual);
}
[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)
{
var timeSpan = new TimeSpan(days, 0, 0, 0);
var actual = timeSpan.Humanize(precision: precision, culture: new CultureInfo(culture), maxUnit: TimeUnit.Year, toWords: true);
Assert.Equal(expected: expected, actual);
}
}
}
2 changes: 1 addition & 1 deletion src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected virtual string Format(string resourceKey, int number, bool toWords = f
}

return toWords
? resourceString.FormatWith(number.ToWords())
? resourceString.FormatWith(number.ToWords(_culture))
: resourceString.FormatWith(number);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static IEnumerable<string> CreateTheTimePartsWithUpperAndLowerLimits(Tim

foreach (var timeUnitType in timeUnitsEnumTypes)
{
var timepart = GetTimeUnitPart(timeUnitType, timespan, culture, maxUnit, minUnit, cultureFormatter, toWords);
var timepart = GetTimeUnitPart(timeUnitType,timespan, maxUnit, minUnit, cultureFormatter, toWords);

if (timepart != null || firstValueFound)
{
Expand All @@ -85,7 +85,7 @@ private static IEnumerable<TimeUnit> GetEnumTypesForTimeUnit()
return enumTypeEnumerator.Reverse();
}

private static string GetTimeUnitPart(TimeUnit timeUnitToGet, TimeSpan timespan, CultureInfo culture, TimeUnit maximumTimeUnit, TimeUnit minimumTimeUnit, IFormatter cultureFormatter, bool toWords = false)
private static string GetTimeUnitPart(TimeUnit timeUnitToGet, TimeSpan timespan, TimeUnit maximumTimeUnit, TimeUnit minimumTimeUnit, IFormatter cultureFormatter, bool toWords = false)
{
if (timeUnitToGet <= maximumTimeUnit && timeUnitToGet >= minimumTimeUnit)
{
Expand Down