| | | 1 | | using System.Collections.Frozen; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using Linguini.Bundle; |
| | | 4 | | using Linguini.Bundle.Builder; |
| | | 5 | | using Linguini.Bundle.Errors; |
| | | 6 | | using TeleFlow.Framework.Application; |
| | | 7 | | |
| | | 8 | | namespace TeleFlow.Telegram.I18n.Fluent.Internal; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Owns the immutable in-memory Fluent catalog loaded from application resources during runtime validation. |
| | | 12 | | /// Formatting performs exact, parent, and fallback selection without file access or resource parsing. |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class FluentCatalog(TelegramFluentI18nOptions options) |
| | | 15 | | { |
| | 24 | 16 | | private readonly object _initializationLock = new(); |
| | | 17 | | private FrozenDictionary<string, LocaleBundles>? _bundles; |
| | | 18 | | |
| | | 19 | | public void Initialize() |
| | | 20 | | { |
| | 26 | 21 | | if (_bundles is not null) |
| | | 22 | | { |
| | 2 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 24 | 26 | | lock (_initializationLock) |
| | | 27 | | { |
| | 24 | 28 | | if (_bundles is not null) |
| | | 29 | | { |
| | 0 | 30 | | return; |
| | | 31 | | } |
| | | 32 | | |
| | 24 | 33 | | _bundles = LoadCatalog(); |
| | 22 | 34 | | } |
| | 22 | 35 | | } |
| | | 36 | | |
| | | 37 | | public ResolvedFluentBundle Resolve(Locale requestedLocale, FluentRenderingMode mode) |
| | | 38 | | { |
| | 812 | 39 | | ArgumentNullException.ThrowIfNull(requestedLocale); |
| | | 40 | | |
| | 812 | 41 | | var bundles = _bundles ?? throw new InvalidOperationException( |
| | 812 | 42 | | "The Fluent catalog has not been initialized. Run TeleFlow runtime validation before formatting messages."); |
| | | 43 | | |
| | 812 | 44 | | for (var culture = requestedLocale.Culture; |
| | 815 | 45 | | !string.IsNullOrEmpty(culture.Name); |
| | 3 | 46 | | culture = culture.Parent) |
| | | 47 | | { |
| | 814 | 48 | | if (bundles.TryGetValue(culture.Name, out var localeBundles)) |
| | | 49 | | { |
| | 811 | 50 | | return new ResolvedFluentBundle(localeBundles.Locale, localeBundles.Get(mode)); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | var fallback = bundles[options.FallbackLocale.Name]; |
| | 1 | 55 | | return new ResolvedFluentBundle(fallback.Locale, fallback.Get(mode)); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private FrozenDictionary<string, LocaleBundles> LoadCatalog() |
| | | 59 | | { |
| | 24 | 60 | | var rootPath = ResolveResourceRoot(options.ResourcesPath); |
| | | 61 | | |
| | 24 | 62 | | if (!Directory.Exists(rootPath)) |
| | | 63 | | { |
| | 0 | 64 | | throw new TeleFlowConfigurationException( |
| | 0 | 65 | | $"Fluent resource directory '{rootPath}' does not exist."); |
| | | 66 | | } |
| | | 67 | | |
| | 24 | 68 | | var catalog = new Dictionary<string, LocaleBundles>(StringComparer.OrdinalIgnoreCase); |
| | 24 | 69 | | var localeDirectories = Directory |
| | 24 | 70 | | .EnumerateDirectories(rootPath) |
| | 24 | 71 | | .Order(StringComparer.Ordinal) |
| | 24 | 72 | | .ToArray(); |
| | | 73 | | |
| | 24 | 74 | | if (localeDirectories.Length == 0) |
| | | 75 | | { |
| | 0 | 76 | | throw new TeleFlowConfigurationException( |
| | 0 | 77 | | $"Fluent resource directory '{rootPath}' does not contain locale directories."); |
| | | 78 | | } |
| | | 79 | | |
| | 101 | 80 | | foreach (var localeDirectory in localeDirectories) |
| | | 81 | | { |
| | 27 | 82 | | var directoryName = Path.GetFileName(localeDirectory); |
| | | 83 | | |
| | 27 | 84 | | if (!Locale.TryCreate(directoryName, out var locale)) |
| | | 85 | | { |
| | 0 | 86 | | throw new TeleFlowConfigurationException( |
| | 0 | 87 | | $"Fluent locale directory '{directoryName}' is not a valid locale name."); |
| | | 88 | | } |
| | | 89 | | |
| | 27 | 90 | | if (catalog.ContainsKey(locale.Name)) |
| | | 91 | | { |
| | 0 | 92 | | throw new TeleFlowConfigurationException( |
| | 0 | 93 | | $"Fluent locale '{locale.Name}' is configured by more than one directory."); |
| | | 94 | | } |
| | | 95 | | |
| | 27 | 96 | | catalog.Add(locale.Name, LoadLocale(locale, localeDirectory)); |
| | | 97 | | } |
| | | 98 | | |
| | 23 | 99 | | if (!catalog.ContainsKey(options.FallbackLocale.Name)) |
| | | 100 | | { |
| | 1 | 101 | | throw new TeleFlowConfigurationException( |
| | 1 | 102 | | $"Fluent fallback locale '{options.FallbackLocale.Name}' does not have a resource catalog."); |
| | | 103 | | } |
| | | 104 | | |
| | 22 | 105 | | return catalog.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static LocaleBundles LoadLocale(Locale locale, string localeDirectory) |
| | | 109 | | { |
| | 27 | 110 | | var resourcePaths = Directory |
| | 27 | 111 | | .EnumerateFiles(localeDirectory, "*.ftl", SearchOption.AllDirectories) |
| | 27 | 112 | | .Order(StringComparer.Ordinal) |
| | 27 | 113 | | .ToArray(); |
| | | 114 | | |
| | 27 | 115 | | if (resourcePaths.Length == 0) |
| | | 116 | | { |
| | 0 | 117 | | throw new TeleFlowConfigurationException( |
| | 0 | 118 | | $"Fluent locale '{locale.Name}' does not contain any .ftl resources."); |
| | | 119 | | } |
| | | 120 | | |
| | 27 | 121 | | var readers = new (TextReader Reader, string? FileName)[resourcePaths.Length]; |
| | | 122 | | |
| | | 123 | | try |
| | | 124 | | { |
| | 110 | 125 | | for (var index = 0; index < resourcePaths.Length; index++) |
| | | 126 | | { |
| | 28 | 127 | | readers[index] = (File.OpenText(resourcePaths[index]), resourcePaths[index]); |
| | | 128 | | } |
| | | 129 | | |
| | 27 | 130 | | var ready = LinguiniBuilder |
| | 27 | 131 | | .Builder() |
| | 27 | 132 | | .CultureInfo(locale.Culture) |
| | 28 | 133 | | .AddResources(readers.Select(static item => (item.Reader, item.FileName))) |
| | 27 | 134 | | .SetUseIsolating(false) |
| | 27 | 135 | | .UseConcurrent(); |
| | 27 | 136 | | var (baseBundle, errors) = ready.Build(); |
| | | 137 | | |
| | 27 | 138 | | if (errors is { Count: > 0 }) |
| | | 139 | | { |
| | 1 | 140 | | throw CreateResourceException(locale, resourcePaths, errors); |
| | | 141 | | } |
| | | 142 | | |
| | 26 | 143 | | return new LocaleBundles( |
| | 26 | 144 | | locale, |
| | 26 | 145 | | CreateBundle(baseBundle, locale.Culture, FluentRenderingMode.Plain), |
| | 26 | 146 | | CreateBundle(baseBundle, locale.Culture, FluentRenderingMode.Html), |
| | 26 | 147 | | CreateBundle(baseBundle, locale.Culture, FluentRenderingMode.MarkdownV2)); |
| | | 148 | | } |
| | 1 | 149 | | catch (TeleFlowConfigurationException) |
| | | 150 | | { |
| | 1 | 151 | | throw; |
| | | 152 | | } |
| | 0 | 153 | | catch (Exception exception) |
| | | 154 | | { |
| | 0 | 155 | | throw new TeleFlowConfigurationException( |
| | 0 | 156 | | $"Fluent resources for locale '{locale.Name}' could not be loaded.", |
| | 0 | 157 | | exception); |
| | | 158 | | } |
| | | 159 | | finally |
| | | 160 | | { |
| | 110 | 161 | | foreach (var (reader, _) in readers) |
| | | 162 | | { |
| | 28 | 163 | | reader?.Dispose(); |
| | | 164 | | } |
| | 27 | 165 | | } |
| | 26 | 166 | | } |
| | | 167 | | |
| | | 168 | | private static FrozenBundle CreateBundle( |
| | | 169 | | FluentBundle baseBundle, |
| | | 170 | | CultureInfo culture, |
| | | 171 | | FluentRenderingMode mode) |
| | | 172 | | { |
| | 78 | 173 | | var bundle = baseBundle.DeepClone(); |
| | 78 | 174 | | bundle.AddFunctionUnchecked("NUMBER", FluentFunctions.CreateNumber(culture, mode)); |
| | 78 | 175 | | bundle.AddFunctionUnchecked("DATETIME", FluentFunctions.CreateDateTime(culture, mode)); |
| | 78 | 176 | | return bundle.ToFrozenBundle(); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | private static TeleFlowConfigurationException CreateResourceException( |
| | | 180 | | Locale locale, |
| | | 181 | | IEnumerable<string> resourcePaths, |
| | | 182 | | IEnumerable<FluentError> errors) |
| | | 183 | | { |
| | 1 | 184 | | return new TeleFlowConfigurationException( |
| | 1 | 185 | | $"Fluent resources for locale '{locale.Name}' are invalid:" + |
| | 1 | 186 | | Environment.NewLine + |
| | 1 | 187 | | string.Join(Environment.NewLine, resourcePaths.Select(static path => $"Resource: {path}")) + |
| | 1 | 188 | | Environment.NewLine + |
| | 2 | 189 | | string.Join(Environment.NewLine, errors.Select(static error => error.ToString()))); |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | private static string ResolveResourceRoot(string configuredPath) |
| | | 193 | | { |
| | 24 | 194 | | return Path.IsPathRooted(configuredPath) |
| | 24 | 195 | | ? Path.GetFullPath(configuredPath) |
| | 24 | 196 | | : Path.GetFullPath(configuredPath, AppContext.BaseDirectory); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Groups the immutable plain-text and Telegram-markup bundles built from one parsed locale catalog. |
| | | 201 | | /// </summary> |
| | | 202 | | internal sealed record LocaleBundles( |
| | | 203 | | Locale Locale, |
| | | 204 | | FrozenBundle Plain, |
| | | 205 | | FrozenBundle Html, |
| | | 206 | | FrozenBundle MarkdownV2) |
| | | 207 | | { |
| | | 208 | | public FrozenBundle Get(FluentRenderingMode mode) |
| | | 209 | | { |
| | 812 | 210 | | return mode switch |
| | 812 | 211 | | { |
| | 805 | 212 | | FluentRenderingMode.Plain => Plain, |
| | 5 | 213 | | FluentRenderingMode.Html => Html, |
| | 2 | 214 | | FluentRenderingMode.MarkdownV2 => MarkdownV2, |
| | 0 | 215 | | _ => throw new ArgumentOutOfRangeException(nameof(mode), mode, "Unknown Fluent rendering mode.") |
| | 812 | 216 | | }; |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Couples the concrete locale selected by catalog fallback with its immutable mode-specific Linguini bundle. |
| | | 223 | | /// </summary> |
| | | 224 | | internal readonly record struct ResolvedFluentBundle(Locale Locale, FrozenBundle Bundle); |