Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
EnumCoreLocalizedStringHelper.cs
1using System.Globalization;
2using System.Resources;
4
6{
11 public static class EnumCoreLocalizedStringHelper
12 {
18 public static string Convert(Enum value)
19 {
20 if (value == null)
21 {
22 return string.Empty;
23 }
24 else
25 {
26 // https://stackoverflow.com/questions/17380900/enum-localization
27 ResourceManager rmCore = new ResourceManager(typeof(Properties.EnumsCore));
28 string resourceDisplayName = rmCore.GetString($"{value.GetType().Name}_{value}");
29 return string.IsNullOrWhiteSpace(resourceDisplayName) ? string.Format("{0}", value) : resourceDisplayName;
30 }
31 }
32
36 private static readonly Dictionary<Type, Dictionary<string, object>> _shortcuts = new Dictionary<Type, Dictionary<string, object>>()
37 {
38 [typeof(Genders)] = new(StringComparer.OrdinalIgnoreCase)
39 {
40 { "m", Genders.Male },
41 { "w", Genders.Female },
42 { "f", Genders.Female }
43 }
44 };
45
53 public static bool TryParse<TEnum>(string input, out TEnum result) where TEnum : struct, Enum
54 {
55 if (string.IsNullOrWhiteSpace(input))
56 {
57 result = default;
58 return false;
59 }
60
61 // Try to parse the input directly as an enum name (case-insensitive)
62 if (Enum.TryParse(input, true, out result))
63 {
64 return true;
65 }
66
67 // Check if the _shortcuts dictionary contains a mapping for the input
68 if (_shortcuts.TryGetValue(typeof(TEnum), out var dict) && dict.TryGetValue(input.Trim(), out object value))
69 {
70 result = (TEnum)value;
71 return true;
72 }
73
74 // Check localized resources
75 ResourceManager rmCore = new ResourceManager(typeof(Properties.EnumsCore));
76 foreach (TEnum enumValue in Enum.GetValues(typeof(TEnum)).Cast<TEnum>())
77 {
78 string enumKey = $"{typeof(TEnum).Name}_{enumValue}";
79 List<string> localizedEnums = GeneralLocalizationHelper.GetAllTranslationsForKey(rmCore, enumKey).Values.ToList();
80
81 foreach (string localizedEnum in localizedEnums)
82 {
83 if (string.Equals(localizedEnum, input, StringComparison.OrdinalIgnoreCase))
84 {
85 result = enumValue;
86 return true;
87 }
88 }
89 }
90
91 result = default;
92 return false;
93 }
94 }
95}
Genders
Available genders for a person.
Definition Genders.cs:7