Refactored AbpDataAnnotationAutoLocalizationMetadataDetailsProvider

pull/400/head
Halil ibrahim Kalkan 7 years ago
parent 1913ced92e
commit fc40d2f79b

@ -12,6 +12,8 @@ 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;
@ -24,27 +26,38 @@ namespace Volo.Abp.AspNetCore.Mvc
public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
{
var displayMetadata = context.DisplayMetadata;
if (displayMetadata.DisplayName != null)
{
return;
}
var attributes = context.Attributes;
var displayAttribute = attributes.OfType<DisplayAttribute>().FirstOrDefault();
var displayNameAttribute = attributes.OfType<DisplayNameAttribute>().FirstOrDefault();
if (displayAttribute != null || displayNameAttribute != null)
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);
if (context.Key.Name == "BirthDate")
displayMetadata.DisplayName = () =>
{
displayMetadata.DisplayName = () => localizer[context.Key.Name];
}
var localizedString = localizer[PropertyLocalizationKeyPrefix + context.Key.Name];
if (localizedString.ResourceNotFound)
{
localizedString = localizer[context.Key.Name];
}
return localizedString;
};
}
}
}

@ -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)

Loading…
Cancel
Save