Home  /  Questions  /  Question

50   50
Sep 08, 2010

Localization for [Required(...)] attribute

The CustomerModel class has ErrorMessages for Required and StringLength attributes that are hardcoded english strings. 

/// <summary>     
/// Customer Model class.     
/// Properties have data annotations used for Validation and Display.     
/// </summary>     
public class CustomerModel
    /// <summary>         
    /// The Customer Identifier.         
    /// </summary>         
    public int CustomerId { get; set; }


    /// <summary>
    /// Customer Company name.
    /// </summary>         
    [DisplayName("Company Name")]
    [Required(ErrorMessage = "Company Name is required.")]
    [StringLength(30, ErrorMessage = "Company Name can be at most 30 characters")]
    public string CompanyName { get; set; }
 What about error messages in other languages?

830   99.9
Sep 10, 2010
Localization is a fairly large topic, but essentially it is supported throughout .NET.  As such, the Design Pattern Framework supports localization.

To get the errormessage to display in a different language you need something like this:

[Required(ErrorMessageResourceType = typeof(Resources.Errors), 
          ErrorMessageResourceName = "CompanyName_Required")]
Remember that in ASP.NET you also need to set the current culture on the Thread, like so:.

Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
Here is a link to a discussion on these issues (but you can easily find many more references on localization).

I hope this gets you going.

 1 comment
This helps a lot. Thanks Dan... --- M S  Sep 10, 2010