Could anyone pass on a bit of advice about using ErrorMessageResourceName and ErrorMessageResourceType with resource files. I am using an MVC app and we are setting the data annotations in the Data Model. Should the resource file (in a App_LocalResources
folder) be at the Data Model or where the View is contained?
I don't think it matters exactly where the RESX file is as long as you can "see" the type from wherever you're using the Data Annotations. For example, I think you can have your RESX file alongside wherever you're using the Required attribute (such as in
the Models folder). If you're getting Intellisense for the resource type then it should work.
The App_GlobalResources and App_LocalResources folders are primarily intended for resources that are used inside your view pages (though in some cases you can use them in other places).
Thanks,
Eilon
Blog: http://weblogs.asp.net/LeftSlipper/
Marked as answer by ricka6 on Jun 24, 2009 11:11 PM
What I didn't understand was that the resource file must be converter to a class before being able to reference it in the typeof of the ErrorMessageResourceType in the data annotation. I hadn't encountered this ability before of turning a resource file into
a class.
I guess, it's a little difficult to see the advantage of this extra step as if one changes the resource file, then the class must be re-generated to see the change. Our MVC app has very little in the way of view state being passed back and forth so using
the server side approach to resource files may be an acceptable compromise.
Thanks for taking the time to respond to my initial query.
The default behavior of Visual Studio when you put a .resx file into a project is to automatically create the .designer.cs file with the class in it, and whenever you update the .resx file, the class is automatically updated.
DataAnnotations uses this type-based resource system because its scope is larger than "web applications" (for example, it also works with Silverlight applications with RIA Services).
Marked as answer by ricka6 on Jun 24, 2009 11:12 PM
For some reason, when I added resource files to the project, the .designer.cs class wasn't added. I'll have another look at this and see what the story is, but thanks for pointing me in this direction. It makes a lot more sense now.
You definitely put me on the right track with the .designer.cs file. I found out about setting the 'access modifier' to public and also setting the build action of the resource file to 'embedded resource'. This has worked a treat in terms of displaying the
data annotations on screen.
However, how do I now localise this content? I have added another resource file with a resources.fr-FR.resx name but the localised content is not being picked up.
ASP.NET by default does not set the current culture based on the browser request.
You have two choices:
Use some ambient information to determine which culture the UI should be in (for example, choosing a URL "example.com" vs. "example.fr"). In this case, you will need to set System.Threading.Thread.CurrentThread.CurrentUICulture to override the resource
lookup (you should probably also set .CurrentCulture in this case for things like DateTime formatting).
Enable ASP.NET the auto-culture feature in your Web.config, as described
here.
Thanks Brad, I using option 2 already, but it's not working for the data annotations. It's working fine for the MVC ui in that when i set the language in the browser to french, the content in the .fr-FR.resx appears perfectly. However, this doesn't work
for the data annotations of the data model.
I'll keep looking into it and I appreciate your expert help up to this point. It's been super ...
MVClocalizationlocalisationerrormessageresourcenameerrormessageresourcetypeData annotationsdata model
I want to post some advice for everybody who has problem with displaying own errors via resources.
ErrorMessageResoourceType attribute specifies class with custom string properties for example
typeof(MyProject.Properties.Resources)
ErrorMessageResourceName attrib. defines name of the property for example
Title
When you use a generated resource class, check if it has public properties! Resources has only internal properties as default, so you must select resource file (.resx) in Solution Explorer and in window Properties replace Custom Tool field
value "ResXFileCodeGenerator" with "PublicResXFileCodeGenerator" then rebuild solution...
bmoyno
Member
35 Points
68 Posts
Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 10, 2009 02:20 PM|LINK
Hi there,
Could anyone pass on a bit of advice about using ErrorMessageResourceName and ErrorMessageResourceType with resource files. I am using an MVC app and we are setting the data annotations in the Data Model. Should the resource file (in a App_LocalResources folder) be at the Data Model or where the View is contained?
Also, could anyone give a concrete example of
errormessageresourcename errormessageresourcetype data annotation
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 16, 2009 07:01 PM|LINK
Hi,
I don't think it matters exactly where the RESX file is as long as you can "see" the type from wherever you're using the Data Annotations. For example, I think you can have your RESX file alongside wherever you're using the Required attribute (such as in the Models folder). If you're getting Intellisense for the resource type then it should work.
The App_GlobalResources and App_LocalResources folders are primarily intended for resources that are used inside your view pages (though in some cases you can use them in other places).
Thanks,
Eilon
bmoyno
Member
35 Points
68 Posts
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 24, 2009 11:07 AM|LINK
Thanks Eilon,
What I didn't understand was that the resource file must be converter to a class before being able to reference it in the typeof of the ErrorMessageResourceType in the data annotation. I hadn't encountered this ability before of turning a resource file into a class.
I guess, it's a little difficult to see the advantage of this extra step as if one changes the resource file, then the class must be re-generated to see the change. Our MVC app has very little in the way of view state being passed back and forth so using the server side approach to resource files may be an acceptable compromise.
Thanks for taking the time to respond to my initial query.
Regards,
Bmoyno
errormessageresourcename errormessageresourcetype Data annotations
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 24, 2009 08:03 PM|LINK
The default behavior of Visual Studio when you put a .resx file into a project is to automatically create the .designer.cs file with the class in it, and whenever you update the .resx file, the class is automatically updated.
DataAnnotations uses this type-based resource system because its scope is larger than "web applications" (for example, it also works with Silverlight applications with RIA Services).
bmoyno
Member
35 Points
68 Posts
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 26, 2009 09:52 AM|LINK
Hi Brad,
For some reason, when I added resource files to the project, the .designer.cs class wasn't added. I'll have another look at this and see what the story is, but thanks for pointing me in this direction. It makes a lot more sense now.
Cheers ...
bmoyno
Member
35 Points
68 Posts
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 29, 2009 08:12 AM|LINK
Hi Brad,
You definitely put me on the right track with the .designer.cs file. I found out about setting the 'access modifier' to public and also setting the build action of the resource file to 'embedded resource'. This has worked a treat in terms of displaying the data annotations on screen.
However, how do I now localise this content? I have added another resource file with a resources.fr-FR.resx name but the localised content is not being picked up.
Any ideas?
localization localisation errormessageresourcename errormessageresourcetype Data annotations
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 29, 2009 06:26 PM|LINK
ASP.NET by default does not set the current culture based on the browser request.
You have two choices:
There is a lot of discussion about the impact of automatic browser culture that's worth reading: http://www.bing.com/search?q=asp.net+auto+culture
bmoyno
Member
35 Points
68 Posts
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jun 30, 2009 11:39 AM|LINK
Thanks Brad, I using option 2 already, but it's not working for the data annotations. It's working fine for the MVC ui in that when i set the language in the browser to french, the content in the .fr-FR.resx appears perfectly. However, this doesn't work for the data annotations of the data model.
I'll keep looking into it and I appreciate your expert help up to this point. It's been super ...
MVC localization localisation errormessageresourcename errormessageresourcetype Data annotations data model
bmoyno
Member
35 Points
68 Posts
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Jul 09, 2009 08:53 AM|LINK
I needed to add the following entry under <server.web> in the web.config of my mvc app ...
<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />
Now the data annotations are finally appearing localised
MiraJaros
Member
2 Points
1 Post
Re: Data annotations, ErrorMessageResourceName, ErrorMessageResourceType
Sep 14, 2009 06:29 PM|LINK
Hi,
I want to post some advice for everybody who has problem with displaying own errors via resources.