Browse Source

WIP message arguments localization

pull/5973/head
Mladen Macanovic 6 years ago
parent
commit
f4e568befe
  1. 15
      modules/identity/src/Volo.Abp.Identity.Blazor/AbpIdentityBlazorMessageLocalizerExtensions.cs
  2. 5
      modules/identity/src/Volo.Abp.Identity.Blazor/Pages/Identity/RoleManagement.razor
  3. 5
      modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityDomainSharedModule.cs
  4. 46
      modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/AbpIdentityMessageLocalizer.cs

15
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<string> Localize<T>(this AbpIdentityMessageLocalizer<T> abpIdentityMessageLocalizer,
ValidationMessageLocalizerEventArgs eventArgs)
{
return abpIdentityMessageLocalizer.Localize(eventArgs.Messages?.Select(m => (m.Message, m.MessageArguments)))?.ToArray();
}
}
}

5
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<IdentityResource> L
@inject AbpIdentityMessageLocalizer<IdentityResource> ML
@* ************************* PAGE HEADER ************************* *@
<Row>
<Column ColumnSize="ColumnSize.Is6">
@ -84,7 +85,7 @@
</ModalHeader>
<ModalBody>
<Validations @ref="@CreateValidationsRef" Mode="ValidationMode.Auto" Model="@NewEntity" ValidateOnLoad="false">
<Validation>
<Validation MessageLocalizer="@((e)=>ML.Localize(e))">
<Field>
<FieldLabel>@L["DisplayName:RoleName"]</FieldLabel>
<TextEdit @bind-Text="@NewEntity.Name">
@ -120,7 +121,7 @@
<ModalBody>
<Validations @ref="@EditValidationsRef" Mode="ValidationMode.Auto" Model="@EditingEntity" ValidateOnLoad="false">
<input type="hidden" name="ConcurrencyStamp" @bind-value="EditingEntity.ConcurrencyStamp" />
<Validation>
<Validation MessageLocalizer="@((e)=>ML.Localize(e))">
<Field>
<FieldLabel>@L["DisplayName:RoleName"]</FieldLabel>
<TextEdit @bind-Text="EditingEntity.Name">

5
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<>));
}
}
}

46
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<T>
{
private readonly IStringLocalizer<T> stringLocalizer;
public AbpIdentityMessageLocalizer(IStringLocalizer<T> 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<string> Localize(IEnumerable<(string format, string[] arguments)> messages)
{
return messages?.Select(m => Localize(m.format, m.arguments));
}
protected virtual IEnumerable<string> LocalizeArguments(IEnumerable<string> arguments)
{
foreach (var argument in arguments)
{
yield return stringLocalizer[$"DisplayName:{argument}"]
?? stringLocalizer[argument]
?? argument;
}
}
}
}
Loading…
Cancel
Save