MuchInfoResourceManager.cs 4.0 KB

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