|
|
|
@ -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)
|
|
|
|
@ -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<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)
|
|
|
|
@ -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<object>().ToArray());
|
|
|
|
|
}
|
|
|
|
|
return string.Format(localizedString.Value, values.Cast<object>().ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|