SHA256
feat StayExport - Added LocalizerService
- Added new "LocalizerService" to get localization for enum values (and other dynamic/db values in the future).
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user