Localization and DataAnnotations. GlobalResourceProxyGenerator and PublicResxFileCodeGenerator

前端 未结 1 870
囚心锁ツ
囚心锁ツ 2021-02-05 05:34

Why do DataAnnotation attributes have difficulty accessing resources created by PublicResxFileCodeGenerator?

I find that the following attribute:

[Compar         


        
1条回答
  •  遥遥无期
    2021-02-05 06:09

    That's because you placed your resource file inside the App_GlobalResources folder which is a special folder in ASP.NET. This should work if you put your resources file somewhere else. This could also be a completely separate project from your ASP.NET MVC application.

    Here are the steps you could make this work:

    1. Create a new ASP.NET MVC 3 application using the default internet template
    2. Add a ~/Messages.resx file containing the RegisterModel_ConfirmPasswordError resource string
    3. Set the custom tool to PublicResXFileCodeGenerator for this resource file:

      enter image description here

    4. Add a model:

      public class MyViewModel
      {
          [Compare("NewPassword", 
                   ErrorMessageResourceName = "RegisterModel_ConfirmPasswordError",
                   ErrorMessageResourceType = typeof(MvcApplication1.Messages))]
          public string Password { get; set; }
      
          public string NewPassword { get; set; }
      }
      
    5. Controller:

      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              return View(new MyViewModel());
          }
      
          [HttpPost]
          public ActionResult Index(MyViewModel model)
          {
              return View(model);
          }
      }
      
    6. View:

      @model MyViewModel
      
      @using (Html.BeginForm())
      {
          
      @Html.LabelFor(x => x.Password) @Html.EditorFor(x => x.Password) @Html.ValidationMessageFor(x => x.Password)
      @Html.LabelFor(x => x.NewPassword) @Html.EditorFor(x => x.NewPassword) @Html.ValidationMessageFor(x => x.NewPassword)
      }

    Then you could start localizing by providing respective translations:

    • Messages.fr-FR.resx
    • Messages.de-DE.resx
    • Messages.it-IT.resx
    • Messages.es-ES.resx
    • ...

    UPDATE:

    I was asked in the comments section what's so special about the App_GlobalResources folder and why it doesn't work with it. Well, actually you could make it work. All you need to do is set the Build Action to Embedded Resource. By default when you add a file to the App_GlobalResources folder, Visual Studio set it to Content meaning that this resource will not be incorporated into the runtime assembly and ASP.NET MVC cannot find it:

    enter image description here

    0 讨论(0)
提交回复
热议问题