本地化和DataAnnotations。 GlobalResourceProxyGenerator和PublicResxFile codeGenerator [英] Localization and DataAnnotations. GlobalResourceProxyGenerator and PublicResxFileCodeGenerator
问题描述
为什么DataAnnotation属性有困难访问由PublicResxFile codeGenerator创建的资源?
Why do DataAnnotation attributes have difficulty accessing resources created by PublicResxFileCodeGenerator?
我发现以下属性:
[Compare("NewPassword", ErrorMessageResourceName = "RegisterModel_ConfirmPasswordError", ErrorMessageResourceType = typeof(Resources.Global))]
将无法找到资源,如果它已与PublicResxFile codeGenerator创建。然而,随着GlobalResourceProxyGenerator创建一个相同的资源将正常工作。这两个资源文件设置为内容,住在App_GlobalResources文件。我试图把默认的语言在App_LocalResources文件太多,但它似乎没有什么区别。
我的测试是我的第二语言(GlobalResourceProxyGenerator)的作品,但我的主要语言(PublicResxFile codeGenerator)抛出一个异常(没有找到资源文件)。如果我切换既GlobalResourceProxyGenerator那么一切都很好(但显然没有公开访问)。
Will fail to find the resource if it has been created with PublicResxFileCodeGenerator. However an identical resource created with GlobalResourceProxyGenerator will work correctly. Both resource files are set to Content and live in App_GlobalResources. I've tried putting the default language in App_LocalResources too but it seems to make no difference. My test being that my secondary language (GlobalResourceProxyGenerator) works but my primary language (PublicResxFileCodeGenerator) throws an exception (it fails to find the resource file). If I switch both to GlobalResourceProxyGenerator then everything is fine (but obviously there is no public access).
有谁知道这是为什么?我想的资源转移到今后再组装。
Does anyone know why this is? I'd like to move the resources into another assembly in the future.
推荐答案
这是因为你放这是在ASP.NET一个特殊的文件夹中的 App_GlobalResources文件
文件夹内的资源文件。如果你把你的资源文件在其他地方这应该工作。这也可能是从你的ASP.NET MVC应用程序完全独立的项目。
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:
- 使用默认的Internet模板创建一个新的ASP.NET MVC 3应用程序
- 添加
〜/ Messages.resx
包含RegisterModel_ConfirmPasswordError
资源字符串文件 -
设置自定义工具
PublicResXFile codeGenerator
此资源文件:
- Create a new ASP.NET MVC 3 application using the default internet template
- Add a
~/Messages.resx
file containing theRegisterModel_ConfirmPasswordError
resource string Set the custom tool to
PublicResXFileCodeGenerator
for this resource file:
添加一个模型:
public class MyViewModel
{
[Compare("NewPassword",
ErrorMessageResourceName = "RegisterModel_ConfirmPasswordError",
ErrorMessageResourceType = typeof(MvcApplication1.Messages))]
public string Password { get; set; }
public string NewPassword { get; set; }
}
控制器:
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
查看:
View:
@model MyViewModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.Password)
@Html.EditorFor(x => x.Password)
@Html.ValidationMessageFor(x => x.Password)
</div>
<div>
@Html.LabelFor(x => x.NewPassword)
@Html.EditorFor(x => x.NewPassword)
@Html.ValidationMessageFor(x => x.NewPassword)
</div>
<button type="submit">OK</button>
}
然后,你就可以开始通过提供相应的本地化翻译:
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
- ...
更新:
我在评论区有什么特别之处 App_GlobalResources文件
文件夹,为什么它不使用它的问道。嗯,其实你可以把它的工作。所有你需要做的是将生成操作
到嵌入的资源
。默认情况下,当你添加一个文件到 App_GlobalResources文件
文件夹,Visual Studio将其设置为内容
这意味着这种资源不会被纳入运行时组件和ASP.NET MVC不能找到它:
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:
这篇关于本地化和DataAnnotations。 GlobalResourceProxyGenerator和PublicResxFile codeGenerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!