Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    My projects

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Localize ASP.NET MVC 2 DataAnnotations validation messages

    Living in a country where there are there are three languages being used, almost every application you work on requires some form of localization. In an earlier blog post, I already mentioned ASP.NET MVC 2’s DataAnnotations support for doing model validation. Ever since, I was wondering if it would be possible to use resource files or something to do localization of error messages, since every example that could be found on the Internet looks something like this:

    [MetadataType(typeof(PersonBuddy))]
    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }

    public class PersonBuddy
    {
        [Required(ErrorMessage = "Name is required.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "E-mail is required.")
        public string Email { get; set; }
    }

    Yes, those are hardcoded error messages. And yes, only in one language. Let’s see how localization of these would work.

    1. Create a resource file

    Add a resource file to your ASP.NET MVC 2 application. Not in App_GlobalResources or App_LocalResources, just a resource file in a regular namespace. Next, enter all error messages that should be localized in a key/value manner. Before you leave this file, make sure that the Access Modifier property is set to Public.

    Access modifier in resource file

    2. Update your “buddy classes”

    Update your “buddy classes” (or metadata classes or whatever you call them) to use the ErrorMessageResourceType and ErrorMessageResourceName parameters instead of the ErrorMessage parameter that you normally pass. Here’s the example from above:

    [MetadataType(typeof(PersonBuddy))]
    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }

    public class PersonBuddy
    {
        [Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = "NameRequired")]
        public string Name { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = "EmailRequired")]
        public string Email { get; set; }
    }

    3. See it work!

    After creating a resource file and updating the buddy classes, you can go back to work and use model binders, ValidationMessage and ValidationSummary. ASP.NET will make sure that the correct language is used based on the thread culture info.

    Localized error messages

    kick it on DotNetKicks.com


    Categories: ASP.NET | C# | General | MVC

    Comments

    topsy.com | Reply

    Thursday, November 05, 2009 2:51 PM

    pingback

    Pingback from topsy.com

    Twitter Trackbacks for
            
            Localize ASP.NET MVC 2 DataAnnotations validation messages
            [maartenballiauw.be]
            on Topsy.com

    Manuel Castro Colombia | Reply

    Thursday, November 05, 2009 3:26 PM

    Manuel Castro

    Simple + Useful = Fantastic !!

    Microsoft RealDolmen blogs | Reply

    Thursday, November 05, 2009 3:46 PM

    trackback

    Localize ASP.NET MVC 2 DataAnnotations validation messages

    Localize ASP.NET MVC 2 DataAnnotations validation messages

    DotNetKicks.com | Reply

    Thursday, November 05, 2009 3:50 PM

    trackback

    Localize ASP.NET MVC 2 DataAnnotations validation messages

    You've been kicked (a good thing) - Trackback from DotNetKicks.com

    progg.ru | Reply

    Thursday, November 05, 2009 6:30 PM

    trackback

    Локализация сообщений валидации ASP.NET MVC 2 DataAnnotations

    Thank you for submitting this cool story - Trackback from progg.ru

    uberVU - social comments | Reply

    Friday, November 06, 2009 5:00 AM

    trackback

    Social comments and analytics for this post

    This post was mentioned on Twitter by maartenballiauw: Blogged: Localize ASP.NET MVC 2 DataAnnotations validation messages - http://bit.ly/44EPKw

    HoangDung, Le Vietnam | Reply

    Monday, November 09, 2009 4:17 AM

    HoangDung, Le

    Great job!

    Besause we I in Vietnam and using vietnamese, so I must write apps that support multiple languages, I've successed in implement localization within SharpArch (NHibernate, ASP.NET MVC 1.0).

    How can you using Localization Resources when you separate your entities, controllers and views in separated assemblies? (You can not use Resources.ModelValidation for example)

    Thanks in advanced!

    Klenne Belgium | Reply

    Monday, November 09, 2009 3:02 PM

    Klenne

    All great, there is only one major problem with this.  The localized message have to come from a satellite assembly (dll).

    In asp.net one can specify the resource provider factory to use when performing localization.  If you use an alternate factory (like a database provider), asp.net uses that factory to pull all resources into memory.  DataAnnotations does not use the factory method and you are limited to using the default factory (being satellite assemblies).  Big bummer!!!

    maartenba Belgium | Reply

    Monday, November 09, 2009 7:29 PM

    maartenba

    I did not test it but I think you can actually use any type that has an indexer on it in which the key can be requested.

    Fred Canada | Reply

    Wednesday, November 25, 2009 9:33 PM

    Fred

    Hey Maarten,

    How can I access a resource that resides in another assembly which is not being referenced by the project that contains the dataannotation.
    Can I get it through the caller assembly or somehow?

    Thanks,
    Fred

    maartenba Belgium | Reply

    Thursday, November 26, 2009 10:11 AM

    maartenba

    That would require some plumbing and goes beyond the scope of this blog post and the approach described. Using the approach described, a reference will always have to exist.

    solvista.nl | Reply

    Tuesday, July 13, 2010 8:22 PM

    pingback

    Pingback from solvista.nl

    Scribbler   » DataAnnotaties messages uit resource file lezen ipv hardgecodeerd

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading