| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="LocalizedEnum.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using MyNet.Humanizer; |
| | | 10 | | using MyNet.Humanizer.Facade; |
| | | 11 | | using MyNet.Observable.Behaviors.Metadata.Features.Events; |
| | | 12 | | using MyNet.Observable.Metadata; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace MyNet.Observable; |
| | | 16 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Culture-bound display text for a <see cref="Enum"/> value. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="enumValue">The enum value to present.</param> |
| | | 22 | | [SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "LocalizedEnum is more |
| | 42 | 23 | | public class LocalizedEnum(Enum enumValue) : LocalizedEnum<Enum>(enumValue); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Culture-bound display text for an enum value. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <typeparam name="TEnum">The enum type.</typeparam> |
| | | 29 | | /// <param name="enumValue">The enum value to present.</param> |
| | | 30 | | [ExemptFromGeneratedMetadata] |
| | | 31 | | [SuppressMessage("Naming", "CA1711:Identifiers should not have incorrect suffix", Justification = "LocalizedEnum is more |
| | | 32 | | public class LocalizedEnum<TEnum>(TEnum enumValue) : CultureBoundValue<TEnum>(() => enumValue) |
| | | 33 | | where TEnum : Enum |
| | | 34 | | { |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the description from <see cref="System.ComponentModel.DescriptionAttribute"/> when present. |
| | | 37 | | /// </summary> |
| | | 38 | | public string Description => Value is null ? string.Empty : Value.GetDescription() ?? string.Empty; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the localized display text for the enum value. |
| | | 42 | | /// </summary> |
| | | 43 | | public string Display => Value?.Humanize() ?? string.Empty; |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public override string ToString() => Display; |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public override void OnEvent(CultureChangedEvent e) |
| | | 50 | | { |
| | | 51 | | base.OnEvent(e); |
| | | 52 | | NotifyPropertyChanged(nameof(Display)); |
| | | 53 | | NotifyPropertyChanged(nameof(Description)); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public override bool Equals(object? obj) => obj is LocalizedEnum<TEnum> result && (result.Value?.Equals(Value) ?? fa |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public override int GetHashCode() => Value?.GetHashCode() ?? 0; |
| | | 61 | | } |
| | | 62 | | |