| | | 1 | | using System.Globalization; |
| | | 2 | | using Linguini.Bundle; |
| | | 3 | | using Linguini.Shared.Types.Bundle; |
| | | 4 | | using TeleFlow.Telegram.Formatting; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram.I18n.Fluent.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Formats messages from immutable startup-loaded catalogs and enforces argument escaping and failure semantics. |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class FluentTextFormatter(FluentCatalog catalog) : IFluentTextFormatter |
| | | 12 | | { |
| | | 13 | | public string Format( |
| | | 14 | | Locale locale, |
| | | 15 | | string messageId, |
| | | 16 | | params ReadOnlySpan<I18nArgument> arguments) |
| | | 17 | | { |
| | 805 | 18 | | return FormatCore(locale, messageId, arguments, FluentRenderingMode.Plain); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public TelegramFormattedText FormatHtml( |
| | | 22 | | Locale locale, |
| | | 23 | | string messageId, |
| | | 24 | | params ReadOnlySpan<I18nArgument> arguments) |
| | | 25 | | { |
| | 5 | 26 | | var text = FormatCore(locale, messageId, arguments, FluentRenderingMode.Html); |
| | | 27 | | |
| | | 28 | | try |
| | | 29 | | { |
| | 4 | 30 | | return TelegramHtml.UnsafeMarkup(text); |
| | | 31 | | } |
| | 0 | 32 | | catch (ArgumentException exception) |
| | | 33 | | { |
| | 0 | 34 | | throw new FluentLocalizationException( |
| | 0 | 35 | | messageId, |
| | 0 | 36 | | locale, |
| | 0 | 37 | | "the formatted HTML message was empty", |
| | 0 | 38 | | exception); |
| | | 39 | | } |
| | 4 | 40 | | } |
| | | 41 | | |
| | | 42 | | public TelegramFormattedText FormatMarkdownV2( |
| | | 43 | | Locale locale, |
| | | 44 | | string messageId, |
| | | 45 | | params ReadOnlySpan<I18nArgument> arguments) |
| | | 46 | | { |
| | 2 | 47 | | var text = FormatCore(locale, messageId, arguments, FluentRenderingMode.MarkdownV2); |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 2 | 51 | | return TelegramMarkdownV2.UnsafeMarkup(text); |
| | | 52 | | } |
| | 0 | 53 | | catch (ArgumentException exception) |
| | | 54 | | { |
| | 0 | 55 | | throw new FluentLocalizationException( |
| | 0 | 56 | | messageId, |
| | 0 | 57 | | locale, |
| | 0 | 58 | | "the formatted MarkdownV2 message was empty", |
| | 0 | 59 | | exception); |
| | | 60 | | } |
| | 2 | 61 | | } |
| | | 62 | | |
| | | 63 | | private string FormatCore( |
| | | 64 | | Locale locale, |
| | | 65 | | string messageId, |
| | | 66 | | ReadOnlySpan<I18nArgument> arguments, |
| | | 67 | | FluentRenderingMode mode) |
| | | 68 | | { |
| | 812 | 69 | | ArgumentNullException.ThrowIfNull(locale); |
| | 812 | 70 | | ArgumentException.ThrowIfNullOrWhiteSpace(messageId); |
| | | 71 | | |
| | 812 | 72 | | var (message, attribute) = ParseMessageId(messageId, locale); |
| | 812 | 73 | | var resolved = catalog.Resolve(locale, mode); |
| | | 74 | | |
| | | 75 | | try |
| | | 76 | | { |
| | 812 | 77 | | var fluentArguments = ConvertArguments( |
| | 812 | 78 | | arguments, |
| | 812 | 79 | | resolved.Locale.Culture, |
| | 812 | 80 | | mode, |
| | 812 | 81 | | messageId, |
| | 812 | 82 | | locale); |
| | | 83 | | |
| | 811 | 84 | | IReadBundle bundle = resolved.Bundle; |
| | | 85 | | |
| | 811 | 86 | | if (!bundle.TryGetMessage( |
| | 811 | 87 | | message, |
| | 811 | 88 | | attribute, |
| | 811 | 89 | | fluentArguments, |
| | 811 | 90 | | out var errors, |
| | 811 | 91 | | out var formatted) || |
| | 811 | 92 | | formatted is null) |
| | | 93 | | { |
| | 3 | 94 | | throw new FluentLocalizationException( |
| | 3 | 95 | | messageId, |
| | 3 | 96 | | locale, |
| | 3 | 97 | | errors is { Count: > 0 } |
| | 3 | 98 | | ? "the message, attribute, variable, or function could not be resolved" |
| | 3 | 99 | | : "the message or attribute was not found"); |
| | | 100 | | } |
| | | 101 | | |
| | 806 | 102 | | return formatted; |
| | | 103 | | } |
| | 3 | 104 | | catch (FluentLocalizationException) |
| | | 105 | | { |
| | 3 | 106 | | throw; |
| | | 107 | | } |
| | 3 | 108 | | catch (Exception exception) |
| | | 109 | | { |
| | 3 | 110 | | throw new FluentLocalizationException( |
| | 3 | 111 | | messageId, |
| | 3 | 112 | | locale, |
| | 3 | 113 | | "an argument or Fluent function could not be formatted", |
| | 3 | 114 | | exception); |
| | | 115 | | } |
| | 806 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static Dictionary<string, IFluentType>? ConvertArguments( |
| | | 119 | | ReadOnlySpan<I18nArgument> arguments, |
| | | 120 | | CultureInfo culture, |
| | | 121 | | FluentRenderingMode mode, |
| | | 122 | | string messageId, |
| | | 123 | | Locale requestedLocale) |
| | | 124 | | { |
| | 812 | 125 | | if (arguments.IsEmpty) |
| | | 126 | | { |
| | 297 | 127 | | return null; |
| | | 128 | | } |
| | | 129 | | |
| | 515 | 130 | | var result = new Dictionary<string, IFluentType>(arguments.Length, StringComparer.Ordinal); |
| | | 131 | | |
| | 2061 | 132 | | foreach (var argument in arguments) |
| | | 133 | | { |
| | 516 | 134 | | if (!result.TryAdd( |
| | 516 | 135 | | argument.Name, |
| | 516 | 136 | | FluentValueConverter.Convert(argument.Value, culture, mode))) |
| | | 137 | | { |
| | 0 | 138 | | throw new FluentLocalizationException( |
| | 0 | 139 | | messageId, |
| | 0 | 140 | | requestedLocale, |
| | 0 | 141 | | $"argument '{argument.Name}' was provided more than once"); |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 514 | 145 | | return result; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private static (string Message, string? Attribute) ParseMessageId( |
| | | 149 | | string messageId, |
| | | 150 | | Locale locale) |
| | | 151 | | { |
| | 812 | 152 | | var firstDot = messageId.IndexOf('.'); |
| | | 153 | | |
| | 812 | 154 | | if (firstDot < 0) |
| | | 155 | | { |
| | 810 | 156 | | return (messageId, null); |
| | | 157 | | } |
| | | 158 | | |
| | 2 | 159 | | if (firstDot == 0 || |
| | 2 | 160 | | firstDot == messageId.Length - 1 || |
| | 2 | 161 | | messageId.IndexOf('.', firstDot + 1) >= 0) |
| | | 162 | | { |
| | 0 | 163 | | throw new FluentLocalizationException( |
| | 0 | 164 | | messageId, |
| | 0 | 165 | | locale, |
| | 0 | 166 | | "message identifiers must use 'message-id' or 'message-id.attribute'"); |
| | | 167 | | } |
| | | 168 | | |
| | 2 | 169 | | return (messageId[..firstDot], messageId[(firstDot + 1)..]); |
| | | 170 | | } |
| | | 171 | | } |