fredrikholm / Infolyzer.IcuParser

ICU Message Format Parser
MIT License
0 stars 0 forks source link
icu icu-dotnet icu-library icu-messageformat messageform parser-library

Infolyzer.IcuParser

An ICU message format parser for .NET Core 2.2 or later.

Get it on NuGet:

PM> Install-Package Infolyzer.IcuParser

What problem does it solve?

Let's say you want to prevent unlimited login retries in your web app. After, let's say, three invalid login attempts, the account gets locked for five minutes.

What I used to do in this scenario was simply to display a message like this:

Simple as that. But then the user tries again, which displays this message at first:

So, I need to add the capability to have variables in my messages, no problem. I just use something like this:

Your account is still locked, please try again in {remainingMinutes} minutes.

But some minutes later, as our impatient user hammers the keyboard, this I display this:

Not so nice.

Some people use the pragmatic but hacky approch to cover all variants:

Come on, sure we can do better than that, right?

Yes we can. Enter ICU message formatting.

Smarter Message Formatting

The cool thing about this format is that it supports pluralization. With a slightly more complex message template I can display any of these three variations:

Here's the new message template:

Your account is still locked, please try again in {
    remainingMinutes, plural,
        =0 {just a moment}
        =1 {about one minute}
        other {in # minutes}
}

Here I have used some indentation to make it more readable, but typically these expressions live in files the don't work well with line breaks, like JSON files. So in my translation files, the above would look more like this:

{ "StillLockedOut": "Your account is still locked, please try again in {remainingMinutes, plural, =0 {just a moment} =1 {about one minute} other {in # minutes}}" }

There is also support for set-based selectors, so I could utilize a gender variable to use the correct pronouns. Yeah!

Links