I have an MVC app. In each web request I flip the
Thread.CurrentThread.CurrentUICulture and
Thread.CurrentThread.CurrentCulture to the user's culture. I would
expect any validation messages from nhibernate validator to be in that
culture (assume I test only languages supported by nhibernate
validator).

I created an unit test test fixture for this. The problem is that when
I run all tests in the test fixture class, it runs the english test
first then all the subsequent messages are displayed in English even
though I flipped the thread culture to French respectively to Spanish.
If I run each test individually it works fine, the messages are in the
right language.

So it looks like that whatever nhibernate validator component is
responsible with getting these messages it is not reacting to the
current thread culture to return the messages in the appropriate
language.

Am I missing some something? Do I need to make some other nv api call
to have the messages returned in the appropriate language?

This is the code for the test fixture (I use #architecture framework,
castle windsor and I built some helper functions to create objects):

   [Test]
    public void TestNhValidationEn()
    {
      CultureInfo ci = CultureInfo.GetCultureInfo("en-CA");

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

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

      Thread.CurrentThread.CurrentCulture = ci;
      Thread.CurrentThread.CurrentUICulture = ci;

      TestNhValidation();
    }

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

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

    private void TestNhValidation()
    {
      Console.WriteLine("{0} - {1}", Thread.CurrentThread.Name,
Thread.CurrentThread.CurrentCulture.EnglishName);
      IEntityRepository repository = GetObject<IEntityRepository>();

      ISession session = NHibernateSession.Current;
      Entity entity = (from e in session.Query<Entity>() select
e).FirstOrDefault();
      e.Title =
"012345678901234567890123456789012345678901234567890123456789"; //
this should trigger a validation error
      try
      {
        repository.SaveOrUpdateWithTransaction(entity);
        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);
        }
      }

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to