mirror of https://github.com/abpframework/abp
Merge pull request #15275 from abpframework/auto-merge/rel-7-0/1628
Merge branch dev with rel-7.0pull/15283/head
commit
95996ea7fc
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.Localization;
|
||||
|
||||
public class AbpEnumLocalizer : IAbpEnumLocalizer, ITransientDependency
|
||||
{
|
||||
protected readonly IStringLocalizerFactory StringLocalizerFactory;
|
||||
|
||||
public AbpEnumLocalizer(IStringLocalizerFactory stringLocalizerFactory)
|
||||
{
|
||||
StringLocalizerFactory = stringLocalizerFactory;
|
||||
}
|
||||
|
||||
public virtual string GetString(Type enumType, object enumValue)
|
||||
{
|
||||
return GetStringInternal(enumType, enumValue, StringLocalizerFactory.CreateDefaultOrNull());
|
||||
}
|
||||
|
||||
public virtual string GetString(Type enumType, object enumValue, params IStringLocalizer[] specifyLocalizers)
|
||||
{
|
||||
return GetStringInternal(enumType, enumValue, specifyLocalizers);
|
||||
}
|
||||
|
||||
protected virtual string GetStringInternal(Type enumType, object enumValue, params IStringLocalizer[] specifyLocalizers)
|
||||
{
|
||||
var memberName = enumType.GetEnumName(enumValue);
|
||||
var localizedString = GetStringOrNull(
|
||||
specifyLocalizers,
|
||||
new[]
|
||||
{
|
||||
$"Enum:{enumType.Name}.{enumValue}",
|
||||
$"Enum:{enumType.Name}.{memberName}",
|
||||
$"{enumType.Name}.{enumValue}",
|
||||
$"{enumType.Name}.{memberName}",
|
||||
memberName
|
||||
}
|
||||
);
|
||||
|
||||
return localizedString ?? memberName;
|
||||
}
|
||||
|
||||
protected virtual string GetStringOrNull(IStringLocalizer[] localizers, IEnumerable<string> keys)
|
||||
{
|
||||
foreach (var key in keys)
|
||||
{
|
||||
foreach (var l in localizers)
|
||||
{
|
||||
if (l == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var localizedString = l[key];
|
||||
if (!localizedString.ResourceNotFound)
|
||||
{
|
||||
return localizedString.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
namespace Volo.Abp.Localization;
|
||||
|
||||
public static class AbpEnumLocalizerExtensions
|
||||
{
|
||||
public static string GetString<TEnum>(this IAbpEnumLocalizer abpEnumLocalizer, object enumValue)
|
||||
where TEnum : Enum
|
||||
{
|
||||
return abpEnumLocalizer.GetString(typeof(TEnum), enumValue);
|
||||
}
|
||||
|
||||
public static string GetString<TEnum>(this IAbpEnumLocalizer abpEnumLocalizer, object enumValue, IStringLocalizer[] specifyLocalizers)
|
||||
where TEnum : Enum
|
||||
{
|
||||
return abpEnumLocalizer.GetString(typeof(TEnum), enumValue, specifyLocalizers);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
namespace Volo.Abp.Localization;
|
||||
|
||||
public interface IAbpEnumLocalizer
|
||||
{
|
||||
string GetString(Type enumType, object enumValue);
|
||||
|
||||
string GetString(Type enumType, object enumValue, IStringLocalizer[] specifyLocalizers);
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Shouldly;
|
||||
using Volo.Abp.Localization.TestResources.Base.Validation;
|
||||
using Volo.Abp.Testing;
|
||||
using Xunit;
|
||||
|
||||
namespace Volo.Abp.Localization;
|
||||
|
||||
public class AbpEnumLocalizer_Tests : AbpIntegratedTest<AbpLocalizationTestModule>
|
||||
{
|
||||
private readonly IAbpEnumLocalizer _enumLocalizer;
|
||||
|
||||
public AbpEnumLocalizer_Tests()
|
||||
{
|
||||
_enumLocalizer = GetRequiredService<IAbpEnumLocalizer>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetString_Test()
|
||||
{
|
||||
using (CultureHelper.Use("en"))
|
||||
{
|
||||
_enumLocalizer.GetString<BookType>(BookType.Undefined).ShouldBe("Undefined");
|
||||
_enumLocalizer.GetString<BookType>(BookType.Adventure).ShouldBe("Adventure");
|
||||
_enumLocalizer.GetString<BookType>(0).ShouldBe("Undefined with value 0");
|
||||
_enumLocalizer.GetString<BookType>(1).ShouldBe("Adventure with value 1");
|
||||
_enumLocalizer.GetString<BookType>(BookType.Biography).ShouldBe("Biography");
|
||||
|
||||
var specifyLocalizer = new[]
|
||||
{
|
||||
GetRequiredService<IStringLocalizerFactory>().Create<LocalizationTestValidationResource>()
|
||||
};
|
||||
_enumLocalizer.GetString<BookType>(BookType.Undefined, specifyLocalizer).ShouldBe("Undefined from ValidationResource");
|
||||
_enumLocalizer.GetString<BookType>(BookType.Adventure, specifyLocalizer).ShouldBe("Adventure from ValidationResource");
|
||||
_enumLocalizer.GetString<BookType>(0, specifyLocalizer).ShouldBe("Undefined with value 0 from ValidationResource");
|
||||
_enumLocalizer.GetString<BookType>(1, specifyLocalizer).ShouldBe("Adventure with value 1 from ValidationResource");
|
||||
_enumLocalizer.GetString<BookType>(BookType.Biography, specifyLocalizer).ShouldBe("Biography from ValidationResource");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum BookType
|
||||
{
|
||||
Undefined,
|
||||
Adventure,
|
||||
Biography,
|
||||
}
|
||||
Loading…
Reference in new issue