1

We're currently migrating an existing application to ASP.NET MVC 3. We're reusing the database schema which contains all of our resources in the database rather than resx files.

We have a ResourceProvider that acts as a facade around the resource repository which has a method that accepts a Key and returns the specified resource in the users default culture.

We're going to want to leverage MVC's model DataAnnotations to bind our model to our razor views providing labels and validation etc.

My question is how do we achieve this so that it can talk to our ResourceProvider?

Do I need to implement a custom DataAnnotationsModelMetadataProvider? or is there a better/easier way to achieve this?

Any ideas are appreciated.

2
  • Are you talking only about validation attributes and their corresponding error messages or more generally about things like DisplayName and so on ...? Commented Mar 15, 2012 at 17:27
  • @Darin Dimitrov Validation and DisplayName etc.. Commented Mar 16, 2012 at 8:45

2 Answers 2

2

I've got a ready solution (that works with model and validation localization) which you can use: http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/

All you need to do is to implement the IStringProvider interface to read from your own database instead.

Update

I've now written an article about it.

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for this. Does this method work based on convention i.e. Key names or is there a way to specify this as part of the Attribute? What I'm getting at is would I need to update existing keys for resources in the database?
I don't follow. The interface defines what the library asks for. You can use that information to query the proper values in your database.
OK thanks mate. Let me give this a further look and I'll let you know how I get on/update this thread. Much appreciated.
@TomMiller: Read my article: www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri.
I had to refactor the provider to get it more stable. Sorry for breaking the api. All strings are now provided through this class: github.com/jgauffin/griffin.mvccontrib/blob/master/source/…
|
1

I think this is what you are looking for, it's MVC2 targeted but should just as well work with MVC3 / MVC4:
http://carrarini.blogspot.com/2010/08/localize-aspnet-mvc-2-dataannotations.html

We did the same thing and in the end we went with the T4 Template if I'm not mistaken.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.