From ca07c9ac116eec3101dcae67d4091dd9d07da427 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Fri, 10 Jul 2020 12:23:47 +0800 Subject: [PATCH] update Former-commit-id: b3338fd30be0f7934e6df62c9a0b774db2ea1173 Former-commit-id: 0215a318ab681c10f2f7e0f7de03568e25066281 --- .../Extensions/ControllerExtensions.cs | 48 ++++++++++++------- projects/Infrastructure/Web/BaseStartup.cs | 2 - .../Application/Models/EditNodeModel.cs | 2 + .../WebMVC/wwwroot/router/shared/edit.html | 2 +- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/projects/Infrastructure/Extensions/ControllerExtensions.cs b/projects/Infrastructure/Extensions/ControllerExtensions.cs index 90ab055b..d8bd179c 100644 --- a/projects/Infrastructure/Extensions/ControllerExtensions.cs +++ b/projects/Infrastructure/Extensions/ControllerExtensions.cs @@ -1,10 +1,11 @@ -using Jint.Parser.Ast; +using Infrastructure.Resources; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; using System.ComponentModel; @@ -63,6 +64,8 @@ namespace Infrastructure.Extensions } try { + var factory = controller.HttpContext.RequestServices.GetService(); + var localizer = factory.Create("Resources.Resource", Assembly.GetEntryAssembly().GetName().Name); dynamic json = new ExpandoObject(); var dictionary = (IDictionary)json; //type @@ -109,13 +112,13 @@ namespace Infrastructure.Extensions { json.title = metadata.DisplayName; } - if (metadata.IsRequired) - { - if (metadata.ModelType != typeof(bool)) - { - json.required = true; - } - } + //if (metadata.IsRequired) + //{ + // if (metadata.ModelType != typeof(bool)) + // { + // json.required = true; + // } + //} foreach (var attribute in metadata.Attributes.Attributes) { if (attribute is DescriptionAttribute descriptionAttribute) @@ -124,7 +127,16 @@ namespace Infrastructure.Extensions } 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) {//pattern @@ -212,23 +224,23 @@ namespace Infrastructure.Extensions } dynamic properties = new ExpandoObject(); var propertiesDictionary = (IDictionary)properties; - var requiredList = new List(); + //var requiredList = new List(); if (metadata.IsComplexType && !metadata.IsCollectionType && metadata.Properties.Any()) { foreach (var item in metadata.Properties) { var modelMetadataItem = item as DefaultModelMetadata; propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem); - if (modelMetadataItem.IsRequired) - { - requiredList.Add(item.PropertyName); - } + //if (modelMetadataItem.IsRequired) + //{ + // requiredList.Add(item.PropertyName); + //} } json.properties = properties; - if (requiredList.Any()) - { - json.required = requiredList; - } + // if (requiredList.Any()) + // { + // json.required = requiredList; + // } } return json; } diff --git a/projects/Infrastructure/Web/BaseStartup.cs b/projects/Infrastructure/Web/BaseStartup.cs index 4f11ad24..28a1047e 100644 --- a/projects/Infrastructure/Web/BaseStartup.cs +++ b/projects/Infrastructure/Web/BaseStartup.cs @@ -102,9 +102,7 @@ namespace Infrastructure.Web { services.AddDbContext(options => options.UseNpgsql(_connectionString)); } - //https://github.com/aspnet/Entropy/blob/master/samples/Localization.StarterWeb/Startup.cs services.AddLocalization(options => options.ResourcesPath = null); - //https://yetawf.com/BlogEntry/Title/AdditionalMetadataAttribute%20Anyone/?BlogEntry=1005 services.AddMvc(o => { o.ModelMetadataDetailsProviders.Add(new AdditionalMetadataProvider()); diff --git a/projects/IoT.Shared/Application/Models/EditNodeModel.cs b/projects/IoT.Shared/Application/Models/EditNodeModel.cs index d6726a8e..31daedaf 100644 --- a/projects/IoT.Shared/Application/Models/EditNodeModel.cs +++ b/projects/IoT.Shared/Application/Models/EditNodeModel.cs @@ -1,6 +1,8 @@ using Infrastructure.Application; +using Infrastructure.Resources; using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.Security.AccessControl; namespace Application.Models { diff --git a/projects/WebMVC/wwwroot/router/shared/edit.html b/projects/WebMVC/wwwroot/router/shared/edit.html index 85888382..1f11a1b4 100644 --- a/projects/WebMVC/wwwroot/router/shared/edit.html +++ b/projects/WebMVC/wwwroot/router/shared/edit.html @@ -117,7 +117,7 @@ } } else { - var message = property.required; + var message = property.required.message; if (!error) { error = { key: name,