Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to customize the English localized text of Identity errors. #7362

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<string, string> IdentityStrings = new Dictionary<string, string>();

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)
Expand Down Expand Up @@ -41,25 +74,19 @@ public static string[] GetValuesFromErrorMessage(this IdentityResult identityRes
}

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<string>();
}

if (FormattedStringValueExtracter.IsMatch(error.Description, englishLocalizedString.Value,
out var values))
{
return values;
}

return Array.Empty<string>();
}

if (FormattedStringValueExtracter.IsMatch(error.Description, englishString, out var values))
{
return values;
}

return Array.Empty<string>();
}

public static string LocalizeErrors(this IdentityResult identityResult, IStringLocalizer localizer)
Expand All @@ -85,16 +112,12 @@ public static string LocalizeErrorMessage(this IdentityError error, IStringLocal

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<object>().ToArray());
}
return string.Format(localizedString.Value, values.Cast<object>().ToArray());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Volo\Abp\Identity\LocalizationExtensions\*.json" />
<Content Remove="Volo\Abp\Identity\LocalizationExtensions\*.json" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,6 +24,18 @@ public override void ConfigureServices(ServiceConfigurationContext context)
{
options.AutoEventSelectors.Add<IdentityUser>();
});

Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<AbpIdentityDomainTestModule>();
});

Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Get<IdentityResource>()
.AddVirtualJson("/Volo/Abp/Identity/LocalizationExtensions");
});
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ public void Should_Localize_Messages()
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.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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."
}
}