Merge branch 'auto-display-name'

pull/400/head
Halil ibrahim Kalkan 7 years ago
commit 53b43df152

@ -0,0 +1,63 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNetCore.Mvc.DataAnnotations;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
namespace Volo.Abp.AspNetCore.Mvc
{
public class AbpDataAnnotationAutoLocalizationMetadataDetailsProvider : IDisplayMetadataProvider
{
private const string PropertyLocalizationKeyPrefix = "Property:";
private readonly Lazy<IStringLocalizerFactory> _stringLocalizerFactory;
private readonly Lazy<IOptions<MvcDataAnnotationsLocalizationOptions>> _localizationOptions;
public AbpDataAnnotationAutoLocalizationMetadataDetailsProvider(IServiceCollection services)
{
_stringLocalizerFactory = services.GetRequiredServiceLazy<IStringLocalizerFactory>();
_localizationOptions = services.GetRequiredServiceLazy<IOptions<MvcDataAnnotationsLocalizationOptions>>();
}
public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
{
var displayMetadata = context.DisplayMetadata;
if (displayMetadata.DisplayName != null)
{
return;
}
var attributes = context.Attributes;
if (attributes.OfType<DisplayAttribute>().Any() ||
attributes.OfType<DisplayNameAttribute>().Any())
{
return;
}
if (_localizationOptions.Value.Value.DataAnnotationLocalizerProvider == null)
{
return;
}
var containerType = context.Key.ContainerType ?? context.Key.ModelType;
var localizer = _localizationOptions.Value.Value.DataAnnotationLocalizerProvider(containerType, _stringLocalizerFactory.Value);
displayMetadata.DisplayName = () =>
{
var localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.Name];
if (localizedString.ResourceNotFound)
{
localizedString = localizer[context.Key.Name];
}
return localizedString;
};
}
}
}

@ -15,6 +15,7 @@ namespace Volo.Abp.AspNetCore.Mvc
AddConventions(options, services);
AddFilters(options);
AddModelBinders(options);
AddMetadataProviders(options, services);
}
private static void AddConventions(MvcOptions options, IServiceCollection services)
@ -34,5 +35,12 @@ namespace Volo.Abp.AspNetCore.Mvc
{
//options.ModelBinderProviders.Add(new AbpDateTimeModelBinderProvider());
}
private static void AddMetadataProviders(MvcOptions options, IServiceCollection services)
{
options.ModelMetadataDetailsProviders.Add(
new AbpDataAnnotationAutoLocalizationMetadataDetailsProvider(services)
);
}
}
}

@ -15,12 +15,12 @@ namespace Volo.Abp.AspNetCore.Mvc.Localization
}
public void AddAssemblyResource(
[NotNull] Type resourceType,
[NotNull] Type resourceType,
params Assembly[] assemblies)
{
if (assemblies.IsNullOrEmpty())
{
assemblies = new Assembly[] {resourceType.Assembly};
assemblies = new[] { resourceType.Assembly };
}
foreach (var assembly in assemblies)

@ -19,7 +19,7 @@ namespace Volo.Abp.AspNetCore.App
public class PersonModel
{
[Display(Name = nameof(BirthDate))]
//[Display(Name = nameof(BirthDate))]
public string BirthDate { get; set; }
public PersonModel()

Loading…
Cancel
Save