using MuchInfo.Localization.Languages; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Resources; using System.Windows; namespace MuchInfo.Localization { /// /// APLangIndexer,语言资源索引器。 /// public class APLangIndexer : INotifyPropertyChanged { static APLangIndexer() { //TODO:初始化语言资源 var Rlt = new List(); Rlt.Add(new APLangType() { LangTypeAbbre = "zh_CN", Title = "简体中文", ResxClass = typeof(zh_CN), ResxManger = zh_CN.ResourceManager, ExtLangModelList = new List() }); Rlt.Add(new APLangType() { LangTypeAbbre = "en-US", Title = "English", ResxClass = typeof(en_US), ResxManger = en_US.ResourceManager, ExtLangModelList = new List() }); Rlt.Add(new APLangType() { LangTypeAbbre = "zh_TW", Title = "繁體中文", ResxClass = typeof(zh_TW), ResxManger = zh_TW.ResourceManager, ExtLangModelList = new List() }); _LangTypeList = Rlt; } /// /// 默认的语言列表。 /// /// public static List LangTypeList { get { return _LangTypeList; } } private static List _LangTypeList = null; /// /// LangType,当前语言类型。 /// public APLangType LangType { get { return _LangType; } set { _LangType = value; RaisePropertyChanged("APLangIndexer"); RaisePropertyChanged(string.Empty); } } private APLangType _LangType = LangTypeList[0]; /// /// 语言资源索引。 /// /// /// public string this[string ResKey] { get { var Rlt = ""; var NullRlt = "APConfig_Error"; //TODO:1、现在拓展语言资源找此关键字的语言资源 if (string.IsNullOrEmpty(Rlt)) { if (this.LangType.ExtLangModelList != null && this.LangType.ExtLangModelList.Where(x => x.Key == ResKey).Count() > 0) { Rlt = this.LangType.ExtLangModelList.First(x => x.Key == ResKey).Value; } } //TODO:如果没有找到,则在资源文件查找 if (string.IsNullOrEmpty(Rlt)) { var BdFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase; var Props = this.LangType.ResxClass.GetProperties(BdFlag); var Prop = Props.First(x => x.Name.Trim().ToUpper() == ResKey.Trim().ToUpper()); if (Prop != null) { try { Rlt = Prop.GetValue(null, null) + ""; } catch { ;} } } return Rlt == "" ? this[NullRlt] : Rlt; } } /// /// PropertyChanged,属性值发生变化事件。 /// public event PropertyChangedEventHandler PropertyChanged; /// /// 引发属性值发生变化事件 /// /// public void RaisePropertyChanged(string PropertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); } } } /// /// APLangType,语言类型定义。 /// public class APLangType : DependencyObject { /// /// 语言类型简称 /// public string LangTypeAbbre { get; set; } /// /// 语言标题 /// public string Title { get; set; } /// /// 资源类型 /// public Type ResxClass { get; set; } /// /// 资源 /// public ResourceManager ResxManger { get { return (ResourceManager)GetValue(ResxMangerProperty); } set { SetValue(ResxMangerProperty, value); } } public static readonly DependencyProperty ResxMangerProperty = DependencyProperty.Register("ResxManger", typeof(ResourceManager), typeof(APLangType), new PropertyMetadata(null)); /// /// 拓展语言资源 /// public List ExtLangModelList { get; set; } } /// /// ExtLangModel,拓展语言资源 /// public class ExtLangModel { /// /// Key,关键字 /// public string Key { get; set; } /// /// Value,值 /// public string Value { get; set; } } }