| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections;
- using System.Globalization;
- using System.IO;
- using System.Resources;
- namespace Muchinfo.MTPClient.CustomException
- {
- public class ErrorResourceManager : ResourceManager
- {
- private const string _defaultFileName = "Muchinfo.ErrorResource.zh-CN.dll";
- private const string _defaultCultureName = "zh-CN";
- private const string _unKnownResource = "UnKnownResource";
- private const string _unfindResource = "UnFindResource";
- private static ResourceManager resourceMan;
- public static ResourceManager ResourceManager
- {
- get
- {
- if (object.ReferenceEquals(resourceMan, null))
- {
- global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Muchinfo.MTPClient.CustomException.ErrorResources", typeof(ErrorResources).Assembly);
- resourceMan = temp;
- }
- return resourceMan;
- }
- }
- public ErrorResourceManager()
- {
- ResourceSets = new Hashtable();
- var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _defaultFileName);
- try
- {
- LoadResourceFiles(filePath, _defaultCultureName);
- }
- catch (Exception) //读取不到文件使用系统默认的
- {
- }
- }
- public ErrorResourceManager(string errorResourceFilePath)
- {
- ResourceSets = new Hashtable();
- try
- {
- LoadResourceFiles(errorResourceFilePath, _defaultCultureName);
- }
- catch (Exception) //读取不到文件使用系统默认的
- {
- }
- }
- public void LoadResourceFiles(string resourceFile, string cultureName)
- {
- var reader = new ResourceReader(resourceFile);
- var resourceSet = new ResourceSet(reader);
- this.ResourceSets[cultureName] = resourceSet;
- }
- public override string GetString(string name)
- {
- var culture = new CultureInfo(_defaultCultureName);
- return GetString(name, culture);
- }
- public override string GetString(string name, CultureInfo culture)
- {
- if (this.ResourceSets != null && this.ResourceSets.Count > 0)
- {
- if (culture == null)
- {
- culture = new CultureInfo(_defaultCultureName);
- }
- if (this.ResourceSets.Contains(culture.Name))
- {
- var resourceSets = this.ResourceSets[culture.Name] as ResourceSet;
- if (resourceSets != null)
- {
- try
- {
- return resourceSets.GetString(name);
- }
- catch
- {
- return _unfindResource;
- }
- }
- }
- }
- else
- {
- return ResourceManager.GetString(name, null);
- }
- return _unKnownResource;
- }
- }
- public partial class ErrorResources
- {
- private static bool _resourceLoaded = false;
- public static bool ResourceLoaded
- {
- get
- {
- return _resourceLoaded;
- }
- }
- public static void Resource(ResourceManager manager)
- {
- resourceMan = manager;
- _resourceLoaded = true;
- }
- }
- }
|