mirror of https://github.com/abpframework/abp.git
5 changed files with 46 additions and 67 deletions
@ -1,15 +1,44 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Blazorise; |
|||
using Microsoft.Extensions.Localization; |
|||
|
|||
namespace Volo.Abp.Identity.Blazor |
|||
{ |
|||
public static class AbpIdentityBlazorMessageLocalizerExtensions |
|||
{ |
|||
public static IEnumerable<string> Localize<T>(this AbpIdentityMessageLocalizer<T> abpIdentityMessageLocalizer, |
|||
ValidationMessageLocalizerEventArgs eventArgs) |
|||
public static IEnumerable<string> Localize<T>(this IStringLocalizer<T> stringLocalizer, ValidationMessageLocalizerEventArgs eventArgs) |
|||
{ |
|||
return abpIdentityMessageLocalizer.Localize(eventArgs.Messages?.Select(m => (m.Message, m.MessageArguments)))?.ToArray(); |
|||
return LocalizeMessages(stringLocalizer, eventArgs.Messages?.Select(m => (m.Message, m.MessageArguments)))?.ToArray(); |
|||
} |
|||
|
|||
private static IEnumerable<string> LocalizeMessages<T>(IStringLocalizer<T> stringLocalizer, IEnumerable<(string format, string[] arguments)> messages) |
|||
{ |
|||
return messages?.Select(m => LocalizeMessage(stringLocalizer, m.format, m.arguments)); |
|||
} |
|||
|
|||
private static string LocalizeMessage<T>(IStringLocalizer<T> stringLocalizer, string format, params string[] arguments) |
|||
{ |
|||
try |
|||
{ |
|||
return arguments?.Length > 0 |
|||
? string.Format(stringLocalizer[format], LocalizeMessageArguments(stringLocalizer, arguments)?.ToArray()) |
|||
: stringLocalizer[format]; |
|||
} |
|||
catch |
|||
{ |
|||
return stringLocalizer[format]; |
|||
} |
|||
} |
|||
|
|||
private static IEnumerable<string> LocalizeMessageArguments<T>(IStringLocalizer<T> stringLocalizer, IEnumerable<string> arguments) |
|||
{ |
|||
foreach (var argument in arguments) |
|||
{ |
|||
yield return stringLocalizer[$"DisplayName:{argument}"] |
|||
?? stringLocalizer[argument] |
|||
?? argument; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,46 +0,0 @@ |
|||
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…
Reference in new issue