APLangIndexer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using MuchInfo.Localization.Languages;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Resources;
  8. using System.Windows;
  9. namespace MuchInfo.Localization
  10. {
  11. /// <summary>
  12. /// APLangIndexer,语言资源索引器。
  13. /// </summary>
  14. public class APLangIndexer : INotifyPropertyChanged
  15. {
  16. static APLangIndexer()
  17. {
  18. //TODO:初始化语言资源
  19. var Rlt = new List<APLangType>();
  20. Rlt.Add(new APLangType()
  21. {
  22. LangTypeAbbre = "zh_CN",
  23. Title = "简体中文",
  24. ResxClass = typeof(zh_CN),
  25. ResxManger = zh_CN.ResourceManager,
  26. ExtLangModelList = new List<ExtLangModel>()
  27. });
  28. Rlt.Add(new APLangType()
  29. {
  30. LangTypeAbbre = "en-US",
  31. Title = "English",
  32. ResxClass = typeof(en_US),
  33. ResxManger = en_US.ResourceManager,
  34. ExtLangModelList = new List<ExtLangModel>()
  35. });
  36. Rlt.Add(new APLangType()
  37. {
  38. LangTypeAbbre = "zh_TW",
  39. Title = "繁體中文",
  40. ResxClass = typeof(zh_TW),
  41. ResxManger = zh_TW.ResourceManager,
  42. ExtLangModelList = new List<ExtLangModel>()
  43. });
  44. _LangTypeList = Rlt;
  45. }
  46. /// <summary>
  47. /// 默认的语言列表。
  48. /// </summary>
  49. /// <returns></returns>
  50. public static List<APLangType> LangTypeList { get { return _LangTypeList; } }
  51. private static List<APLangType> _LangTypeList = null;
  52. /// <summary>
  53. /// LangType,当前语言类型。
  54. /// </summary>
  55. public APLangType LangType
  56. {
  57. get { return _LangType; }
  58. set
  59. {
  60. _LangType = value;
  61. RaisePropertyChanged("APLangIndexer");
  62. RaisePropertyChanged(string.Empty);
  63. }
  64. }
  65. private APLangType _LangType = LangTypeList[0];
  66. /// <summary>
  67. /// 语言资源索引。
  68. /// </summary>
  69. /// <param name="ResKey"></param>
  70. /// <returns></returns>
  71. public string this[string ResKey]
  72. {
  73. get
  74. {
  75. var Rlt = "";
  76. var NullRlt = "APConfig_Error";
  77. //TODO:1、现在拓展语言资源找此关键字的语言资源
  78. if (string.IsNullOrEmpty(Rlt))
  79. {
  80. if (this.LangType.ExtLangModelList != null && this.LangType.ExtLangModelList.Where(x => x.Key == ResKey).Count() > 0)
  81. {
  82. Rlt = this.LangType.ExtLangModelList.First(x => x.Key == ResKey).Value;
  83. }
  84. }
  85. //TODO:如果没有找到,则在资源文件查找
  86. if (string.IsNullOrEmpty(Rlt))
  87. {
  88. var BdFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase;
  89. var Props = this.LangType.ResxClass.GetProperties(BdFlag);
  90. var Prop = Props.First(x => x.Name.Trim().ToUpper() == ResKey.Trim().ToUpper());
  91. if (Prop != null)
  92. {
  93. try
  94. {
  95. Rlt = Prop.GetValue(null, null) + "";
  96. }
  97. catch { ;}
  98. }
  99. }
  100. return Rlt == "" ? this[NullRlt] : Rlt;
  101. }
  102. }
  103. /// <summary>
  104. /// PropertyChanged,属性值发生变化事件。
  105. /// </summary>
  106. public event PropertyChangedEventHandler PropertyChanged;
  107. /// <summary>
  108. /// 引发属性值发生变化事件
  109. /// </summary>
  110. /// <param name="PropertyName"></param>
  111. public void RaisePropertyChanged(string PropertyName)
  112. {
  113. if (PropertyChanged != null)
  114. {
  115. PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// APLangType,语言类型定义。
  121. /// </summary>
  122. public class APLangType : DependencyObject
  123. {
  124. /// <summary>
  125. /// 语言类型简称
  126. /// </summary>
  127. public string LangTypeAbbre { get; set; }
  128. /// <summary>
  129. /// 语言标题
  130. /// </summary>
  131. public string Title { get; set; }
  132. /// <summary>
  133. /// 资源类型
  134. /// </summary>
  135. public Type ResxClass { get; set; }
  136. /// <summary>
  137. /// 资源
  138. /// </summary>
  139. public ResourceManager ResxManger
  140. {
  141. get { return (ResourceManager)GetValue(ResxMangerProperty); }
  142. set { SetValue(ResxMangerProperty, value); }
  143. }
  144. public static readonly DependencyProperty ResxMangerProperty =
  145. DependencyProperty.Register("ResxManger", typeof(ResourceManager),
  146. typeof(APLangType), new PropertyMetadata(null));
  147. /// <summary>
  148. /// 拓展语言资源
  149. /// </summary>
  150. public List<ExtLangModel> ExtLangModelList { get; set; }
  151. }
  152. /// <summary>
  153. /// ExtLangModel,拓展语言资源
  154. /// </summary>
  155. public class ExtLangModel
  156. {
  157. /// <summary>
  158. /// Key,关键字
  159. /// </summary>
  160. public string Key { get; set; }
  161. /// <summary>
  162. /// Value,值
  163. /// </summary>
  164. public string Value { get; set; }
  165. }
  166. }