Localization in ASP.NET MVC 2 using ModelMetadata - Raj Kaimal

Localization in ASP.NET MVC 2 using ModelMetadata

This post uses an MVC 2 RTM application inside VS 2010 that is targeting the .NET Framework 4.

.NET 4 DataAnnotations comes with a new Display attribute that has several properties including specifying the value that is used for display in the UI and a ResourceType. Unfortunately, this attribute is new and is not supported in MVC 2 RTM.

The good news is it will be supported and is currently available in the MVC Futures release.

The steps to get this working are shown below:

  1. Download the MVC futures library
     
  2. Add a reference to the Microsoft.Web.MVC.AspNet4 dll.
     
  3. Add a folder in your MVC project where you will store the resx files
    image
     
  4. Open the resx file and change “Access Modifier” to “Public”. This allows the resources to accessible from other assemblies. Internaly, it changes the “Custom Tool” used to generate the code behind from  ResXFileCodeGenerator to “PublicResXFileCodeGenerator”

     image 
  5. Confirm that the “Build Action” of the resx is set to “Embedded Resource”
     
  6. Add your localized strings in the resx.
     
  7. Register the new ModelMetadataProvider
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
     
        RegisterRoutes(RouteTable.Routes);
     
        //Add this
        ModelMetadataProviders.Current = new DataAnnotations4ModelMetadataProvider();
        DataAnnotations4ModelValidatorProvider.RegisterProvider();
    }

     
  8. Use the Display attribute in your Model
    public class Employee
    {
        [Display(Name="ID")]
        public int ID { get; set; }
     
        [Display(ResourceType = typeof(Common), Name="Name")]
        public string Name { get; set; }
    }

  9. Use the new HTML UI Helpers in your strongly typed view:
      <%: Html.EditorForModel() %>
      <%: Html.EditorFor(m => m) %>
      <%: Html.LabelFor(m => m.Name) %>

      ..and you are good to go.

    Adventure is out there!

    Published Tuesday, April 27, 2010 11:02 AM by rajbk
    Filed under: , , ,

    Comments

    # re: Localization in ASP.NET MVC 2 using ModelMetadata

    Hello rajbk

    Can I use this metod for multi-lenguage sites?

    For example, can I do something like this?

    Add in v.s 2010:

    common.resx

    common.es-Ar.resx

    common.en-Us.resx

    is it possible?

    Thanks!!

    Sunday, May 9, 2010 9:35 PM by Cristian

    # re: Localization in ASP.NET MVC 2 using ModelMetadata

    Monday, May 10, 2010 12:35 AM by rajbk

    # re: Localization in ASP.NET MVC 2 using ModelMetadata

    Hello  rajbk

    The link is great, thanks friend

    Bye bye!!!

    Monday, May 10, 2010 10:01 AM by Cristian

    # re: Localization in ASP.NET MVC 2 using ModelMetadata

    What about modelbinding. Now when you post a form with the watermark it will be bound to the property on the model. We would need to exclude it during binding? Should we write a custom modelbinder and register it for type string? this modelbinder would explicitly handle the watermark. Is this correct?

    Friday, August 13, 2010 4:02 PM by Peter