< Summary

Information
Class: TeleFlow.Telegram.I18n.Internal.TelegramLocaleMiddleware
Assembly: TeleFlow.Framework.I18n
File(s): /_/src/TeleFlow.Framework.I18n/Internal/TelegramLocaleMiddleware.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 52
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
InvokeAsync()100%11100%
ResolveAsync()87.5%88100%

File(s)

/_/src/TeleFlow.Framework.I18n/Internal/TelegramLocaleMiddleware.cs

#LineLine coverage
 1using TeleFlow.Framework.Middleware;
 2using TeleFlow.Framework.Updates;
 3
 4namespace TeleFlow.Telegram.I18n.Internal;
 5
 6/// <summary>
 7/// Resolves one locale per Telegram update before handler dispatch, then exposes it synchronously through the scoped ac
 8/// </summary>
 9internal sealed class TelegramLocaleMiddleware(
 10    IEnumerable<ILocaleResolver> resolvers,
 11    ITelegramCurrentUpdateAccessor currentUpdate,
 12    LocaleAccessor localeAccessor,
 13    TelegramI18nOptions options) : IUpdateMiddleware
 14{
 915    private readonly IReadOnlyList<ILocaleResolver> _resolvers = resolvers.ToArray();
 16
 17    public async Task InvokeAsync(UpdateContext context, UpdateDelegate next)
 18    {
 919        ArgumentNullException.ThrowIfNull(context);
 920        ArgumentNullException.ThrowIfNull(next);
 21
 922        var resolutionContext = new LocaleResolutionContext(
 923            currentUpdate.Update,
 924            currentUpdate.User,
 925            currentUpdate.Chat);
 926        var locale = await ResolveAsync(resolutionContext, context.CancellationToken).ConfigureAwait(false);
 27
 828        localeAccessor.Initialize(locale);
 829        await next(context).ConfigureAwait(false);
 830    }
 31
 32    private async ValueTask<Locale> ResolveAsync(
 33        LocaleResolutionContext context,
 34        CancellationToken cancellationToken)
 35    {
 2236        foreach (var resolver in _resolvers)
 37        {
 338            var locale = await resolver
 339                .TryResolveAsync(context, cancellationToken)
 340                .ConfigureAwait(false);
 41
 242            if (locale is not null)
 43            {
 144                return locale;
 45            }
 46        }
 47
 748        return Locale.TryCreate(context.User?.LanguageCode, out var telegramLocale)
 749            ? telegramLocale
 750            : options.FallbackLocale;
 851    }
 52}