How to provide localized validation messages for validation attributes

前端 未结 4 602
北海茫月
北海茫月 2020-12-09 21:46

I am working on an ASP.NET Core application and I would like to override the default validation error messages for data-annotations, like Required,

4条回答
  •  悲&欢浪女
    2020-12-09 22:07

    So, I landed here because of creating my own custom IStringLocalizer and wanted to share my solution because @jlchavez helped me out.

    I created a MongoDb IStringLocalizer and wanted to use the resources via the DataAnnotations. Problem is that DataAnnotations Attributes expect localizations via a static class exposing the resources.

    One enhancement over @jlchavez is that this will fix the resource messages for all ValidationAttribute(s)

    services.AddTransient();
    services.AddOptions()
            .Configure((options, provider) =>
            {
                options.ModelMetadataDetailsProviders.Add(provider);
            });
    
    
    
    public class Resource
    {
        public string Id => Culture + "." + Name;
        public string Culture { get; set; }
        public string Name { get; set; }
        public string Text { get; set; }
    }
    public class MongoLocalizerFactory : IStringLocalizerFactory
    {
        private readonly IMongoCollection _resources;
        public MongoLocalizerFactory(IMongoCollection resources)
        {
            _resources = resources;
        }
    
        public IStringLocalizer Create(Type resourceSource)
        {
            return new MongoLocalizer(_resources);
        }
    
        public IStringLocalizer Create(string baseName, string location)
        {
            return new MongoLocalizer(_resources);
        }
    }
    public class MongoLocalizer : IStringLocalizer
    {
        private readonly IMongoCollection _resources;
    
        public MongoLocalizer(IMongoCollection resources)
        {
            _resources = resources;
        }
    
        public LocalizedString this[string name]
        {
            get
            {
                var value = GetString(name);
                return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
            }
        }
    
        public LocalizedString this[string name, params object[] arguments]
        {
            get
            {
                var format = GetString(name);
                var value = string.Format(format ?? name, arguments);
                return new LocalizedString(name, value, resourceNotFound: format == null);
            }
        }
    
        public IStringLocalizer WithCulture(CultureInfo culture)
        {
            CultureInfo.DefaultThreadCurrentCulture = culture;
    
            return new MongoLocalizer(_resources);
        }
    
        public IEnumerable GetAllStrings(bool includeAncestorCultures)
        {
            var resources = _resources.Find(r => r.Culture == CultureInfo.CurrentCulture.Parent.Name).ToList();
            return resources.Select(r => new LocalizedString(r.Name, r.Text, false));
    
        }
    
        private string GetString(string name)
        {
            var resource = _resources.Find(r => r.Culture == CultureInfo.CurrentCulture.Parent.Name && r.Name == name).SingleOrDefault();
            if (resource != null)
            {
                return new LocalizedString(resource.Name, resource.Text, false);
            }
            return new LocalizedString(name, name, true);
        }
    }
    
    public class LocalizedValidationMetadataProvider : IValidationMetadataProvider
    {
        private IStringLocalizer _localizer;
        public LocalizedValidationMetadataProvider(IStringLocalizer localizer)
        {
            _localizer = localizer;
        }
    
        public void CreateValidationMetadata(ValidationMetadataProviderContext context)
        {
            foreach(var metadata in context.ValidationMetadata.ValidatorMetadata)
            {
                if (metadata is ValidationAttribute attribute)
                {
                    attribute.ErrorMessage = _localizer[attribute.ErrorMessage].Value;
                }
            }
        }
    }
    

提交回复
热议问题