| | | 1 | | using System.Globalization; |
| | | 2 | | using Linguini.Bundle.Types; |
| | | 3 | | using Linguini.Shared.Types.Bundle; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Telegram.I18n.Fluent.Internal; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Implements TeleFlow's documented NUMBER and DATETIME Fluent functions with locale-aware, mode-safe output. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class FluentFunctions |
| | | 11 | | { |
| | | 12 | | public static ExternalFunction CreateNumber(CultureInfo culture, FluentRenderingMode mode) |
| | | 13 | | { |
| | 78 | 14 | | return (arguments, namedArguments) => |
| | 78 | 15 | | { |
| | 4 | 16 | | if (arguments.Count != 1 || arguments[0] is not FluentNumber number) |
| | 78 | 17 | | { |
| | 0 | 18 | | throw new InvalidOperationException("NUMBER requires exactly one numeric positional argument."); |
| | 78 | 19 | | } |
| | 78 | 20 | | |
| | 4 | 21 | | var minimumFractionDigits = GetIntegerOption(namedArguments, "minimumFractionDigits", 0, 100) ?? 0; |
| | 4 | 22 | | var maximumFractionDigits = GetIntegerOption(namedArguments, "maximumFractionDigits", 0, 100) ?? |
| | 4 | 23 | | Math.Max(minimumFractionDigits, 3); |
| | 78 | 24 | | |
| | 4 | 25 | | if (maximumFractionDigits < minimumFractionDigits) |
| | 78 | 26 | | { |
| | 0 | 27 | | throw new InvalidOperationException( |
| | 0 | 28 | | "NUMBER maximumFractionDigits must be greater than or equal to minimumFractionDigits."); |
| | 78 | 29 | | } |
| | 78 | 30 | | |
| | 4 | 31 | | var useGrouping = GetBooleanOption(namedArguments, "useGrouping") ?? true; |
| | 4 | 32 | | var formatted = FormatNumber( |
| | 4 | 33 | | number.Value, |
| | 4 | 34 | | culture, |
| | 4 | 35 | | useGrouping, |
| | 4 | 36 | | minimumFractionDigits, |
| | 4 | 37 | | maximumFractionDigits); |
| | 78 | 38 | | |
| | 4 | 39 | | return (FluentString)FluentValueConverter.Escape(formatted, mode); |
| | 78 | 40 | | }; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public static ExternalFunction CreateDateTime(CultureInfo culture, FluentRenderingMode mode) |
| | | 44 | | { |
| | 78 | 45 | | return (arguments, namedArguments) => |
| | 78 | 46 | | { |
| | 9 | 47 | | if (arguments.Count != 1 || arguments[0] is not FluentDateTimeValue dateTime) |
| | 78 | 48 | | { |
| | 0 | 49 | | throw new InvalidOperationException( |
| | 0 | 50 | | "DATETIME requires exactly one DateTime, DateTimeOffset, DateOnly, or TimeOnly argument."); |
| | 78 | 51 | | } |
| | 78 | 52 | | |
| | 9 | 53 | | var dateStyle = GetStringOption(namedArguments, "dateStyle"); |
| | 9 | 54 | | var timeStyle = GetStringOption(namedArguments, "timeStyle"); |
| | 9 | 55 | | var formatted = dateTime.Format(culture, dateStyle, timeStyle); |
| | 7 | 56 | | return (FluentString)FluentValueConverter.Escape(formatted, mode); |
| | 78 | 57 | | }; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | private static string FormatNumber( |
| | | 61 | | double value, |
| | | 62 | | CultureInfo culture, |
| | | 63 | | bool useGrouping, |
| | | 64 | | int minimumFractionDigits, |
| | | 65 | | int maximumFractionDigits) |
| | | 66 | | { |
| | 4 | 67 | | var format = (useGrouping ? "N" : "F") + maximumFractionDigits.ToString(CultureInfo.InvariantCulture); |
| | 4 | 68 | | var formatted = value.ToString(format, culture); |
| | | 69 | | |
| | 4 | 70 | | if (maximumFractionDigits == minimumFractionDigits) |
| | | 71 | | { |
| | 2 | 72 | | return formatted; |
| | | 73 | | } |
| | | 74 | | |
| | 2 | 75 | | var separator = culture.NumberFormat.NumberDecimalSeparator; |
| | 2 | 76 | | var separatorIndex = formatted.LastIndexOf(separator, StringComparison.Ordinal); |
| | | 77 | | |
| | 2 | 78 | | if (separatorIndex < 0) |
| | | 79 | | { |
| | 0 | 80 | | return formatted; |
| | | 81 | | } |
| | | 82 | | |
| | 2 | 83 | | var trimEnd = formatted.Length; |
| | 2 | 84 | | var minimumEnd = separatorIndex + separator.Length + minimumFractionDigits; |
| | | 85 | | |
| | 8 | 86 | | while (trimEnd > minimumEnd && formatted[trimEnd - 1] == '0') |
| | | 87 | | { |
| | 6 | 88 | | trimEnd--; |
| | | 89 | | } |
| | | 90 | | |
| | 2 | 91 | | if (trimEnd == separatorIndex + separator.Length) |
| | | 92 | | { |
| | 2 | 93 | | trimEnd = separatorIndex; |
| | | 94 | | } |
| | | 95 | | |
| | 2 | 96 | | return formatted[..trimEnd]; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static int? GetIntegerOption( |
| | | 100 | | IDictionary<string, IFluentType> arguments, |
| | | 101 | | string name, |
| | | 102 | | int minimum, |
| | | 103 | | int maximum) |
| | | 104 | | { |
| | 8 | 105 | | if (!arguments.TryGetValue(name, out var value)) |
| | | 106 | | { |
| | 5 | 107 | | return null; |
| | | 108 | | } |
| | | 109 | | |
| | 3 | 110 | | if (value is not FluentNumber number || |
| | 3 | 111 | | !double.IsInteger(number.Value) || |
| | 3 | 112 | | number.Value < minimum || |
| | 3 | 113 | | number.Value > maximum) |
| | | 114 | | { |
| | 0 | 115 | | throw new InvalidOperationException( |
| | 0 | 116 | | $"Fluent option '{name}' must be an integer from {minimum} through {maximum}."); |
| | | 117 | | } |
| | | 118 | | |
| | 3 | 119 | | return (int)number.Value; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private static bool? GetBooleanOption(IDictionary<string, IFluentType> arguments, string name) |
| | | 123 | | { |
| | 4 | 124 | | var value = GetStringOption(arguments, name); |
| | | 125 | | |
| | 4 | 126 | | if (value is null) |
| | | 127 | | { |
| | 3 | 128 | | return null; |
| | | 129 | | } |
| | | 130 | | |
| | 1 | 131 | | return value.ToUpperInvariant() switch |
| | 1 | 132 | | { |
| | 0 | 133 | | "TRUE" or "1" => true, |
| | 1 | 134 | | "FALSE" or "0" => false, |
| | 0 | 135 | | _ => throw new InvalidOperationException( |
| | 0 | 136 | | $"Fluent option '{name}' must be true, false, 1, or 0.") |
| | 1 | 137 | | }; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private static string? GetStringOption(IDictionary<string, IFluentType> arguments, string name) |
| | | 141 | | { |
| | 22 | 142 | | return arguments.TryGetValue(name, out var value) |
| | 22 | 143 | | ? value.AsString() |
| | 22 | 144 | | : null; |
| | | 145 | | } |
| | | 146 | | } |