Fix localizer with async local setter of culture

This commit is contained in:
Sergey Nazarov 2024-05-03 12:09:18 +03:00
parent 7bf91a073c
commit b740317a40
4 changed files with 20 additions and 13 deletions

View File

@ -0,0 +1,14 @@
using Insight.Localizer;
using Insight.TelegramBot.Handling.Handlers;
using Telegram.Bot.Types;
namespace Nocr.TelegramClient.AppServices.Handlers;
public sealed class SetLocalizerCultureHandler : IUpdateHandler
{
public Task Handle(Update update, CancellationToken cancellationToken = default)
{
Localizer.CurrentCulture = "ru-ru";
return Task.CompletedTask;
}
}

View File

@ -4,7 +4,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Nocr.TelegramClient.AppServices.Handlers; using Nocr.TelegramClient.AppServices.Handlers;
using Nocr.TelegramClient.AppServices.Handlers.Messages.FeedbackMessage;
using Nocr.TelegramClient.AppServices.Options; using Nocr.TelegramClient.AppServices.Options;
using Nocr.TelegramClient.AppServices.TextSubscriptions.Handlers; using Nocr.TelegramClient.AppServices.TextSubscriptions.Handlers;
using Nocr.TelegramClient.AppServices.Users; using Nocr.TelegramClient.AppServices.Users;
@ -27,6 +26,7 @@ public static class ServiceCollectionExtensions
services.AddHttpClient(); services.AddHttpClient();
services.Configure<AdministrationOptions>(configuration.GetSection(nameof(AdministrationOptions))); services.Configure<AdministrationOptions>(configuration.GetSection(nameof(AdministrationOptions)));
services.AddScoped<IUpdateHandler, SetLocalizerCultureHandler>();
services.AddScoped<IUpdateHandler, UpdateTelegramLoggingHandler>(); services.AddScoped<IUpdateHandler, UpdateTelegramLoggingHandler>();
services.Configure<UsersRestEaseOptions>(configuration.GetSection(nameof(UsersRestEaseOptions))); services.Configure<UsersRestEaseOptions>(configuration.GetSection(nameof(UsersRestEaseOptions)));

View File

@ -29,14 +29,9 @@ public class Startup
.GetSection(nameof(LocalizerConfiguration)) .GetSection(nameof(LocalizerConfiguration))
.Get<LocalizerConfiguration>(); .Get<LocalizerConfiguration>();
Localizer.Initialize(localizerConfiguration); Localizer.Initialize(localizerConfiguration);
services.AddSingleton<ILocalizer, Localizer>();
services.AddSingleton(new Localizer()); services.AddAppServices(Configuration);
// Хак, чтобы постоянно использовать русскую локализацию. В идеале CurrentCulture должен выставляться на основании контекста пользователя в месте инжекта.
services.AddScoped<ILocalizer>(ctx =>
{
Localizer.CurrentCulture = "ru-ru";
return ctx.GetRequiredService<Localizer>();
});
services.AddSingleton<ICurrentDateProvider, DefaultCurrentDateProvider>(); services.AddSingleton<ICurrentDateProvider, DefaultCurrentDateProvider>();
services.AddTelegramBot(bot => services.AddTelegramBot(bot =>
@ -48,8 +43,6 @@ public class Startup
.WithPolling(polling => polling.WithExceptionHandler<LoggingPollingExceptionHandler>())); .WithPolling(polling => polling.WithExceptionHandler<LoggingPollingExceptionHandler>()));
services.AddTelegramBotHandling(typeof(BotClient).Assembly); services.AddTelegramBotHandling(typeof(BotClient).Assembly);
services.AddAppServices(Configuration);
services.Configure<RebusRabbitMqOptions>(Configuration.GetSection(nameof(RebusRabbitMqOptions))); services.Configure<RebusRabbitMqOptions>(Configuration.GetSection(nameof(RebusRabbitMqOptions)));
services.AddRebus((builder, ctx) => services.AddRebus((builder, ctx) =>
builder.Transport(t => builder.Transport(t =>