| 以下是引用片段: /**//// /// 翻译类,将内部码翻译成容易理解的中文 /// /// /// 根据配置文件中的信息,将系统内部码(错误码、成功码)翻译成中文(或人容易理解的语言)。 /// public static class Translation ...{ private static System.IO.FileSystemWatcher watcher; private static XmlDocument content; private static string configFile; private static object locker = new object(); /**//// /// 加载配置文件 /// /// public static void Configure(string configFile) ...{ LoadFile(configFile); if (watcher != null) ...{ watcher.Dispose(); } watcher = new FileSystemWatcher(Path.GetDirectoryName(configFile), Path.GetFileName(configFile)); watcher.Changed += new FileSystemEventHandler(watcher_Changed); } /**//// /// 加载默认配置文件 /// public static void Configure() ...{ if (System.Web.HttpContext.Current != null) ...{ Configure(System.Web.HttpContext.Current.Server.MapPath("~/translation.config")); } else ...{ Configure(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\" + "translation.config"); } } /**//// /// 加载文件内容 /// /// private static void LoadFile(string configFile) ...{ lock (locker) ...{ XmlDocument doc = new XmlDocument(); doc.Load(configFile); content = doc; Translation.configFile = configFile; } } /**//// /// 当文件变更时,从新加载文件 /// /// /// private static void watcher_Changed(object sender, FileSystemEventArgs e) ...{ LoadFile(configFile); } /**//// /// 获取Enum的解释,如果Enum有Flag标记,则使用逗号分隔各个解释 /// /// /// public static string GetEnumDescription(Enum enumValue) ...{ return GetEnumDescription(enumValue, ","); } /**//// /// 获取Enum的解释,如果Enum有Flag标记,则使用sparator分隔各个解释 /// /// /// /// public static string GetEnumDescription(Enum enumValue, string sparator) ...{ Type type = enumValue.GetType(); //检查类型是否有Flags特性 object[] attrs = type.GetCustomAttributes(typeof(FlagsAttribute), false); if (attrs.Length > 0) ...{ StringBuilder builder = new StringBuilder(); Array arr = Enum.GetValues(type); foreach (Enum enu in arr) //循环获取每一个值的解释 ...{ if ((Convert.ToUInt64(enumValue) & Convert.ToUInt64(enu)) == Convert.ToUInt64(enu)) //判断是否有这个值 ...{ builder.Append(GetEnumDes(type, enu.ToString())); builder.Append(sparator); } } if (builder.Length != 0) //拿掉最后的分隔符 builder.Remove(builder.Length - sparator.Length, sparator.Length); return builder.ToString(); } else ...{ return GetEnumDes(type, enumValue.ToString()); } } /**//// /// 获取某一Enum类型值的解释 /// /// /// /// private static string GetEnumDes(Type type, string value) ...{ string xquery = "/translation/enum/" + type.FullName + "/" + value; XmlNode node = content.SelectSingleNode(xquery); if (node != null) return node.InnerText; else return value; } /**//// /// 翻译指定值 /// /// /// public static string GetValueDescription(object obj) ...{ return GetValueDescription("default", obj); } /**//// /// 在指定组中翻译指定值 /// /// /// /// public static string GetValueDescription(string group, object obj) ...{ if (obj == null) return "null"; string xquery = "/translation/description[@group='" + group + "']/add[@key='" + obj.ToString() + "']/@value"; XmlNode node = content.SelectSingleNode(xquery); if (node == null) return obj.ToString(); else return node.Value; } } |
| 以下是引用片段: <?xml version="1.0" encoding="utf-8" ?> <translation> <enum> <!--此出要用枚举的全名--> <Library.UserType> <Unknow>未知</Unknow> <AfterPayUser>后付费用户</AfterPayUser> <BeforePayUser>预付费用户</BeforePayUser> </Library.UserType> </enum> <description group="default"> <add key="FileErrorl" value="文件已损坏" /> </description> <description group="skin"> <add key="Default" value="默认皮肤" /> </description> <description group="topic"> <add key="space" value="<span class='red'>您剩余的空间不足,请您删除部分文件。</span><br/>" /> <add key="yue" value="<span class='red'>您的余额不足,请尽快充值。</span><br />" /> </description> </translation> |