SHA256
- Added new "LocalizerService" to get localization for enum values (and other dynamic/db values in the future).
37 lines
958 B
C#
37 lines
958 B
C#
using Microsoft.Extensions.Localization;
|
|
using Somnia.GuestLog.Resources;
|
|
|
|
namespace Somnia.GuestLog.Services;
|
|
|
|
public class LocalizerService(
|
|
IServiceProvider serviceProvider
|
|
) : ILocalizerService
|
|
{
|
|
private IStringLocalizer<LanguageLocale> Localizer
|
|
{
|
|
get
|
|
{
|
|
field ??= serviceProvider.GetRequiredService<IStringLocalizer<LanguageLocale>>();
|
|
return field;
|
|
}
|
|
}
|
|
|
|
public string GetEnumValue<TEnum>(TEnum? enumValue) where TEnum : struct, Enum
|
|
{
|
|
if (enumValue == null)
|
|
{
|
|
return GuestConsts.EmptyField;
|
|
}
|
|
|
|
return GetEnumValue(enumValue.Value);
|
|
}
|
|
|
|
public string GetEnumValue<TEnum>(TEnum enumValue) where TEnum : struct, Enum
|
|
{
|
|
string enumName = typeof(TEnum).Name;
|
|
string? strEnumValue = Enum.GetName(enumValue);
|
|
string localeKey = $"enum.{enumName}.{strEnumValue}";
|
|
|
|
return Localizer[localeKey];
|
|
}
|
|
} |