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
GeneralLocalizationHelper.cs
1using System.Globalization;
2using System.Reflection;
3using System.Resources;
4
6{
10 public static class GeneralLocalizationHelper
11 {
15 private static IEnumerable<CultureInfo> _availableCultures = null;
16
20 private static Dictionary<string, Dictionary<string, string>> _allTranslationsCache = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
21
25 static GeneralLocalizationHelper()
26 {
27 Assembly mainAssembly = typeof(GeneralLocalizationHelper).Assembly;
28 _availableCultures = getAvailableCultures(mainAssembly);
29 }
30
37 public static Dictionary<string, string> GetAllTranslationsForKey(ResourceManager rm, string resourceKey)
38 {
39 if (_allTranslationsCache.ContainsKey(resourceKey))
40 {
41 return _allTranslationsCache[resourceKey];
42 }
43
44 Dictionary<string, string> result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
45 string neutral = rm.GetString(resourceKey, CultureInfo.InvariantCulture);
46 if (!string.IsNullOrEmpty(neutral))
47 {
48 result["neutral"] = neutral;
49 }
50
51 foreach (CultureInfo culture in _availableCultures)
52 {
53 try
54 {
55 string value = rm.GetString(resourceKey, culture);
56 if (!string.IsNullOrEmpty(value))
57 {
58 result[culture.TwoLetterISOLanguageName] = value;
59 }
60 }
61 catch (MissingManifestResourceException)
62 {
63 // Culture has no own resx - ignore
64 }
65 }
66
67 _allTranslationsCache[resourceKey] = result;
68 return result;
69 }
70
76 private static IEnumerable<CultureInfo> getAvailableCultures(Assembly assembly)
77 {
78 string baseDir = Path.GetDirectoryName(assembly.Location)!;
79
80 foreach (string dir in Directory.GetDirectories(baseDir))
81 {
82 string name = Path.GetFileName(dir);
83 CultureInfo? culture = null;
84
85 try
86 {
87 culture = CultureInfo.GetCultureInfo(name);
88 }
89 catch (CultureNotFoundException)
90 {
91 // No valid culture folder
92 }
93
94 if (culture != null)
95 {
96 string satellitePath = Path.Combine(dir, assembly.GetName().Name + ".resources.dll");
97 if (File.Exists(satellitePath))
98 {
99 yield return culture;
100 }
101 }
102 }
103 }
104 }
105}