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

@ -37,7 +37,7 @@ public sealed class UpdateTelegramLoggingHandler : IUpdateHandler
_logger.LogWarning("Failed to extract `from` from update:\n{@Update}", update); _logger.LogWarning("Failed to extract `from` from update:\n{@Update}", update);
return Task.CompletedTask; return Task.CompletedTask;
} }
var text = GetText(update); var text = GetText(update);
foreach (var updateReceiverId in _options.UpdateReceiverIds) foreach (var updateReceiverId in _options.UpdateReceiverIds)
{ {
@ -49,7 +49,7 @@ public sealed class UpdateTelegramLoggingHandler : IUpdateHandler
_messageDispatcherQueue.Enqueue(message); _messageDispatcherQueue.Enqueue(message);
} }
} }
return Task.CompletedTask; 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());
// Хак, чтобы постоянно использовать русскую локализацию. В идеале CurrentCulture должен выставляться на основании контекста пользователя в месте инжекта. services.AddAppServices(Configuration);
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 =>
@ -47,8 +42,6 @@ public class Startup
.WithOptions(opt => opt.FromConfiguration(Configuration)) .WithOptions(opt => opt.FromConfiguration(Configuration))
.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) =>