| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ServiceCollectionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Linq; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 11 | | using MyNet.Globalization; |
| | | 12 | | using MyNet.Globalization.Culture; |
| | | 13 | | using MyNet.Globalization.Localization.Providers; |
| | | 14 | | using MyNet.Globalization.Localization.Translation; |
| | | 15 | | using MyNet.Humanizer.Display; |
| | | 16 | | using MyNet.Humanizer.Display.Registration; |
| | | 17 | | using MyNet.Humanizer.Display.Strategies; |
| | | 18 | | using MyNet.Humanizer.Facade; |
| | | 19 | | using MyNet.Humanizer.Formatting.Addresses; |
| | | 20 | | using MyNet.Humanizer.Formatting.Addresses.Cultures; |
| | | 21 | | using MyNet.Humanizer.Formatting.Collections; |
| | | 22 | | using MyNet.Humanizer.Ordinalizing; |
| | | 23 | | using MyNet.Humanizer.Ordinalizing.Cultures; |
| | | 24 | | using MyNet.Humanizer.Resources; |
| | | 25 | | using MyNet.Humanizer.Temporal; |
| | | 26 | | using MyNet.Primitives; |
| | | 27 | | |
| | | 28 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 29 | | namespace MyNet.Humanizer; |
| | | 30 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Dependency injection extensions for registering humanizer providers. |
| | | 34 | | /// Call this after AddLocalization(). |
| | | 35 | | /// </summary> |
| | | 36 | | public static class ServiceCollectionExtensions |
| | | 37 | | { |
| | | 38 | | extension(IServiceCollection services) |
| | | 39 | | { |
| | | 40 | | /// <summary> |
| | | 41 | | /// Adds humanizer services to the service collection. |
| | | 42 | | /// This method registers humanizer resources and providers into the localization system. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="configure">An action to configure the time localization options.</param> |
| | | 45 | | /// <returns>The updated service collection.</returns> |
| | | 46 | | public IServiceCollection AddHumanizer(Action<TimeLocalizationOptions>? configure = null) |
| | | 47 | | { |
| | 14 | 48 | | ArgumentNullException.ThrowIfNull(services); |
| | | 49 | | |
| | 14 | 50 | | var options = new TimeLocalizationOptions(); |
| | 14 | 51 | | configure?.Invoke(options); |
| | | 52 | | |
| | | 53 | | // Ensure Localization infrastructure is loaded before registering humanizer services, as some providers dep |
| | 14 | 54 | | services.AddLocalization(); |
| | | 55 | | |
| | | 56 | | // List formatters |
| | 14 | 57 | | services.AddLocalizationService<IListFormatter>((sp, culture) => new ListFormatter(sp.GetRequiredService<ITr |
| | | 58 | | |
| | | 59 | | // DateTime providers |
| | | 60 | | // Uses LocalizationServiceContext to resolve IListFormatter for the same culture without repeating it. |
| | 14 | 61 | | services.AddLocalizationService<ITimeHumanizer>((sp, culture) => new TimeHumanizer( |
| | 14 | 62 | | sp.GetRequiredService<ITranslationService>(), |
| | 14 | 63 | | sp.GetRequiredService<ICultureScopedServiceSource<IListFormatter>>(), |
| | 14 | 64 | | culture, |
| | 14 | 65 | | options)); |
| | | 66 | | |
| | | 67 | | // Ordinalizers |
| | 14 | 68 | | services.AddLocalizationService<IOrdinalizer>((_, _) => Ordinalizers.Invariant); |
| | 14 | 69 | | services.ConfigureLocalizationService<IOrdinalizer>(builder => builder |
| | 14 | 70 | | .RegisterCulture(SupportedCultures.English, () => Ordinalizers.English) |
| | 14 | 71 | | .RegisterCulture(SupportedCultures.French, () => Ordinalizers.French)); |
| | | 72 | | |
| | | 73 | | // Address formatters |
| | 14 | 74 | | services.AddLocalizationService<IAddressFormatter>((_, _) => AddressFormatters.Invariant); |
| | 14 | 75 | | services.ConfigureLocalizationService<IAddressFormatter>(builder => builder |
| | 14 | 76 | | .RegisterCulture(SupportedCultures.English, () => AddressFormatters.English) |
| | 14 | 77 | | .RegisterCulture(SupportedCultures.French, () => AddressFormatters.French)); |
| | | 78 | | |
| | | 79 | | // Resources |
| | 14 | 80 | | services.AddTranslationResource(nameof(DateTimeResources), DateTimeResources.ResourceManager); |
| | 14 | 81 | | services.AddTranslationResource(nameof(EnumResources), EnumResources.ResourceManager); |
| | 14 | 82 | | services.AddTranslationResource(nameof(ListResources), ListResources.ResourceManager); |
| | | 83 | | |
| | | 84 | | // DisplayText providers |
| | 14 | 85 | | services.AddDisplayTextInfrastructure(); |
| | 14 | 86 | | services.AddDisplayTextStrategy<Enum, EnumDisplayTextStrategy>(); |
| | 14 | 87 | | services.AddDisplayTextStrategy<ISmartEnum, SmartEnumDisplayTextStrategy>(); |
| | | 88 | | |
| | 14 | 89 | | return services; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Adds a display text strategy for the specified type and strategy implementation. This method registers the s |
| | | 94 | | /// </summary> |
| | | 95 | | /// <typeparam name="T">The target type for which the display text strategy is registered.</typeparam> |
| | | 96 | | /// <typeparam name="TStrategy">The display text strategy implementation.</typeparam> |
| | | 97 | | /// <returns>The updated service collection.</returns> |
| | | 98 | | public IServiceCollection AddDisplayTextStrategy<T, TStrategy>() |
| | | 99 | | where T : notnull |
| | | 100 | | where TStrategy : class, IDisplayTextStrategy<T> |
| | | 101 | | { |
| | 58 | 102 | | services.AddLocalization(); |
| | 58 | 103 | | services.AddDisplayTextInfrastructure(); |
| | | 104 | | |
| | 58 | 105 | | services.TryAddSingleton<TStrategy>(); |
| | | 106 | | |
| | | 107 | | // Register the non-generic marker so the registry can discover all factories. |
| | | 108 | | // We use AddSingleton (not TryAddEnumerable) because TryAddEnumerable requires |
| | | 109 | | // a concrete implementation type for deduplication, which factory-delegate descriptors lack. |
| | 58 | 110 | | services.TryAddSingleton<IDisplayTextStrategy<T>, TStrategy>(); |
| | 58 | 111 | | services.AddSingleton<IDisplayTextStrategyRegistration>(sp => new DisplayTextStrategyRegistration<T>(sp.GetR |
| | | 112 | | |
| | 58 | 113 | | return services; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Adds the necessary infrastructure for display text providers, including the registry and resolver. |
| | | 118 | | /// </summary> |
| | | 119 | | private void AddDisplayTextInfrastructure() |
| | | 120 | | { |
| | 72 | 121 | | services.TryAddSingleton<IDisplayTextStrategyRegistry>(sp => |
| | 72 | 122 | | { |
| | 72 | 123 | | var registrations = sp.GetServices<IDisplayTextStrategyRegistration>(); |
| | 72 | 124 | | |
| | 72 | 125 | | var dictionary = registrations.ToDictionary(x => x.TargetType, x => x.Strategy); |
| | 72 | 126 | | |
| | 72 | 127 | | return new DisplayTextStrategyRegistry(dictionary); |
| | 72 | 128 | | }); |
| | 72 | 129 | | services.TryAddSingleton<IDisplayTextStrategyResolver, DisplayTextStrategyResolver>(); |
| | 72 | 130 | | services.TryAddSingleton<IDisplayTextService, DisplayTextService>(); |
| | 72 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | extension(IServiceProvider serviceProvider) |
| | | 135 | | { |
| | | 136 | | /// <summary> |
| | | 137 | | /// Configures the static Humanizer class to use the IDisplayTextService from the service provider. This method |
| | | 138 | | /// </summary> |
| | | 139 | | /// <returns>The updated service provider.</returns> |
| | | 140 | | public IServiceProvider UseDisplayText() |
| | | 141 | | { |
| | 8 | 142 | | TextHumanizer.Configure(serviceProvider.GetRequiredService<IDisplayTextService>()); |
| | | 143 | | |
| | 8 | 144 | | return serviceProvider; |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | |