Humanizer is an open-source library for C# that provides a series of methods to facilitate the readability and localization of text strings in .NET applications.
The concept of the library is to generate more readable texts for human consumption, making the interface more pleasant.
Among the functionalities offered by Humanizer are:
- Date and time formatting
- Pluralization and singularization of strings
- Handling of uppercase and lowercase
- Generation of random names
It is a very powerful library with many useful methods that will save us a great deal of work.
How to use Humanizer
We can add the library to a .NET project easily, 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 lot more functions. You can check the project documentation to see all the options.
It’s worth noting that Humanizer is compatible with multiple languages, including Spanish. In general, it is very well translated and you shouldn’t have any problems.
Humanizer is Open Source, and all the project code is available on GitHub - Humanizr/Humanizer

