Working With Localization - YoussefSell/Result.Net GitHub Wiki

The method .WithMessage() takes a string message as input and associates it with the Result object instance.

So if you want to attach a localized message to the Result object, you need to do the following:

// so you may have an action that retrieves the localized message string
var localizedMessage = GetLocalizedMessage("some_text_code");

// and then you pass the message value
var result = Result.Failure()
   .WithMessage(localizedMessage)
   .WithCode("some_error_code");

As you can see, this breaks the fluent nature of the Result object instantiation.

Now, how can we fix this?

.WithLocalizedMessage()

In version 1.4.0, we have introduced a new extension method that will allow you to set the localized message in a more fluent manner.

// to use the error code as the text_code
var result = Result.Failure()
   .WithCode("some_error_code")
   .WithLocalizedMessage();

// to use a custom text_code
var result = Result.Failure()
   .WithLocalizedMessage("text_code");

But where do these localized messages come from?

Result.Configure(config => config.Localization)

All the configuration related to the localization behavior can be found in the Configure method on the Result class:

Result.Configure(config =>
{
    config.Localization.ReturnNull = false;
    config.Localization.ThrowIfNotFound = false;
    config.Localization.GetText = (text_code, language_code) => null;
});

We have introduced a mechanism to configure the options related to localization actions. It exposes the following options:

config.Localization.GetText:

You need to set this property to a delegate that will return the appropriate localized string based on a given text_code:

Result.Configure(config =>
{
    config.Localization.GetText = (text_code, _) =>
    {
        // code to retrieve the localized messages, for example, from a resource manager or an in-memory Dictionary
        var localizedMessage = string.Empty;
        return localizedMessage;
    };
});

config.Localization.ThrowIfNotFound:

A flag. If set to true, an exception will be thrown in case the GetText method returns no value (null or empty).

config.Localization.ReturnNull:

A flag. If set to true, null will be returned when no translation has been found for a given text_code. If not, the text_code value will be returned.

ResultMessageLocalizer.Localize():

This method is what WithLocalizedMessage() calls internally to retrieve the messages. It holds the logic for validating the text_code and checking the return value of GetText.

// you can also use it to return the localized messages directly if you need them elsewhere
var localizedMessage = ResultObjectConfiguration.MessageLocalizer.Localize("some_text_code");

How to Use .WithLocalizedMessage()

As we've seen, in order to retrieve the localized message you need a text_code. Here we have two options:

  1. Using error codes as the text code:
var result = Result.Failure()
   .WithCode("some_error_code")
   .WithLocalizedMessage();

Note: If you are using this option, always make sure you are calling WithCode() before WithLocalizedMessage().

  1. Using custom text code:
var result = Result.Failure()
   .WithLocalizedMessage("some_text_code");

var result = Result.Success()
   .WithLocalizedMessage("some_text_code");