1using System.Globalization;
2using System.Reflection;
10 public static class GeneralLocalizationHelper
15 private static IEnumerable<CultureInfo> _availableCultures =
null;
20 private static Dictionary<string, Dictionary<string, string>> _allTranslationsCache =
new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
25 static GeneralLocalizationHelper()
27 Assembly mainAssembly = typeof(GeneralLocalizationHelper).Assembly;
28 _availableCultures = getAvailableCultures(mainAssembly);
37 public static Dictionary<string, string> GetAllTranslationsForKey(ResourceManager rm,
string resourceKey)
39 if (_allTranslationsCache.ContainsKey(resourceKey))
41 return _allTranslationsCache[resourceKey];
44 Dictionary<string, string> result =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
45 string neutral = rm.GetString(resourceKey, CultureInfo.InvariantCulture);
46 if (!
string.IsNullOrEmpty(neutral))
48 result[
"neutral"] = neutral;
51 foreach (CultureInfo culture
in _availableCultures)
55 string value = rm.GetString(resourceKey, culture);
56 if (!
string.IsNullOrEmpty(value))
58 result[culture.TwoLetterISOLanguageName] = value;
61 catch (MissingManifestResourceException)
67 _allTranslationsCache[resourceKey] = result;
76 private static IEnumerable<CultureInfo> getAvailableCultures(Assembly assembly)
78 string baseDir = Path.GetDirectoryName(assembly.Location)!;
80 foreach (
string dir
in Directory.GetDirectories(baseDir))
82 string name = Path.GetFileName(dir);
83 CultureInfo? culture =
null;
87 culture = CultureInfo.GetCultureInfo(name);
89 catch (CultureNotFoundException)
96 string satellitePath = Path.Combine(dir, assembly.GetName().Name +
".resources.dll");
97 if (File.Exists(satellitePath))