| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using TeleFlow.Framework.Middleware; |
| | | 4 | | using TeleFlow.Telegram.I18n.Internal; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram.I18n; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Registers Telegram update locale resolution and application-provided locale resolvers. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class TelegramI18nServiceCollectionExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adds the engine-neutral scoped locale pipeline to the Telegram handler framework. |
| | | 15 | | /// </summary> |
| | | 16 | | public static IServiceCollection AddTelegramI18n( |
| | | 17 | | this IServiceCollection services, |
| | | 18 | | Action<TelegramI18nOptions>? configure = null) |
| | | 19 | | { |
| | 30 | 20 | | ArgumentNullException.ThrowIfNull(services); |
| | | 21 | | |
| | 30 | 22 | | EnsureTelegramBotRegistered(services, nameof(AddTelegramI18n)); |
| | | 23 | | |
| | 750 | 24 | | if (services.Any(static descriptor => descriptor.ServiceType == typeof(TelegramI18nOptions))) |
| | | 25 | | { |
| | 0 | 26 | | throw new InvalidOperationException($"{nameof(AddTelegramI18n)} cannot be called more than once."); |
| | | 27 | | } |
| | | 28 | | |
| | 30 | 29 | | var options = new TelegramI18nOptions(); |
| | 30 | 30 | | configure?.Invoke(options); |
| | | 31 | | |
| | 30 | 32 | | if (options.FallbackLocale is null) |
| | | 33 | | { |
| | 0 | 34 | | throw new InvalidOperationException("Telegram i18n fallback locale must be configured."); |
| | | 35 | | } |
| | | 36 | | |
| | 30 | 37 | | services.AddSingleton(options); |
| | 30 | 38 | | services.TryAddScoped<LocaleAccessor>(); |
| | 39 | 39 | | services.TryAddScoped<ILocaleAccessor>(static provider => provider.GetRequiredService<LocaleAccessor>()); |
| | 30 | 40 | | services.AddUpdateMiddleware<TelegramLocaleMiddleware>(); |
| | 30 | 41 | | return services; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Adds a scoped application locale resolver. Resolvers run in registration order before Telegram language fallback |
| | | 46 | | /// </summary> |
| | | 47 | | public static IServiceCollection AddTelegramLocaleResolver<TResolver>(this IServiceCollection services) |
| | | 48 | | where TResolver : class, ILocaleResolver |
| | | 49 | | { |
| | 3 | 50 | | ArgumentNullException.ThrowIfNull(services); |
| | | 51 | | |
| | 3 | 52 | | services.AddScoped<TResolver>(); |
| | 6 | 53 | | services.AddScoped<ILocaleResolver>(static provider => provider.GetRequiredService<TResolver>()); |
| | 3 | 54 | | return services; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | private static void EnsureTelegramBotRegistered(IServiceCollection services, string apiName) |
| | | 58 | | { |
| | 330 | 59 | | if (services.All(static descriptor => descriptor.ServiceType != typeof(TelegramBotOptions))) |
| | | 60 | | { |
| | 0 | 61 | | throw new InvalidOperationException( |
| | 0 | 62 | | $"{nameof(TelegramServiceCollectionExtensions.AddTelegramBot)} must be called before {apiName}."); |
| | | 63 | | } |
| | 30 | 64 | | } |
| | | 65 | | } |