1

i am writing object with Model validation. My application is supposed to work with 3 languages( english, german and czech)

How should i assign and after get appropriate language string for validation model?

czech option:

[DisplayName("Nazev")]
[StringLength(200,ErrorMessage="Nazev musi byt 10 az 200 znaku dlouhy",MinimumLength=10)]
[Column]
public string Name { get; set; }

English option:

[DisplayName("Name")]
[StringLength(200,ErrorMessage="Name has to be between 10 and 200",MinimumLength=10)]
[Column]
public string Name { get; set; }
cpoDesign
  • 8,953
  • 13
  • 62
  • 106

2 Answers2

4

You have to use LocalilizedDisplayName attribute, see this question : DisplayName attribute from Resources?

[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "LastNameMandatory")]
[LocalizedDisplayName("LastName")]
public string RenterLastName { get; set; }
Community
  • 1
  • 1
VinnyG
  • 6,883
  • 7
  • 58
  • 76
  • i can choose what ever way i want to implement it. I have never done this before, thats why – cpoDesign Apr 18 '11 at 20:54
  • Yes, you can put this class where you want, for myself I have a Helpers folder in my project. Then, you need to add your resources in the resources file (that you add in App_GlobalResources) – VinnyG Apr 18 '11 at 21:01
  • so in your helpers you just provide variable name in the resource file and after in html helper will find the right tag in resource file and replace it? – cpoDesign Apr 18 '11 at 21:07
0

The LocalizedDisplayName works well if your solution allows you to work with resource strings. unfortunately in my project we have several languages and growing... and the translations are all maintained in the database.

We have therefore taken the approach to

  1. inherit from the attribute in our own dll, and
  2. then get the default format string and use that as a base value for our messages
  3. have our translation factory get the value or register the default in the database

then we import the namespace and give it an alias, the implemented version looks something like this:

using tf = MyDating.Translation;

in the ViewViewModel we do:

[tf.DisplayName("Verify Password")]
[DataType(DataType.Password)]
[tf.Compare("Password")]
public string VerifyPassword
{
    get;
    set;
}

the above CompareAttribute then looks something like this:

public class CompareAttribute : System.ComponentModel.DataAnnotations.CompareAttribute
{
    public CompareAttribute(string otherProperty)
        :base(otherProperty)
    {
        var tf = TranslatetionFactory.Current.GetSection("CompareAttribute");
        var msg = tf.Get(this.ErrorMessageString);
        ErrorMessage = msg;
    }
}