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
PropertyNameLocalizedStringHelper.cs
1using System;
2using System.Globalization;
3using System.Reflection;
4using System.Resources;
6
8{
13 public static class PropertyNameLocalizedStringHelper
14 {
21 public static string Convert(Type objectType, string propertyName)
22 {
23 if (string.IsNullOrEmpty(propertyName))
24 {
25 return string.Empty;
26 }
27 else
28 {
29 // https://stackoverflow.com/questions/17380900/enum-localization
30 ResourceManager rmPropNames = new ResourceManager(typeof(Properties.PropertyNameLocalizations));
31 string resourceDisplayName = rmPropNames.GetString($"{objectType.Name}_{propertyName}");
32 return string.IsNullOrWhiteSpace(resourceDisplayName) ? propertyName : resourceDisplayName;
33 }
34 }
35
39 private static Dictionary<(Type, string), string> _findPropertyCache = new Dictionary<(Type, string), string>();
40
47 public static string FindProperty(Type objectType, string input)
48 {
49 if (string.IsNullOrWhiteSpace(input))
50 {
51 return string.Empty;
52 }
53 if(_findPropertyCache.ContainsKey((objectType, input)))
54 {
55 return _findPropertyCache[(objectType, input)];
56 }
57
58 ResourceManager rmPropNames = new ResourceManager(typeof(Properties.PropertyNameLocalizations));
59
60 // Search in resources (case-insensitive)
61 foreach (PropertyInfo prop in objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
62 {
63 string resourceKey = $"{objectType.Name}_{prop.Name}";
64 List<string> localizedPropertyNames = GeneralLocalizationHelper.GetAllTranslationsForKey(rmPropNames, resourceKey).Values.ToList();
65
66 foreach (string localizedPropName in localizedPropertyNames)
67 {
68 if (string.Equals(localizedPropName, input, StringComparison.OrdinalIgnoreCase))
69 {
70 _findPropertyCache[(objectType, input)] = prop.Name;
71 return prop.Name;
72 }
73 }
74 }
75
76 // Fallback: direct property name might already match
77 if (objectType.GetProperty(input, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase) is PropertyInfo p)
78 {
79 _findPropertyCache[(objectType, input)] = p.Name;
80 return p.Name;
81 }
82
83 return string.Empty;
84 }
85 }
86}