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..cb1ffc9d8a 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,50 @@ 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; using Volo.Abp.Identity; -using Volo.Abp.Localization; using Volo.Abp.Text.Formatting; namespace Microsoft.AspNetCore.Identity { public static class AbpIdentityResultExtensions { + private static readonly Dictionary IdentityStrings = new Dictionary(); + + static AbpIdentityResultExtensions() + { + var identityResourceManager = new ResourceManager("Microsoft.Extensions.Identity.Core.Resources", typeof(UserManager<>).Assembly); + var resourceSet = identityResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false); + if (resourceSet == null) + { + throw new AbpException("Can't get the ResourceSet of Identity."); + } + + var iterator = resourceSet.GetEnumerator(); + while (true) + { + if (!iterator.MoveNext()) + { + break; + } + + var key = iterator.Key?.ToString(); + var value = iterator.Value?.ToString(); + if (key != null && value != null) + { + IdentityStrings.Add(key, value); + } + } + + if (!IdentityStrings.Any()) + { + throw new AbpException("ResourceSet values of Identity is empty."); + } + } + public static void CheckErrors(this IdentityResult identityResult) { if (identityResult.Succeeded) @@ -41,25 +74,19 @@ namespace Microsoft.AspNetCore.Identity } var error = identityResult.Errors.First(); - var key = $"Volo.Abp.Identity:{error.Code}"; + var englishString = IdentityStrings.GetOrDefault(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 +112,12 @@ namespace Microsoft.AspNetCore.Identity if (!localizedString.ResourceNotFound) { - using (CultureHelper.Use(CultureInfo.GetCultureInfo("en"))) + var englishString = IdentityStrings.GetOrDefault(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." + } +}