| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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
- {
- /// <summary>
- /// APLangIndexer,语言资源索引器。
- /// </summary>
- public class APLangIndexer : INotifyPropertyChanged
- {
- static APLangIndexer()
- {
- //TODO:初始化语言资源
- var Rlt = new List<APLangType>();
- Rlt.Add(new APLangType()
- {
- LangTypeAbbre = "zh_CN",
- Title = "简体中文",
- ResxClass = typeof(zh_CN),
- ResxManger = zh_CN.ResourceManager,
- ExtLangModelList = new List<ExtLangModel>()
- });
- Rlt.Add(new APLangType()
- {
- LangTypeAbbre = "en-US",
- Title = "English",
- ResxClass = typeof(en_US),
- ResxManger = en_US.ResourceManager,
- ExtLangModelList = new List<ExtLangModel>()
- });
- Rlt.Add(new APLangType()
- {
- LangTypeAbbre = "zh_TW",
- Title = "繁體中文",
- ResxClass = typeof(zh_TW),
- ResxManger = zh_TW.ResourceManager,
- ExtLangModelList = new List<ExtLangModel>()
- });
- _LangTypeList = Rlt;
- }
- /// <summary>
- /// 默认的语言列表。
- /// </summary>
- /// <returns></returns>
- public static List<APLangType> LangTypeList { get { return _LangTypeList; } }
- private static List<APLangType> _LangTypeList = null;
- /// <summary>
- /// LangType,当前语言类型。
- /// </summary>
- public APLangType LangType
- {
- get { return _LangType; }
- set
- {
- _LangType = value;
- RaisePropertyChanged("APLangIndexer");
- RaisePropertyChanged(string.Empty);
- }
- }
- private APLangType _LangType = LangTypeList[0];
- /// <summary>
- /// 语言资源索引。
- /// </summary>
- /// <param name="ResKey"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// PropertyChanged,属性值发生变化事件。
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>
- /// 引发属性值发生变化事件
- /// </summary>
- /// <param name="PropertyName"></param>
- public void RaisePropertyChanged(string PropertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
- }
- }
- }
- /// <summary>
- /// APLangType,语言类型定义。
- /// </summary>
- public class APLangType : DependencyObject
- {
- /// <summary>
- /// 语言类型简称
- /// </summary>
- public string LangTypeAbbre { get; set; }
- /// <summary>
- /// 语言标题
- /// </summary>
- public string Title { get; set; }
- /// <summary>
- /// 资源类型
- /// </summary>
- public Type ResxClass { get; set; }
- /// <summary>
- /// 资源
- /// </summary>
- 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));
- /// <summary>
- /// 拓展语言资源
- /// </summary>
- public List<ExtLangModel> ExtLangModelList { get; set; }
- }
- /// <summary>
- /// ExtLangModel,拓展语言资源
- /// </summary>
- public class ExtLangModel
- {
- /// <summary>
- /// Key,关键字
- /// </summary>
- public string Key { get; set; }
- /// <summary>
- /// Value,值
- /// </summary>
- public string Value { get; set; }
- }
- }
|