| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DisplayTextStrategyBase.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.ComponentModel.DataAnnotations; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Resources; |
| | | 11 | | using MyNet.Globalization.Localization.Translation; |
| | | 12 | | using MyNet.Globalization.Localization.Translation.KeyGeneration; |
| | | 13 | | using MyNet.Primitives; |
| | | 14 | | |
| | | 15 | | namespace MyNet.Humanizer.Display.Strategies; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Base class for display text providers, implementing the common logic for retrieving display texts from attributes, t |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="translationService">The translator to use for translating resource keys.</param> |
| | | 21 | | /// <param name="keyProvider">The key provider to use for generating translation keys.</param> |
| | | 22 | | /// <typeparam name="T">The type of values for which to provide display texts.</typeparam> |
| | | 23 | | public abstract class DisplayTextStrategyBase<T>(ITranslationService translationService, ITranslationKeyProvider keyProv |
| | | 24 | | where T : notnull |
| | | 25 | | { |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public virtual string GetDisplayText(T value, DisplayTextOptions options, CultureInfo culture) |
| | | 28 | | { |
| | 119 | 29 | | ArgumentNullException.ThrowIfNull(value); |
| | | 30 | | |
| | | 31 | | // 1. Symbol |
| | 116 | 32 | | if (options.Style == DisplayStyle.Symbol) |
| | | 33 | | { |
| | 2 | 34 | | var symbol = ProvideSymbol(value, culture); |
| | | 35 | | |
| | 2 | 36 | | if (!string.IsNullOrWhiteSpace(symbol)) |
| | | 37 | | { |
| | 0 | 38 | | return symbol; |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | // 2. Display name |
| | 116 | 43 | | var displayText = ProvideDisplayText(value, culture); |
| | | 44 | | |
| | 113 | 45 | | if (!string.IsNullOrWhiteSpace(displayText)) |
| | | 46 | | { |
| | 23 | 47 | | return displayText; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // 3. Translation |
| | 90 | 51 | | var resourceKey = BuildResourceKey(value); |
| | 90 | 52 | | var translation = translationService.Translate(resourceKey, BuildTranslationOptions(options), culture); |
| | | 53 | | |
| | 90 | 54 | | return !string.IsNullOrWhiteSpace(translation) |
| | 90 | 55 | | ? translation |
| | 90 | 56 | | : |
| | 90 | 57 | | |
| | 90 | 58 | | // 4. Fallback |
| | 90 | 59 | | ProvideFallback(value, culture).OrEmpty(); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Explicit implementation of the non-generic IDisplayTextStrategy interface method, which casts the input value to |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="value">The value for which to get the display text.</param> |
| | | 66 | | /// <param name="options">The options to use when generating the display text.</param> |
| | | 67 | | /// <param name="culture">The culture to use for determining the display text.</param> |
| | | 68 | | /// <returns>The display text for the given value.</returns> |
| | 104 | 69 | | string IDisplayTextStrategy.GetDisplayText(object value, DisplayTextOptions options, CultureInfo culture) => GetDisp |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Provides the symbol for the specified value and culture, if available. This method should be implemented by deri |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="value">The value for which to provide the symbol.</param> |
| | | 75 | | /// <param name="culture">The culture to use for symbol retrieval.</param> |
| | | 76 | | /// <returns>The symbol for the specified value and culture, or null/empty if not defined.</returns> |
| | | 77 | | protected abstract string? ProvideSymbol(T value, CultureInfo culture); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Provides the display name for the specified value and culture, if available. This method should be implemented b |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="value">The value for which to provide the display name.</param> |
| | | 83 | | /// <param name="culture">The culture to use for display name retrieval.</param> |
| | | 84 | | /// <returns>The display name for the specified value and culture, or null/empty if not defined.</returns> |
| | | 85 | | protected abstract string? ProvideDisplayText(T value, CultureInfo culture); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Provides a fallback display name for the specified value and culture, if available. This method can be implement |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="value">The value for which to provide the fallback display name.</param> |
| | | 91 | | /// <param name="culture">The culture to use for fallback display name retrieval.</param> |
| | | 92 | | /// <returns>The fallback display name for the specified value and culture, or null/empty if not defined.</returns> |
| | 51 | 93 | | protected virtual string? ProvideFallback(T value, CultureInfo culture) => value.ToString(); |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Resolves the display name from the specified DisplayAttribute, using the ResourceType if specified, or falling b |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="displayAttribute">The DisplayAttribute to resolve.</param> |
| | | 99 | | /// <param name="culture">The culture to use for resource lookup.</param> |
| | | 100 | | /// <returns>The resolved display name.</returns> |
| | | 101 | | protected virtual string ResolveDisplayAttribute(DisplayAttribute displayAttribute, CultureInfo culture) |
| | | 102 | | { |
| | | 103 | | // 1.1 Display attribute with ResourceType specified |
| | 18 | 104 | | if (displayAttribute.ResourceType is not null) |
| | | 105 | | { |
| | | 106 | | // 1.1.1 Display attribute with ResourceType specified (Name) |
| | | 107 | | // 1.1.2 Display attribute with ResourceType specified (Description) |
| | 3 | 108 | | return new ResourceManager(displayAttribute.ResourceType).GetString(displayAttribute.Name ?? displayAttribut |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | // 1.2 Display attribute with ResourceType specified (Name) |
| | 15 | 112 | | if (!string.IsNullOrWhiteSpace(displayAttribute.Name)) |
| | | 113 | | { |
| | 3 | 114 | | return displayAttribute.Name; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // 1.3 Display attribute without ResourceType specified (Description) |
| | 12 | 118 | | return !string.IsNullOrWhiteSpace(displayAttribute.Description) ? displayAttribute.Description : string.Empty; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Builds the resource key for the specified enum value, following the convention of "{EnumType}{EnumStringValue}". |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="value">The enum value for which to build the resource key.</param> |
| | | 125 | | /// <returns>The resource key for the specified enum value.</returns> |
| | | 126 | | protected virtual string BuildResourceKey(T value) |
| | | 127 | | { |
| | 90 | 128 | | ArgumentNullException.ThrowIfNull(value); |
| | | 129 | | |
| | 90 | 130 | | return keyProvider.GetKey(value); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Builds the translation options based on the specified display name options, including style and quantity informa |
| | | 135 | | /// </summary> |
| | | 136 | | /// <param name="options">The display name options to use for building the translation options.</param> |
| | | 137 | | /// <returns>The built translation options.</returns> |
| | 90 | 138 | | protected virtual TranslationOptions BuildTranslationOptions(DisplayTextOptions options) => new TranslationOptionsBu |
| | | 139 | | } |
| | | 140 | | |