Configuration basics
Prefer per-call culture arguments when an overload provides one. Use CultureInfo.CurrentCulture and CurrentUICulture when a Humanizer API follows ambient application culture. Reserve Configurator for application-wide strategies, registries, or enum metadata rules.
Configure global behavior once during application startup, before any request or background worker can use Humanizer.
Configure a strategy
This verified program installs date and duration strategies at startup:
using System.Globalization;
using Humanizer;
var culture = CultureInfo.GetCultureInfo("en-US");
var comparison = new DateTime(2025, 1, 20, 12, 0, 0, DateTimeKind.Utc);
Configurator.DateTimeHumanizeStrategy =
new PrecisionDateTimeHumanizeStrategy(precision: 0.75);
Configurator.TimeSpanHumanizeStrategy =
new SingleUnitTimeSpanHumanizeStrategy();
var result = comparison.AddMinutes(-45).Humanize(
utcDate: true,
dateToCompareAgainst: comparison,
culture: culture);
var duration = TimeSpan.FromMinutes(62).Humanize(
precision: 2,
culture: culture);
Console.WriteLine($"{result}; {duration}");
sealed class SingleUnitTimeSpanHumanizeStrategy : ITimeSpanHumanizeStrategy
{
readonly DefaultTimeSpanHumanizeStrategy defaultStrategy = new();
public string Humanize(
TimeSpan timeSpan,
int precision,
bool countEmptyUnits,
CultureInfo? culture,
TimeUnit maxUnit,
TimeUnit minUnit,
string? collectionSeparator,
bool toWords,
bool toSymbols) =>
defaultStrategy.Humanize(
timeSpan,
Math.Min(precision, 1),
countEmptyUnits,
culture,
maxUnit,
minUnit,
collectionSeparator,
toWords,
toSymbols);
}
It prints:
an hour ago; 1 hour
For a global date strategy, assign
Configurator.DateTimeHumanizeStrategy once at startup. For a custom localized
component, call the relevant registry's Register method before the registry is
resolved.
Configure before first use
LocaliserRegistry<T> freezes registrations on first use, so registering after
resolution throws InvalidOperationException. Likewise, call
Configurator.UseEnumDescriptionPropertyLocator before the first enum
humanization.