< Summary - ZhEaIsNsAaBn.Essentials coverage

Information
Class: ZhEaIsNsAaBn.Utilities.ErrorLocalizer
Assembly: ZhEaIsNsAaBn.LayerResponse
File(s): /_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/LayerResponse/Errors/ErrorLocalizer.cs
Line coverage
96%
Covered lines: 24
Uncovered lines: 1
Coverable lines: 25
Total lines: 100
Line coverage: 96%
Branch coverage
89%
Covered branches: 25
Total branches: 28
Branch coverage: 89.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Localize(...)100%11100%
Localize(...)100%1010100%
Find(...)87.5%88100%
Fill(...)100%44100%
Format(...)66.66%6683.33%

File(s)

/_work/home-runner-1/ZhEaIsNsAaBn.Essentials/ZhEaIsNsAaBn.Essentials/src/Core/LayerResponse/Errors/ErrorLocalizer.cs

#LineLine coverage
 1#region Usings
 2using System.Globalization;
 3using System.Text.RegularExpressions;
 4#endregion
 5
 6namespace ZhEaIsNsAaBn.Utilities;
 7
 8/// <summary>
 9/// The default <see cref="IErrorLocalizer"/>. For each culture from the wanted one up to its neutral parent (<c>ar-EG</
 10/// <c>ar</c>) it asks every <see cref="IErrorMessageSource"/> in order (the app's first, so apps can override package t
 11/// sources' neutral (English) text; then <see cref="Error.Message"/>. Named placeholders (<c>{Field}</c>) are filled fr
 12/// <see cref="Error.Arguments"/>, formatted for the wanted culture; unknown ones stay as written.
 13/// </summary>
 14/// <remarks>
 15/// <b>Transitional:</b> while error catalogs still carry <see cref="Error.MessageAR"/>, it is used as the <c>ar</c> tex
 16/// has one.
 17/// </remarks>
 18public sealed partial class ErrorLocalizer : IErrorLocalizer
 19{
 20    /// <summary>The language of neutral resources and of <see cref="Error.Message"/>.</summary>
 21    public const string NeutralLocale = "en";
 22
 23    private const string ArabicLocale = "ar";
 24
 25    private readonly IErrorMessageSource[] _sources;
 26
 27
 28    /// <summary>Creates the localizer.</summary>
 29    /// <param name="sources">The message sources, highest priority first. May be empty.</param>
 30    /// <exception cref="ArgumentNullException"><paramref name="sources"/> is <see langword="null"/>.</exception>
 3331    public ErrorLocalizer( IEnumerable<IErrorMessageSource> sources )
 32    {
 3333        ArgumentNullException.ThrowIfNull( sources );
 34
 3335        _sources = [ ..sources ];
 3336    }
 37
 38
 39    /// <inheritdoc />
 340    public LocalizedMessage Localize( Error error ) => Localize( error, CultureInfo.CurrentUICulture );
 41
 42    /// <inheritdoc />
 43    public LocalizedMessage Localize( Error error, CultureInfo culture )
 44    {
 3645        ArgumentNullException.ThrowIfNull( error );
 3646        ArgumentNullException.ThrowIfNull( culture );
 47
 10248        for ( var current = culture; !Equals( current, CultureInfo.InvariantCulture ); current = current.Parent )
 49        {
 5150            if ( Find( error.Code, current ) is { } template ) { return new LocalizedMessage( current.Name, Fill( templa
 51
 52
 2753            if ( string.Equals( current.Name, ArabicLocale, StringComparison.OrdinalIgnoreCase ) && !string.IsNullOrEmpt
 54            {
 1255                return new LocalizedMessage( ArabicLocale, Fill( error.MessageAR, error.Arguments, culture ) );
 56            }
 57        }
 58
 59
 1260        var neutral = Find( error.Code, CultureInfo.InvariantCulture ) ?? error.Message;
 61
 62
 1263        return new LocalizedMessage( NeutralLocale, Fill( neutral, error.Arguments, culture ) );
 64    }
 65
 66
 67    private string? Find( string code, CultureInfo culture )
 68    {
 5169        if ( string.IsNullOrEmpty( code ) ) { return null; }
 70
 71
 12372        foreach ( var source in _sources )
 73        {
 3374            if ( source.Find( code, culture ) is { Length: > 0 } message ) { return message; }
 75        }
 76
 77
 3678        return null;
 79    }
 80
 81    private static string Fill( string template, IReadOnlyDictionary<string, object?>? arguments, CultureInfo culture )
 82    {
 6683        if ( arguments is not { Count: > 0 } ) { return template; }
 84
 85
 686        return Placeholder().Replace( template, match => arguments.TryGetValue( match.Groups[ 1 ].Value, out var value )
 87    }
 88
 89    private static string Format( object? value, CultureInfo culture )
 1290        => value switch
 1291               {
 092                   null              => string.Empty,
 693                   IFormattable text => text.ToString( null, culture ),
 694                   _                 => value.ToString() ?? string.Empty
 1295               };
 96
 97
 98    [ GeneratedRegex( @"\{([A-Za-z_][A-Za-z0-9_]*)\}", RegexOptions.CultureInvariant ) ]
 99    private static partial Regex Placeholder();
 100}