ResourceManagerWrapper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #nullable enable
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.Resources;
  5. namespace Terminal.Gui.Resources;
  6. internal class ResourceManagerWrapper (ResourceManager resourceManager)
  7. {
  8. private readonly ResourceManager _resourceManager = resourceManager ?? throw new ArgumentNullException (nameof (resourceManager));
  9. // Optionally, expose other ResourceManager methods as needed
  10. public object GetObject (string name, CultureInfo culture = null!)
  11. {
  12. object value = _resourceManager.GetObject (name, culture)!;
  13. if (Equals (culture, CultureInfo.InvariantCulture))
  14. {
  15. return value;
  16. }
  17. if (value is null)
  18. {
  19. value = _resourceManager.GetObject (name, CultureInfo.InvariantCulture)!;
  20. }
  21. return value;
  22. }
  23. public ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents)
  24. {
  25. ResourceSet value = _resourceManager.GetResourceSet (culture, createIfNotExists, tryParents)!;
  26. if (Equals (culture, CultureInfo.InvariantCulture))
  27. {
  28. return value;
  29. }
  30. if (value!.Cast<DictionaryEntry> ().Any ())
  31. {
  32. value = _resourceManager.GetResourceSet (CultureInfo.InvariantCulture, createIfNotExists, tryParents)!;
  33. }
  34. return value;
  35. }
  36. public ResourceSet? GetResourceSet (CultureInfo culture, bool createIfNotExists, bool tryParents, Func<DictionaryEntry, bool>? filter)
  37. {
  38. ResourceSet value = _resourceManager.GetResourceSet (culture, createIfNotExists, tryParents)!;
  39. IEnumerable<DictionaryEntry> filteredEntries = value.Cast<DictionaryEntry> ().Where (filter ?? (_ => true));
  40. ResourceSet? filteredValue = ConvertToResourceSet (filteredEntries);
  41. if (Equals (culture, CultureInfo.InvariantCulture))
  42. {
  43. return filteredValue;
  44. }
  45. if (!filteredValue!.Cast<DictionaryEntry> ().Any ())
  46. {
  47. filteredValue = GetResourceSet (CultureInfo.InvariantCulture, createIfNotExists, tryParents, filter)!;
  48. }
  49. return filteredValue;
  50. }
  51. public string GetString (string name, CultureInfo? culture = null!)
  52. {
  53. // Attempt to get the string for the specified culture
  54. string value = _resourceManager.GetString (name, culture)!;
  55. // If it's already using the invariant culture return
  56. if (Equals (culture, CultureInfo.InvariantCulture))
  57. {
  58. return value;
  59. }
  60. // If the string is empty or null, fall back to the invariant culture
  61. if (string.IsNullOrEmpty (value))
  62. {
  63. value = _resourceManager.GetString (name, CultureInfo.InvariantCulture)!;
  64. }
  65. return value;
  66. }
  67. private static ResourceSet? ConvertToResourceSet (IEnumerable<DictionaryEntry> entries)
  68. {
  69. using var memoryStream = new MemoryStream ();
  70. using var resourceWriter = new ResourceWriter (memoryStream);
  71. // Add each DictionaryEntry to the ResourceWriter
  72. foreach (DictionaryEntry entry in entries)
  73. {
  74. resourceWriter.AddResource ((string)entry.Key, entry.Value);
  75. }
  76. // Finish writing to the stream
  77. resourceWriter.Generate ();
  78. // Reset the stream position to the beginning
  79. memoryStream.Position = 0;
  80. // Create a ResourceSet from the MemoryStream
  81. var resourceSet = new ResourceSet (memoryStream);
  82. return resourceSet;
  83. }
  84. }