Samantha J T Star
Samantha J T Star

Reputation: 32808

How should I go about handling localization using MVC3?

I have an application which I need to have work in more than one language. Can someone give me some pointers as to where I should start looking. For example can I decorate the fields in my models with different headings that could be used depending on the language in use?

My application also uses messages like this:

ModelState.AddModelError("username", "You must specify a username.");

How can I code these messages for different languages?

Upvotes: 0

Views: 609

Answers (2)

jgauffin
jgauffin

Reputation: 101150

You can customize a ModelMetadataProvider and a ModelValidatorProvider just as I've done in Griffin.MvcContrib.

With my versions, this is all you need to get localization of models and validations working:

protected void Application_Start()
{
    var stringProvider = new ResourceStringProvider(Resources.LocalizedStrings.ResourceManager);
    ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(stringProvider);
    ModelValidatorProviders.Providers.Clear();
    ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider(stringProvider));
}

More info: http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/

(nuget packages will be available as soon as I've got the administration area working with embedded views)

Upvotes: 0

Related Questions