< Summary

Information
Class: TeleFlow.Telegram.I18n.TelegramI18nServiceCollectionExtensions
Assembly: TeleFlow.Framework.I18n
File(s): /_/src/TeleFlow.Framework.I18n/TelegramI18nServiceCollectionExtensions.cs
Line coverage
80%
Covered lines: 17
Uncovered lines: 4
Coverable lines: 21
Total lines: 65
Line coverage: 80.9%
Branch coverage
50%
Covered branches: 5
Total branches: 10
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddTelegramI18n(...)50%6684.61%
AddTelegramLocaleResolver(...)50%22100%
EnsureTelegramBotRegistered(...)50%3250%

File(s)

/_/src/TeleFlow.Framework.I18n/TelegramI18nServiceCollectionExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3using TeleFlow.Framework.Middleware;
 4using TeleFlow.Telegram.I18n.Internal;
 5
 6namespace TeleFlow.Telegram.I18n;
 7
 8/// <summary>
 9/// Registers Telegram update locale resolution and application-provided locale resolvers.
 10/// </summary>
 11public 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    {
 3020        ArgumentNullException.ThrowIfNull(services);
 21
 3022        EnsureTelegramBotRegistered(services, nameof(AddTelegramI18n));
 23
 75024        if (services.Any(static descriptor => descriptor.ServiceType == typeof(TelegramI18nOptions)))
 25        {
 026            throw new InvalidOperationException($"{nameof(AddTelegramI18n)} cannot be called more than once.");
 27        }
 28
 3029        var options = new TelegramI18nOptions();
 3030        configure?.Invoke(options);
 31
 3032        if (options.FallbackLocale is null)
 33        {
 034            throw new InvalidOperationException("Telegram i18n fallback locale must be configured.");
 35        }
 36
 3037        services.AddSingleton(options);
 3038        services.TryAddScoped<LocaleAccessor>();
 3939        services.TryAddScoped<ILocaleAccessor>(static provider => provider.GetRequiredService<LocaleAccessor>());
 3040        services.AddUpdateMiddleware<TelegramLocaleMiddleware>();
 3041        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    {
 350        ArgumentNullException.ThrowIfNull(services);
 51
 352        services.AddScoped<TResolver>();
 653        services.AddScoped<ILocaleResolver>(static provider => provider.GetRequiredService<TResolver>());
 354        return services;
 55    }
 56
 57    private static void EnsureTelegramBotRegistered(IServiceCollection services, string apiName)
 58    {
 33059        if (services.All(static descriptor => descriptor.ServiceType != typeof(TelegramBotOptions)))
 60        {
 061            throw new InvalidOperationException(
 062                $"{nameof(TelegramServiceCollectionExtensions.AddTelegramBot)} must be called before {apiName}.");
 63        }
 3064    }
 65}