Back to list
Views:   13.1K
Replies:  1
Archived

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>         
    [DisplayName("Id")]
    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?
M S, Sep 08, 2010
Reply 1
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).

http://forums.asp.net/p/1433699/3264915.aspx
 
I hope this gets you going.

 
Dan McMillan, Sep 10, 2010


Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas.      Vsn 1.2.2