NHibernate Validator custom messages


After an interesting thread in nhusers, I wrote this is a small post explaining how you can create a simple interpolator for NHibernate Validator that replaces the default messages for your customized ones and as well treats messages for your custom validators. Everything with internationalization using resources.

The first part is to create your own interpolator:

using System.Globalization;
using System.Reflection;
using System.Resources;
using NHibernate.Validator.Engine;

public class CustomMessageInterpolator : IMessageInterpolator
{
    private readonly string ResourceBaseName = "Project.Properties.Validator";

    private readonly ResourceManager resMan;

    public CustomMessageInterpolator()
    {
        this.resMan = new ResourceManager(this.ResourceBaseName, Assembly.GetExecutingAssembly());
    }

    public string Interpolate(string message, IValidator validator, IMessageInterpolator defaultInterpolator)
    {
        var s = GetMessage(message);

        return defaultInterpolator.Interpolate(s, validator, defaultInterpolator);
    }

    private string GetMessage(string message)
    {
        // It's a tempate
        if (!message.StartsWith("{") && !message.EndsWith("}"))
        {
            return message;
        }

        var resource = message.Substring(1, message.Length - 2);

        var m = this.resMan.GetString(resource, CultureInfo.CurrentCulture);

        if (string.IsNullOrEmpty(m))
        {
            // Returns the original message
            return message;
        }

        return m;
    }
}

I use the same notation as the original project, putting the resource name between { and }. I try to get the string from Validator.resx in the Properties folder of the project and if not found I return the original message. This way, you can override the default messages with your own ones only if you want.

At the end, all messages are passed to the defaultInterpolator, that manages the rest of the substitutions (for example {Max} and {Min} are replaced by the attribute values).

Finally, you have to configure the interpolator in the .config file (it only worked for me in App.config, not in nhvalidator.cfg.xml):

<configuration>
  <configSections>
    <section name="nhv-configuration" type="NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator" />  
  </configSections>
  <nhv-configuration xmlns="urn:nhv-configuration-1.0">
    <property name="message_interpolator_class">Project.Validation.CustomMessageInterpolator, Project.Validation</property>
    <mapping assembly="Project.Domain" />
  </nhv-configuration>
</configuration>

And that’s it. I hope it helps.

Related Posts

DIY Arduino Christmas tree lights

A detailed explanation of my home made light switcher made with Arduino and a relay shield

Back to blogging

My statement on getting back to blogging. Will this go anywhere?

Lessons learned optimizing MySQL

A summary of some things I discovered while trying to optimize the performance of a production MySQL server.

Simple Mini Profiler Glimpse plugin

I just created my first Glimpse plugin, integration between Mininprofiler and Glimpse.

Carbon Copy Cloner saved my day

Just a gratitude post because this piece of software was extremely useful when others don't. And it was my birthday.

Troubleshooting MSDTC, RPC and NServiceBus issues

MSDTC is rare and can cause many headaches. This is the process I followed to track down the problems I was having

Using fail2ban with nginx in Debian

fail2ban helps you fight spam and bots but comes with an Apache sample. I converted it to handle Nginx information.

How msiinv saved my day

mssinv is a tool to manage MSI installed packages and I used it to track a problem with a package partially installed, that had to be removed with this tool

Install ASP.NET MVC 3 Manually

Due to some strange problems in my PC, I had to install this package manually and this is applicable to many other installers

Deployment web site Zip with MsBuild and TeamCity

I needed to make a Zip file of a web site and automate it with TeamCity. I used MsBuild and some obscure tricks