nhibernate validator (1.3.1) localizing error messages

299 Views Asked by At

I created an unit test case for displaying error messages in a different language than English but it doesn't seem to work and I don't know what I am missing.

Here are the details:

I am using attributes.

app.config (I excluded the nhibernate cfg):

<configSections>
  <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  <section name="nhv-configuration" type="NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator" />    
</configSections>

<nhv-configuration xmlns="urn:nhv-configuration-1.0">
  <property name='apply_to_ddl'>false</property>
  <property name='autoregister_listeners'>true</property>
  <property name='default_validator_mode'>OverrideExternalWithAttribute</property>
</nhv-configuration>

Initialization of the validator:

private void InitializeValidator()
{
  var provider = new NHibernateSharedEngineProvider();
  provider.GetEngine().Configure();
  NHibernate.Validator.Cfg.Environment.SharedEngineProvider = provider;
}

The test function (EntityDescription is my entity class and the Repository follows the sharp architecture design with Repository classes):

[Test]
public void TestNhValidationSp()
{
  CultureInfo ci = CultureInfo.GetCultureInfo("fr");

  Thread.CurrentThread.CurrentCulture = ci;
  Thread.CurrentThread.CurrentUICulture = ci;
  TestNhValidation();
}

private void TestNhValidation()
{
  IEntityDescriptionRepository repository = GetObject<IEntityDescriptionRepository>();

  ISession session = NHibernateSession.Current;
  EntityDescription entityDescription=
    (from kpad in session.Query<EntityDescription>()
     select kpad).FirstOrDefault();
  entityDescription.Title = "012345678901234567890123456789012345678901234567890123456789";
  try
  {
    repository.SaveOrUpdateWithTransaction(entityDescription);
    Assert.IsTrue(false);
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
    Assert.IsTrue(ex is InvalidStateException);
    InvalidStateException isex = (InvalidStateException) ex;
    foreach (InvalidValue invalidValue in isex.GetInvalidValues())
    {
      Console.WriteLine("PropertyName={0} Message={1}", invalidValue.PropertyName, invalidValue.Message);
    }
  }
} 

Setting entityDescription.Title = "012345678901234567890123456789012345678901234567890123456789";

will trigger a validation error because the title can have up to 50 chars.

The problem is that the message always comes in English. Any thoughts?

One thing I want to add is that my test project has a dependency on the SharpArchitecture project (1.9.5). I wonder if somehow this screws up my nhibernate validator cnfiguration.

Found this message: NHibernate Validation Localization with S#arp Architecture that reports a similar problem.

1

There are 1 best solutions below

0
On

I think it was my own fault due to a dll versions mix-up.

SharpArchitecture 1.9.5 uses NHV 1.2.0 which in turn has been compiled with NH 2.1.0. My test above used NHV 1.3.1 (I wanted to upgrade to the latest version of NHV) and it didn't work, the messages didn't show in the right language.

I recompiled NHV 1.2.0 to use NH 3.1.0, I switched to NHV 1.2.0 and now the test works fine.