I\'m trying to implement localization in a custom validation attribute in asp.net core 1.0. This is my simplified viewmodel:
public class EditPasswordViewMod
The answer from Ramin is the correct answer. But I decided to take another path, so I don't have to write adapters and adapter providers for many cases.
The idea is to wrap your specific string localizer in a service interface, and get it from the validation attribute itself.
public class CPFAttribute: ValidationAttribute
{
public CPFAttribute()
{
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string cpf;
try
{
cpf = (string)value;
}
catch (Exception)
{
return new ValidationResult(GetErrorMessage(validationContext));
}
if (string.IsNullOrEmpty(cpf) || cpf.Length != 11 || !StringUtil.IsDigitsOnly(cpf))
{
return new ValidationResult(GetErrorMessage(validationContext));
}
return ValidationResult.Success;
}
private string GetErrorMessage(ValidationContext validationContext)
{
if (string.IsNullOrEmpty(ErrorMessage))
{
return "Invalid CPF";
}
ErrorMessageTranslationService errorTranslation = validationContext.GetService(typeof(ErrorMessageTranslationService)) as ErrorMessageTranslationService;
return errorTranslation.GetLocalizedError(ErrorMessage);
}
}
Then the service can be created as:
public class ErrorMessageTranslationService
{
private readonly IStringLocalizer _sharedLocalizer;
public ErrorMessageTranslationService(IStringLocalizer sharedLocalizer)
{
_sharedLocalizer = sharedLocalizer;
}
public string GetLocalizedError(string errorKey)
{
return _sharedLocalizer[errorKey];
}
}
The service can be registered as a singleton, in the Startup class.
services.AddSingleton();
If these validation attributes need to be factored to another assembly, just create an interface for this translation service that can be referenced by all validation attributes you create.