// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : LocalizedEnumHelper.cs // Author(s) : Natalia Portillo // // Component : GUI helpers. // // --[ Description ] ---------------------------------------------------------- // // Helper class for working with localized enum values. // // --[ License ] -------------------------------------------------------------- // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General public License for more details. // // You should have received a copy of the GNU General public License // along with this program. If not, see . // // ---------------------------------------------------------------------------- // Copyright © 2011-2025 Natalia Portillo // ****************************************************************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using JetBrains.Annotations; namespace Aaru.Gui.Helpers; /// Helper class for working with localized enum values public static class LocalizedEnumHelper { /// Gets the localized description for an enum value /// The enum value /// The localized description, or the enum value's name if no description is found [NotNull] public static string GetLocalizedDescription([NotNull] Enum value) { FieldInfo fieldInfo = value.GetType().GetField(value.ToString()); if(fieldInfo == null) return value.ToString(); var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : value.ToString(); } /// Gets all values of an enum type wrapped with their localized descriptions /// The enum type /// A collection of LocalizedEnumValue wrappers [NotNull] public static IEnumerable> GetLocalizedValues() where T : struct, Enum { return Enum.GetValues().Select(static value => new LocalizedEnumValue(value)); } } /// Wrapper class for an enum value with its localized description /// The enum type public class LocalizedEnumValue where T : struct, Enum { /// Initializes a new instance of the LocalizedEnumValue class /// The enum value public LocalizedEnumValue(T value) { Value = value; Description = LocalizedEnumHelper.GetLocalizedDescription(value); } /// Gets the enum value public T Value { get; } /// Gets the localized description public string Description { get; } /// Returns the localized description as the string representation public override string ToString() => Description; }