| | | 1 | | #region Usings |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | #endregion |
| | | 5 | | |
| | | 6 | | namespace 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> |
| | | 18 | | public 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> |
| | 33 | 31 | | public ErrorLocalizer( IEnumerable<IErrorMessageSource> sources ) |
| | | 32 | | { |
| | 33 | 33 | | ArgumentNullException.ThrowIfNull( sources ); |
| | | 34 | | |
| | 33 | 35 | | _sources = [ ..sources ]; |
| | 33 | 36 | | } |
| | | 37 | | |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 3 | 40 | | public LocalizedMessage Localize( Error error ) => Localize( error, CultureInfo.CurrentUICulture ); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public LocalizedMessage Localize( Error error, CultureInfo culture ) |
| | | 44 | | { |
| | 36 | 45 | | ArgumentNullException.ThrowIfNull( error ); |
| | 36 | 46 | | ArgumentNullException.ThrowIfNull( culture ); |
| | | 47 | | |
| | 102 | 48 | | for ( var current = culture; !Equals( current, CultureInfo.InvariantCulture ); current = current.Parent ) |
| | | 49 | | { |
| | 51 | 50 | | if ( Find( error.Code, current ) is { } template ) { return new LocalizedMessage( current.Name, Fill( templa |
| | | 51 | | |
| | | 52 | | |
| | 27 | 53 | | if ( string.Equals( current.Name, ArabicLocale, StringComparison.OrdinalIgnoreCase ) && !string.IsNullOrEmpt |
| | | 54 | | { |
| | 12 | 55 | | return new LocalizedMessage( ArabicLocale, Fill( error.MessageAR, error.Arguments, culture ) ); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | |
| | 12 | 60 | | var neutral = Find( error.Code, CultureInfo.InvariantCulture ) ?? error.Message; |
| | | 61 | | |
| | | 62 | | |
| | 12 | 63 | | return new LocalizedMessage( NeutralLocale, Fill( neutral, error.Arguments, culture ) ); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | |
| | | 67 | | private string? Find( string code, CultureInfo culture ) |
| | | 68 | | { |
| | 51 | 69 | | if ( string.IsNullOrEmpty( code ) ) { return null; } |
| | | 70 | | |
| | | 71 | | |
| | 123 | 72 | | foreach ( var source in _sources ) |
| | | 73 | | { |
| | 33 | 74 | | if ( source.Find( code, culture ) is { Length: > 0 } message ) { return message; } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | |
| | 36 | 78 | | return null; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static string Fill( string template, IReadOnlyDictionary<string, object?>? arguments, CultureInfo culture ) |
| | | 82 | | { |
| | 66 | 83 | | if ( arguments is not { Count: > 0 } ) { return template; } |
| | | 84 | | |
| | | 85 | | |
| | 6 | 86 | | 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 ) |
| | 12 | 90 | | => value switch |
| | 12 | 91 | | { |
| | 0 | 92 | | null => string.Empty, |
| | 6 | 93 | | IFormattable text => text.ToString( null, culture ), |
| | 6 | 94 | | _ => value.ToString() ?? string.Empty |
| | 12 | 95 | | }; |
| | | 96 | | |
| | | 97 | | |
| | | 98 | | [ GeneratedRegex( @"\{([A-Za-z_][A-Za-z0-9_]*)\}", RegexOptions.CultureInvariant ) ] |
| | | 99 | | private static partial Regex Placeholder(); |
| | | 100 | | } |