Is it possible to use translations for System.ComponentModel.DataAnnotations without aspnet mvc?

15 Views Asked by At

I'm using data annotations validation in a console application. I'd like to translate the error messages while using the default attributes (so not specifying specific resource types / keys).

I've seen some examples that do this using aspnet MVC (with various degrees of success), but in my example I'm not using mvc.

here's a unit test that shows what I'm trying to do:


[Fact]
public void Test1()
{
    CultureInfo.CurrentUICulture = new CultureInfo("nl-NL");
    
    var objectWithValidation = new ObjectWithValidation();
    var validationContext = new ValidationContext(objectWithValidation);
    
    var validationResults = new List<ValidationResult>();
    Validator.TryValidateObject(objectWithValidation, validationContext, validationResults, true);
    
    // Validation results should be in dutch, but they are in en-US
}

public class ObjectWithValidation
{
    [Required()]
    public string Name { get; set; }
}
0

There are 0 best solutions below