| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextHumanizer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Threading; |
| | | 11 | | using MyNet.Globalization.Culture; |
| | | 12 | | using MyNet.Humanizer.Display; |
| | | 13 | | using MyNet.Humanizer.Display.Registration; |
| | | 14 | | |
| | | 15 | | namespace MyNet.Humanizer.Facade; |
| | | 16 | | |
| | | 17 | | public static class TextHumanizer |
| | | 18 | | { |
| | 8 | 19 | | private static IDisplayTextService _service = CreateDisplayTextService(); |
| | | 20 | | private static int _configured; |
| | | 21 | | |
| | | 22 | | public static void Configure(IDisplayTextService service) |
| | | 23 | | { |
| | 8 | 24 | | ArgumentNullException.ThrowIfNull(service); |
| | | 25 | | |
| | 8 | 26 | | if (Interlocked.Exchange(ref _configured, 1) == 1) |
| | | 27 | | { |
| | 0 | 28 | | throw new InvalidOperationException("TextHumanizer has already been configured."); |
| | | 29 | | } |
| | | 30 | | |
| | 8 | 31 | | _service = service; |
| | 8 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the display text for a given value of type T, using the appropriate display text strategy registered for th |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="value">The value for which to get the display text.</param> |
| | | 38 | | /// <param name="options">The options to use when generating the display text.</param> |
| | | 39 | | /// <param name="culture">The culture to use for determining the display text.</param> |
| | | 40 | | /// <typeparam name="T">The type of the value.</typeparam> |
| | | 41 | | /// <returns>The display text for the given value.</returns> |
| | | 42 | | public static string Humanize<T>(T value, DisplayTextOptions options, CultureInfo? culture = null) |
| | 104 | 43 | | where T : notnull => _service.GetDisplayText(value, options, culture); |
| | | 44 | | |
| | | 45 | | private static DisplayTextService CreateDisplayTextService() |
| | | 46 | | { |
| | 8 | 47 | | var defaultRegistry = new DisplayTextStrategyRegistry(new Dictionary<Type, IDisplayTextStrategy>()); |
| | 8 | 48 | | return new(new DisplayTextStrategyResolver(defaultRegistry), new ThreadCultureContext()); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |