Language: EN

csharp-humanizer

Create Friendly Texts in .NET with Humanizer

Humanizer is an open-source library for C# that provides a set of methods to make reading and localizing text strings in .NET applications easier.

The concept of the library is to generate more readable text for human reading, making the interface more user-friendly.

Among the features offered by Humanizer are:

  • Formatting of dates and times
  • Pluralization and singularization of strings
  • Handling of capitalization
  • Generation of random names

It is a very powerful library with many useful methods that will save us a lot of work.

How to use Humanizer

We can easily add the library to a .NET project through the corresponding Nuget package.

Install-Package Humanizer

Using Humanizer is very simple. For example, we can convert text strings to a readable format, in different casing types.

"CanReturnTitleCase".Humanize(LetterCasing.Title) => "Can Return Title Case"

"Can_return_title_Case".Humanize(LetterCasing.Title) => "Can Return Title Case"

"CanReturnLowerCase".Humanize(LetterCasing.LowerCase) => "can return lower case"

"CanHumanizeIntoUpperCase".Humanize(LetterCasing.AllCaps) => "CAN HUMANIZE INTO UPPER CASE"

Truncate texts

"Long text to truncate".Truncate(10, Truncator.FixedLength) => "Long text…"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength) => "Long te---"

"Long text to truncate".Truncate(6, Truncator.FixedNumberOfCharacters) => "Long t…"
"Long text to truncate".Truncate(6, "---", Truncator.FixedNumberOfCharacters) => "Lon---"

"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords) => "Long text…"
"Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords) => "Long text---"

Manipulate dates and time intervals.

DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"

DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"

DateTimeOffset.UtcNow.AddHours(1).Humanize() => "an hour from now"

Convert a word to plural or singular.

"Men".Pluralize(inputIsKnownToBeSingular: false) => "Men"
"Man".Pluralize(inputIsKnownToBeSingular: false) => "Men"
"string".Pluralize(inputIsKnownToBeSingular: false) => "strings"

"Men".Singularize(inputIsKnownToBePlural: false) => "Man"
"Man".Singularize(inputIsKnownToBePlural: false) => "Man"
"strings".Singularize(inputIsKnownToBePlural: false) => "string"

In addition to these examples, we can convert enumerations, collections, cardinal numbers, and a bunch of other functions. You can check the project’s documentation for all the options.

It is worth noting that Humanizer is compatible with multiple languages, including Spanish. In general, it is very well translated and you should not have any problems.

Humanizer is Open Source, and all the project’s code is available on GitHub - Humanizr/Humanizer: Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities