| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using TeleFlow.Framework.Application; |
| | | 4 | | using TeleFlow.Telegram.I18n.Fluent.Internal; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram.I18n.Fluent; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Registers startup-loaded Project Fluent catalogs, scoped update localization, and explicit-locale formatting. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class TelegramFluentI18nServiceCollectionExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adds Telegram locale resolution and the Linguini-backed Fluent adapter as optional framework services. |
| | | 15 | | /// </summary> |
| | | 16 | | public static IServiceCollection AddTelegramFluentI18n( |
| | | 17 | | this IServiceCollection services, |
| | | 18 | | Action<TelegramFluentI18nOptions>? configure = null) |
| | | 19 | | { |
| | 24 | 20 | | ArgumentNullException.ThrowIfNull(services); |
| | | 21 | | |
| | 600 | 22 | | if (services.Any(static descriptor => descriptor.ServiceType == typeof(TelegramFluentI18nOptions))) |
| | | 23 | | { |
| | 0 | 24 | | throw new InvalidOperationException($"{nameof(AddTelegramFluentI18n)} cannot be called more than once."); |
| | | 25 | | } |
| | | 26 | | |
| | 24 | 27 | | var options = new TelegramFluentI18nOptions(); |
| | 24 | 28 | | configure?.Invoke(options); |
| | | 29 | | |
| | 24 | 30 | | if (string.IsNullOrWhiteSpace(options.ResourcesPath)) |
| | | 31 | | { |
| | 0 | 32 | | throw new InvalidOperationException("Fluent resource path must not be empty."); |
| | | 33 | | } |
| | | 34 | | |
| | 24 | 35 | | if (options.FallbackLocale is null) |
| | | 36 | | { |
| | 0 | 37 | | throw new InvalidOperationException("Fluent fallback locale must be configured."); |
| | | 38 | | } |
| | | 39 | | |
| | 48 | 40 | | services.AddTelegramI18n(i18n => i18n.FallbackLocale = options.FallbackLocale); |
| | 24 | 41 | | services.AddSingleton(options); |
| | 24 | 42 | | services.AddSingleton<FluentCatalog>(); |
| | 24 | 43 | | services.AddSingleton<IFluentTextFormatter, FluentTextFormatter>(); |
| | 24 | 44 | | services.AddScoped<IFluentLocalizer, FluentLocalizer>(); |
| | 24 | 45 | | services.TryAddEnumerable( |
| | 24 | 46 | | ServiceDescriptor.Singleton<ITeleFlowRuntimeValidator, FluentCatalogRuntimeValidator>()); |
| | 24 | 47 | | return services; |
| | | 48 | | } |
| | | 49 | | } |