diff --git a/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorMessageLocalizerExtensions.cs b/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorMessageLocalizerExtensions.cs new file mode 100644 index 0000000000..480b17ac14 --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorMessageLocalizerExtensions.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Linq; +using Blazorise; + +namespace Volo.Abp.Identity.Blazor +{ + public static class AbpIdentityBlazorMessageLocalizerExtensions + { + public static IEnumerable Localize(this AbpIdentityMessageLocalizer abpIdentityMessageLocalizer, + ValidationMessageLocalizerEventArgs eventArgs) + { + return abpIdentityMessageLocalizer.Localize(eventArgs.Messages?.Select(m => (m.Message, m.MessageArguments)))?.ToArray(); + } + } +} diff --git a/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor b/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor index 033a5293d8..e850e5c87a 100644 --- a/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor +++ b/modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor @@ -7,6 +7,7 @@ @using Volo.Abp.PermissionManagement.Blazor.Components @inherits RoleManagementBase @inject IStringLocalizer L +@inject AbpIdentityMessageLocalizer ML @* ************************* PAGE HEADER ************************* *@ @@ -84,7 +85,7 @@ - + @L["DisplayName:RoleName"] @@ -120,7 +121,7 @@ - + @L["DisplayName:RoleName"] diff --git a/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityDomainSharedModule.cs b/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityDomainSharedModule.cs index e14ce5e3f4..5a63d52b24 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityDomainSharedModule.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityDomainSharedModule.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Features; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Features; using Volo.Abp.Identity.Localization; using Volo.Abp.Localization; using Volo.Abp.Localization.ExceptionHandling; @@ -37,6 +38,8 @@ namespace Volo.Abp.Identity { options.MapCodeNamespace("Volo.Abp.Identity", typeof(IdentityResource)); }); + + context.Services.AddSingleton(typeof(AbpIdentityMessageLocalizer<>), typeof(AbpIdentityMessageLocalizer<>)); } } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityMessageLocalizer.cs b/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityMessageLocalizer.cs new file mode 100644 index 0000000000..ff2c50ee83 --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityMessageLocalizer.cs @@ -0,0 +1,46 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Localization; + +namespace Volo.Abp.Identity +{ + public class AbpIdentityMessageLocalizer + { + private readonly IStringLocalizer stringLocalizer; + + public AbpIdentityMessageLocalizer(IStringLocalizer stringLocalizer) + { + this.stringLocalizer = stringLocalizer; + } + + public virtual string Localize(string format, params string[] arguments) + { + try + { + return arguments?.Length > 0 + ? string.Format(stringLocalizer[format], LocalizeArguments(arguments)?.ToArray()) + : stringLocalizer[format]; + } + catch + { + return stringLocalizer[format]; + } + } + + public virtual IEnumerable Localize(IEnumerable<(string format, string[] arguments)> messages) + { + return messages?.Select(m => Localize(m.format, m.arguments)); + } + + protected virtual IEnumerable LocalizeArguments(IEnumerable arguments) + { + foreach (var argument in arguments) + { + yield return stringLocalizer[$"DisplayName:{argument}"] + ?? stringLocalizer[argument] + ?? argument; + } + } + } +}