nhibernate validator (1.3.1) 本地化错误消息

发布于 2024-12-17 08:05:10 字数 2606 浏览 3 评论 0原文

我创建了一个单元测试用例,用于以与英语不同的语言显示错误消息,但它似乎不起作用,而且我不知道我错过了什么。

详细信息如下:

我正在使用属性。

app.config(我排除了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>

验证器的初始化:

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

测试函数(EntityDescription是我的实体类,存储库遵循存储库类的尖锐架构设计):

[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);
    }
  }
} 

设置 实体描述.Title = "012345678901234567890123456789012345678901234567890123456789";

将触发验证错误,因为标题最多可以有 50 个字符。

问题是该消息总是以英语发送。有什么想法吗?

我想补充的一件事是我的测试项目依赖于 SharpArchitecture 项目 (1.9.5)。我想知道这是否会搞砸我的休眠验证器配置。

发现此消息: NHibernate Validation Localization with S#arp Architecture 报告了类似的情况问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-12-24 08:05:10

我认为这是我自己的错误,因为 dll 版本混淆了。

SharpArchitecture 1.9.5 使用 NHV 1.2.0,而 NHV 1.2.0 又使用 NH 2.1.0 进行编译。
我上面的测试使用了 NHV 1.3.1(我想升级到最新版本的 NHV),但它不起作用,消息没有以正确的语言显示。

我重新编译 NHV 1.2.0 以使用 NH 3.1.0,我切换到 NHV 1.2.0,现在测试工作正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文