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:
@@ -30,4 +30,7 @@ public static class GuestConsts
|
|||||||
public const string DocumentType_PASSPORT = "PASSPORT";
|
public const string DocumentType_PASSPORT = "PASSPORT";
|
||||||
|
|
||||||
public const int GuestDocument_FirstVersion = 1;
|
public const int GuestDocument_FirstVersion = 1;
|
||||||
|
|
||||||
|
|
||||||
|
public const string EmptyField = "-";
|
||||||
}
|
}
|
||||||
@@ -63,8 +63,8 @@ builder.Services.AddSingleton<IStayExportFileEntityService, StayExportFileEntity
|
|||||||
|
|
||||||
// Add Services
|
// Add Services
|
||||||
builder.Services.AddSingleton<IDataListService, StaticDataListService>();
|
builder.Services.AddSingleton<IDataListService, StaticDataListService>();
|
||||||
|
|
||||||
builder.Services.AddScoped<IUserMessageService, UserMessageService>();
|
builder.Services.AddScoped<IUserMessageService, UserMessageService>();
|
||||||
|
builder.Services.AddSingleton<ILocalizerService, LocalizerService>();
|
||||||
|
|
||||||
// Add Factories (and it's services)
|
// Add Factories (and it's services)
|
||||||
builder.Services.AddFactoryDI<IAddressQueryFactory, ConfigFileAddressQueryFactory>();
|
builder.Services.AddFactoryDI<IAddressQueryFactory, ConfigFileAddressQueryFactory>();
|
||||||
|
|||||||
@@ -180,13 +180,13 @@
|
|||||||
|
|
||||||
|
|
||||||
<!-- Common codes -->
|
<!-- Common codes -->
|
||||||
<data name="resultCode.Ok" xml:space="preserve"><value>OK</value></data>
|
<data name="enum.ResultCode.Ok" xml:space="preserve"><value>OK</value></data>
|
||||||
<data name="resultCode.UnknownError" xml:space="preserve"><value>Unknown Error</value></data>
|
<data name="enum.ResultCode.UnknownError" xml:space="preserve"><value>Unknown Error</value></data>
|
||||||
<data name="resultCode.FailedValidation" xml:space="preserve"><value>Failed entity validation</value></data>
|
<data name="enum.ResultCode.FailedValidation" xml:space="preserve"><value>Failed entity validation</value></data>
|
||||||
<data name="resultCode.NotFound" xml:space="preserve"><value>Not found</value></data>
|
<data name="enum.ResultCode.NotFound" xml:space="preserve"><value>Not found</value></data>
|
||||||
<!-- Specific codes -->
|
<!-- Specific codes -->
|
||||||
<data name="resultCode.GuestInStay" xml:space="preserve"><value>Guest (ID: '{0}') is in a stay.</value></data>
|
<data name="enum.ResultCode.GuestInStay" xml:space="preserve"><value>Guest (ID: '{0}') is in a stay.</value></data>
|
||||||
<data name="resultCode.GuestCurrentlyInStay" xml:space="preserve"><value>Guest (ID: '{0}') is already in one stay during the period: {1}</value></data>
|
<data name="enum.ResultCode.GuestCurrentlyInStay" xml:space="preserve"><value>Guest (ID: '{0}') is already in one stay during the period: {1}</value></data>
|
||||||
|
|
||||||
|
|
||||||
<data name="navMenu.GuestDocumentList" xml:space="preserve"><value>Guests</value></data>
|
<data name="navMenu.GuestDocumentList" xml:space="preserve"><value>Guests</value></data>
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Somnia.GuestLog.Services;
|
||||||
|
|
||||||
|
public interface ILocalizerService
|
||||||
|
{
|
||||||
|
public string GetEnumValue<TEnum>(TEnum? enumValue) where TEnum : struct, Enum;
|
||||||
|
public string GetEnumValue<TEnum>(TEnum enumValue) where TEnum : struct, Enum;
|
||||||
|
}
|
||||||
@@ -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];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +1,15 @@
|
|||||||
using Microsoft.Extensions.Localization;
|
|
||||||
using MudBlazor;
|
using MudBlazor;
|
||||||
using Somnia.GuestLog.Enums.Services;
|
using Somnia.GuestLog.Enums.Services;
|
||||||
using Somnia.GuestLog.Enums.UI;
|
using Somnia.GuestLog.Enums.UI;
|
||||||
using Somnia.GuestLog.Models.DTOs;
|
using Somnia.GuestLog.Models.DTOs;
|
||||||
using Somnia.GuestLog.Resources;
|
|
||||||
|
|
||||||
namespace Somnia.GuestLog.Services;
|
namespace Somnia.GuestLog.Services;
|
||||||
|
|
||||||
public class UserMessageService(
|
public class UserMessageService(
|
||||||
IServiceProvider serviceProvider,
|
ILocalizerService Localizer,
|
||||||
ISnackbar snackbar
|
ISnackbar snackbar
|
||||||
) : IUserMessageService
|
) : IUserMessageService
|
||||||
{
|
{
|
||||||
private IStringLocalizer<LanguageLocale> _localizer
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return serviceProvider.GetRequiredService<IStringLocalizer<LanguageLocale>>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void ShowMessage(string text, MessageKind kind, params object[] parameters)
|
public void ShowMessage(string text, MessageKind kind, params object[] parameters)
|
||||||
{
|
{
|
||||||
string formatedMsg = GetFormatedMessage(text, parameters);
|
string formatedMsg = GetFormatedMessage(text, parameters);
|
||||||
@@ -61,7 +50,7 @@ public class UserMessageService(
|
|||||||
|
|
||||||
private string GetMsgForResultCode(ResultCode resultCode)
|
private string GetMsgForResultCode(ResultCode resultCode)
|
||||||
{
|
{
|
||||||
return _localizer[$"resultCode.{Enum.GetName(resultCode)}"];
|
return Localizer.GetEnumValue(resultCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetFormatedMessage(string template, params object[]? parameters)
|
private static string GetFormatedMessage(string template, params object[]? parameters)
|
||||||
|
|||||||
Reference in New Issue
Block a user