ErrorResourceManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Resources;
  6. namespace Muchinfo.MTPClient.CustomException
  7. {
  8. public class ErrorResourceManager : ResourceManager
  9. {
  10. private const string _defaultFileName = "Muchinfo.ErrorResource.zh-CN.dll";
  11. private const string _defaultCultureName = "zh-CN";
  12. private const string _unKnownResource = "UnKnownResource";
  13. private const string _unfindResource = "UnFindResource";
  14. private static ResourceManager resourceMan;
  15. public static ResourceManager ResourceManager
  16. {
  17. get
  18. {
  19. if (object.ReferenceEquals(resourceMan, null))
  20. {
  21. global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Muchinfo.MTPClient.CustomException.ErrorResources", typeof(ErrorResources).Assembly);
  22. resourceMan = temp;
  23. }
  24. return resourceMan;
  25. }
  26. }
  27. public ErrorResourceManager()
  28. {
  29. ResourceSets = new Hashtable();
  30. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _defaultFileName);
  31. try
  32. {
  33. LoadResourceFiles(filePath, _defaultCultureName);
  34. }
  35. catch (Exception) //读取不到文件使用系统默认的
  36. {
  37. }
  38. }
  39. public ErrorResourceManager(string errorResourceFilePath)
  40. {
  41. ResourceSets = new Hashtable();
  42. try
  43. {
  44. LoadResourceFiles(errorResourceFilePath, _defaultCultureName);
  45. }
  46. catch (Exception) //读取不到文件使用系统默认的
  47. {
  48. }
  49. }
  50. public void LoadResourceFiles(string resourceFile, string cultureName)
  51. {
  52. var reader = new ResourceReader(resourceFile);
  53. var resourceSet = new ResourceSet(reader);
  54. this.ResourceSets[cultureName] = resourceSet;
  55. }
  56. public override string GetString(string name)
  57. {
  58. var culture = new CultureInfo(_defaultCultureName);
  59. return GetString(name, culture);
  60. }
  61. public override string GetString(string name, CultureInfo culture)
  62. {
  63. if (this.ResourceSets != null && this.ResourceSets.Count > 0)
  64. {
  65. if (culture == null)
  66. {
  67. culture = new CultureInfo(_defaultCultureName);
  68. }
  69. if (this.ResourceSets.Contains(culture.Name))
  70. {
  71. var resourceSets = this.ResourceSets[culture.Name] as ResourceSet;
  72. if (resourceSets != null)
  73. {
  74. try
  75. {
  76. return resourceSets.GetString(name);
  77. }
  78. catch
  79. {
  80. return _unfindResource;
  81. }
  82. }
  83. }
  84. }
  85. else
  86. {
  87. return ResourceManager.GetString(name, null);
  88. }
  89. return _unKnownResource;
  90. }
  91. }
  92. public partial class ErrorResources
  93. {
  94. private static bool _resourceLoaded = false;
  95. public static bool ResourceLoaded
  96. {
  97. get
  98. {
  99. return _resourceLoaded;
  100. }
  101. }
  102. public static void Resource(ResourceManager manager)
  103. {
  104. resourceMan = manager;
  105. _resourceLoaded = true;
  106. }
  107. }
  108. }