Former-commit-id: b3338fd30be0f7934e6df62c9a0b774db2ea1173
Former-commit-id: 0215a318ab681c10f2f7e0f7de03568e25066281
TSXN
wanggang 5 years ago
parent 57aa32f9f9
commit ca07c9ac11

@ -1,10 +1,11 @@
using Jint.Parser.Ast; using Infrastructure.Resources;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -63,6 +64,8 @@ namespace Infrastructure.Extensions
} }
try try
{ {
var factory = controller.HttpContext.RequestServices.GetService<IStringLocalizerFactory>();
var localizer = factory.Create("Resources.Resource", Assembly.GetEntryAssembly().GetName().Name);
dynamic json = new ExpandoObject(); dynamic json = new ExpandoObject();
var dictionary = (IDictionary<string, object>)json; var dictionary = (IDictionary<string, object>)json;
//type //type
@ -109,13 +112,13 @@ namespace Infrastructure.Extensions
{ {
json.title = metadata.DisplayName; json.title = metadata.DisplayName;
} }
if (metadata.IsRequired) //if (metadata.IsRequired)
{ //{
if (metadata.ModelType != typeof(bool)) // if (metadata.ModelType != typeof(bool))
{ // {
json.required = true; // json.required = true;
} // }
} //}
foreach (var attribute in metadata.Attributes.Attributes) foreach (var attribute in metadata.Attributes.Attributes)
{ {
if (attribute is DescriptionAttribute descriptionAttribute) if (attribute is DescriptionAttribute descriptionAttribute)
@ -124,7 +127,16 @@ namespace Infrastructure.Extensions
} }
else if (attribute is RequiredAttribute requiredAttribute) else if (attribute is RequiredAttribute requiredAttribute)
{ {
json.required = requiredAttribute.ErrorMessage; var localizedString = localizer.GetString(nameof(RequiredAttribute));
var message = requiredAttribute.ErrorMessage;
if (!localizedString.ResourceNotFound)
{
message = string.Format(localizedString.Value, metadata.DisplayName);
}
json.required = new
{
message = localizedString.ResourceNotFound ? requiredAttribute.FormatErrorMessage(metadata.DisplayName) : string.Format(localizedString.Value, metadata.DisplayName)
};
} }
else if (attribute is RegularExpressionAttribute regularExpressionAttribute) else if (attribute is RegularExpressionAttribute regularExpressionAttribute)
{//pattern {//pattern
@ -212,23 +224,23 @@ namespace Infrastructure.Extensions
} }
dynamic properties = new ExpandoObject(); dynamic properties = new ExpandoObject();
var propertiesDictionary = (IDictionary<string, object>)properties; var propertiesDictionary = (IDictionary<string, object>)properties;
var requiredList = new List<string>(); //var requiredList = new List<string>();
if (metadata.IsComplexType && !metadata.IsCollectionType && metadata.Properties.Any()) if (metadata.IsComplexType && !metadata.IsCollectionType && metadata.Properties.Any())
{ {
foreach (var item in metadata.Properties) foreach (var item in metadata.Properties)
{ {
var modelMetadataItem = item as DefaultModelMetadata; var modelMetadataItem = item as DefaultModelMetadata;
propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem); propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem);
if (modelMetadataItem.IsRequired) //if (modelMetadataItem.IsRequired)
{ //{
requiredList.Add(item.PropertyName); // requiredList.Add(item.PropertyName);
} //}
} }
json.properties = properties; json.properties = properties;
if (requiredList.Any()) // if (requiredList.Any())
{ // {
json.required = requiredList; // json.required = requiredList;
} // }
} }
return json; return json;
} }

@ -102,9 +102,7 @@ namespace Infrastructure.Web
{ {
services.AddDbContext<EfDbContext>(options => options.UseNpgsql(_connectionString)); services.AddDbContext<EfDbContext>(options => options.UseNpgsql(_connectionString));
} }
//https://github.com/aspnet/Entropy/blob/master/samples/Localization.StarterWeb/Startup.cs
services.AddLocalization(options => options.ResourcesPath = null); services.AddLocalization(options => options.ResourcesPath = null);
//https://yetawf.com/BlogEntry/Title/AdditionalMetadataAttribute%20Anyone/?BlogEntry=1005
services.AddMvc(o => services.AddMvc(o =>
{ {
o.ModelMetadataDetailsProviders.Add(new AdditionalMetadataProvider()); o.ModelMetadataDetailsProviders.Add(new AdditionalMetadataProvider());

@ -1,6 +1,8 @@
using Infrastructure.Application; using Infrastructure.Application;
using Infrastructure.Resources;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Security.AccessControl;
namespace Application.Models namespace Application.Models
{ {

@ -117,7 +117,7 @@
} }
} }
else { else {
var message = property.required; var message = property.required.message;
if (!error) { if (!error) {
error = { error = {
key: name, key: name,

Loading…
Cancel
Save