From 9fb4ad6e6c962d189f2ca568f00e5aaa5b0659db Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 20 Jan 2021 23:10:16 +0800 Subject: [PATCH] Allows to customize the English localized text of Identity errors. Resolve #7280 --- .../Identity/AbpIdentityResultExtensions.cs | 43 ++++++++----------- .../Volo.Abp.Identity.Domain.Tests.csproj | 6 +++ .../Identity/AbpIdentityDomainTestModule.cs | 15 +++++++ .../AbpIdentityResultException_Tests.cs | 10 ++++- .../Identity/LocalizationExtensions/en.json | 7 +++ 5 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/LocalizationExtensions/en.json diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Microsoft/AspNetCore/Identity/AbpIdentityResultExtensions.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Microsoft/AspNetCore/Identity/AbpIdentityResultExtensions.cs index f71f8f76e9..aa76241ca6 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Microsoft/AspNetCore/Identity/AbpIdentityResultExtensions.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Microsoft/AspNetCore/Identity/AbpIdentityResultExtensions.cs @@ -1,17 +1,18 @@ using System; -using System.Collections; using System.Linq; using System.Collections.Generic; -using System.Globalization; +using System.Resources; using Microsoft.Extensions.Localization; using Volo.Abp.Identity; -using Volo.Abp.Localization; using Volo.Abp.Text.Formatting; namespace Microsoft.AspNetCore.Identity { public static class AbpIdentityResultExtensions { + //TODO: cache? + private static readonly ResourceManager IdentityResourceManager = new ResourceManager("Microsoft.Extensions.Identity.Core.Resources", typeof(UserManager<>).Assembly); + public static void CheckErrors(this IdentityResult identityResult) { if (identityResult.Succeeded) @@ -41,25 +42,19 @@ namespace Microsoft.AspNetCore.Identity } var error = identityResult.Errors.First(); - var key = $"Volo.Abp.Identity:{error.Code}"; + var englishString = IdentityResourceManager.GetString(error.Code); - using (CultureHelper.Use(CultureInfo.GetCultureInfo("en"))) + if (englishString == null) { - var englishLocalizedString = localizer[key]; - - if (englishLocalizedString.ResourceNotFound) - { - return Array.Empty(); - } - - if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value, - out var values)) - { - return values; - } - return Array.Empty(); } + + if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values)) + { + return values; + } + + return Array.Empty(); } public static string LocalizeErrors(this IdentityResult identityResult, IStringLocalizer localizer) @@ -85,16 +80,12 @@ namespace Microsoft.AspNetCore.Identity if (!localizedString.ResourceNotFound) { - using (CultureHelper.Use(CultureInfo.GetCultureInfo("en"))) + var englishString = IdentityResourceManager.GetString(error.Code); + if (englishString != null) { - var englishLocalizedString = localizer[key]; - if (!englishLocalizedString.ResourceNotFound) + if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values)) { - if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value, - out var values)) - { - return string.Format(localizedString.Value, values.Cast().ToArray()); - } + return string.Format(localizedString.Value, values.Cast().ToArray()); } } } diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo.Abp.Identity.Domain.Tests.csproj b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo.Abp.Identity.Domain.Tests.csproj index 1771b68a5c..3413ff430c 100644 --- a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo.Abp.Identity.Domain.Tests.csproj +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo.Abp.Identity.Domain.Tests.csproj @@ -19,6 +19,12 @@ + + + + + + diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs index ccfbb85359..a6228a7cd6 100644 --- a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityDomainTestModule.cs @@ -2,9 +2,12 @@ using Volo.Abp.Authorization.Permissions; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.Identity.EntityFrameworkCore; +using Volo.Abp.Identity.Localization; +using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.Identity; using Volo.Abp.Threading; +using Volo.Abp.VirtualFileSystem; namespace Volo.Abp.Identity { @@ -21,6 +24,18 @@ namespace Volo.Abp.Identity { options.AutoEventSelectors.Add(); }); + + Configure(options => + { + options.FileSets.AddEmbedded(); + }); + + Configure(options => + { + options.Resources + .Get() + .AddVirtualJson("/Volo/Abp/Identity/LocalizationExtensions"); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityResultException_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityResultException_Tests.cs index cc349fd3f3..306ea16c91 100644 --- a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityResultException_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/AbpIdentityResultException_Tests.cs @@ -28,10 +28,18 @@ namespace Volo.Abp.Identity using (CultureHelper.Use("tr")) { var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider)); - + localizeMessage.ShouldContain("Şifre en az 6 karakter uzunluğunda olmalı."); localizeMessage.ShouldContain("Şifre en az bir sayı ya da harf olmayan karakter içermeli."); } + + using (CultureHelper.Use("en")) + { + var localizeMessage = exception.LocalizeMessage(new LocalizationContext(ServiceProvider)); + + localizeMessage.ShouldContain("Password length must be greater than 6 characters."); + localizeMessage.ShouldContain("Password must contain at least one non-alphanumeric character."); + } } } } diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/LocalizationExtensions/en.json b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/LocalizationExtensions/en.json new file mode 100644 index 0000000000..e8f68fb950 --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/LocalizationExtensions/en.json @@ -0,0 +1,7 @@ +{ + "culture": "en", + "texts": { + "Volo.Abp.Identity:PasswordTooShort": "Password length must be greater than {0} characters.", + "Volo.Abp.Identity:PasswordRequiresNonAlphanumeric": "Password must contain at least one non-alphanumeric character." + } +}