Creating human-friendly texts on UWP with Humanizer

This article highlights some of the many features of the Humanizer for .NET project which also runs in UWP. It’s a project from the .NET Foundation that comes with a lot of helper functions for generating human-readable strings from … well … less human-readable expressions, for multiple cultures. Here’s an overview of the functionality:

  • Humanize and dehumanize strings
  • Truncate strings
  • Format strings
  • Humanize and dehumanize enumerations
  • Humanize dates, times, and time spans
  • Pluralize and singularize
  • Transforming numbers to words
  • Transforming numbers to ordinal words
  • Transforming to and from Roman numerals
  • … and more

I used many features of this package in many apps in several .NET technologies (console apps, WPF, and WCF). I’m a huge fan of the relative date and time functions, because after all:

Your 3 books will be delivered in 10 minutes.” sounds a lot better than ‘”Item: book; Quantity:3; ETA 2018-06-18T13:45:30”.

This extremely useful functionality is available as source on GitHub and as a NuGet package. I created a small app to test the package in UWP.

All you need to do is include the namespace:

using Humanizer;

Then you can call the many functions in the package. Most of these are implemented as extension methods. Here’s an overview of some expressions and their result:

“Can_return_title_Case”.Humanize(LetterCasing.Title) Can Return Title Case
“Pascal case input string is turned into sentence”.Dehumanize() PascalCaseInputStringIsTurnedIntoSentence
“Long text to truncate”.Truncate(10) Long text…
DateTime.UtcNow.AddHours(-30).Humanize() yesterday
TimeSpan.FromMilliseconds(1299630020).Humanize(3, collectionSeparator: null) 2 weeks, 1 day, and 1 hour
“Man”.Pluralize() Men
“process”.ToQuantity(2) 2 processes
“dollar”.ToQuantity(2, “C2”, new CultureInfo(“en-US”)) $2.00 dollars
5.Ordinalize() 5th
3501.ToWords() three thousand five hundred and one
21.ToOrdinalWords() twenty-first
“MMXVIII”.FromRoman().ToString() 2018

Seeing is believing, so here’s how these calls look like in the sample UWP app:

Humanizer_HomePage

The main page of the sample app allows you to set a time using Dean Chalk’s radial UWP TimePicker control. It then displays the selected time, and its Humanized value:

Humanizer_MainPage

I’m not diving further into Humanizer’s features and code here, since it is well documented. My UWP sample app lives here on GitHub.

Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s