ResourceManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //------------------------------------------------------------
  2. // All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd.
  3. //------------------------------------------------------------
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. namespace Ant.Service.Utilities
  9. {
  10. /// <summary>
  11. /// ResourceManager
  12. /// 资源管理器
  13. ///
  14. /// 修改纪录
  15. /// 2007.05.16 版本:1.0 JiRiGaLa 重新调整代码的规范化。
  16. ///
  17. /// 版本:1.0
  18. ///
  19. /// <author>
  20. /// <name>JiRiGaLa</name>
  21. /// <date>2007.05.16</date>
  22. /// </author>
  23. /// </summary>
  24. public class ResourceManager
  25. {
  26. private volatile static ResourceManager instance = null;
  27. private static object locker = new Object();
  28. public static ResourceManager Instance
  29. {
  30. get
  31. {
  32. if (instance == null)
  33. {
  34. lock (locker)
  35. {
  36. if (instance == null)
  37. {
  38. instance = new ResourceManager();
  39. }
  40. }
  41. }
  42. return instance;
  43. }
  44. }
  45. private string FolderPath = string.Empty;
  46. public SortedList<String, Resources> LanguageResources = new SortedList<String, Resources>();
  47. public void Serialize(Resources resources, string filePath)
  48. {
  49. ResourcesSerializer.Serialize(filePath, resources);
  50. }
  51. public void Init(string filePath)
  52. {
  53. FolderPath = filePath;
  54. DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
  55. LanguageResources.Clear();
  56. if (!directoryInfo.Exists)
  57. {
  58. return;
  59. }
  60. FileInfo[] FileInfo = directoryInfo.GetFiles();
  61. for (int i = 0; i < FileInfo.Length; i++)
  62. {
  63. Resources resources = ResourcesSerializer.DeSerialize(FileInfo[i].FullName);
  64. resources.createIndex();
  65. LanguageResources.Add(resources.language, resources);
  66. }
  67. }
  68. public Hashtable GetLanguages()
  69. {
  70. Hashtable hashtable = new Hashtable();
  71. IEnumerator<KeyValuePair<String, Resources>> iEnumerator = LanguageResources.GetEnumerator();
  72. while (iEnumerator.MoveNext())
  73. {
  74. hashtable.Add(iEnumerator.Current.Key, iEnumerator.Current.Value.displayName);
  75. }
  76. return hashtable;
  77. }
  78. public Hashtable GetLanguages(string path)
  79. {
  80. Hashtable hashtable = new Hashtable();
  81. DirectoryInfo directoryInfo = new DirectoryInfo(path);
  82. if (!directoryInfo.Exists)
  83. {
  84. return hashtable;
  85. }
  86. FileInfo[] fileInfo = directoryInfo.GetFiles();
  87. for (int i = 0; i < fileInfo.Length; i++)
  88. {
  89. Resources resources = ResourcesSerializer.DeSerialize(fileInfo[i].FullName);
  90. hashtable.Add(resources.language, resources.displayName);
  91. }
  92. return hashtable;
  93. }
  94. public Resources GetResources(string filePath)
  95. {
  96. Resources resources = new Resources();
  97. if (File.Exists(filePath))
  98. {
  99. resources = ResourcesSerializer.DeSerialize(filePath);
  100. resources.createIndex();
  101. }
  102. return resources;
  103. }
  104. public string Get(string language, string key)
  105. {
  106. if (!LanguageResources.ContainsKey(language))
  107. {
  108. return string.Empty;
  109. }
  110. return LanguageResources[language].Get(key);
  111. }
  112. }
  113. }