时间:2021-07-01 10:21:17 帮助过:14人阅读
- <br><configuration> <br><system.web> <br><globalization resourceProviderFactoryType="Westwind.Globalization.DbSimpleResourceProviderFactory,Westwind.Globalization" /> <br></system.web> <br></configuration> <br> <br><br>建立自定义资源提供者类: <br>1.工厂类 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>[DesignTimeResourceProviderFactoryAttribute(typeof(DbDesignTimeResourceProviderFactory))] <br>public class DbSimpleResourceProviderFactory : ResourceProviderFactory <br>{ <br><br>public override IResourceProvider CreateGlobalResourceProvider(string classname) <br>{ <br>return new DbSimpleResourceProvider(null, classname); <br>} <br><br><br>public override IResourceProvider CreateLocalResourceProvider(string virtualPath) <br>{ <br><br>string ResourceSetName = DbResourceConfiguration.Current.StripVirtualPath(virtualPath); <br>return new DbSimpleResourceProvider(null,ResourceSetName.ToLower()); <br>} <br>} <br> <br>2.提供者类 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>public class DbSimpleResourceProvider : IResourceProvider, IImplicitResourceProvider <br>{ <br><br>private string _ResourceSetName; <br><br><br>private IDictionary _resourceCache; <br><br><br>private DbSimpleResourceProvider() <br>{ } <br><br><br>public DbSimpleResourceProvider(string virtualPath, string className) <br>{ <br>_ResourceSetName = className; <br>} <br><br><br><br>private IDictionary GetResourceCache(string cultureName) <br>{ <br>if (cultureName == null) <br>cultureName = ""; <br><br><br>if (this._resourceCache == null) <br>this._resourceCache = new ListDictionary(); <br><br><br>IDictionary Resources = this._resourceCache[cultureName] as IDictionary; <br>if (Resources == null) <br>{ <br>// *** DEPENDENCY HERE (#1): Using DbResourceDataManager to retrieve resources <br><br><br>// *** Use datamanager to retrieve the resource keys from the database <br>DbResourceDataManager Data = new DbResourceDataManager(); <br>Resources = Data.GetResourceSet(cultureName as string, this._ResourceSetName); <br>this._resourceCache[cultureName] = Resources; <br>} <br><br><br>return Resources; <br>} <br><br><br><br>public void ClearResourceCache() <br>{ <br>this._resourceCache.Clear(); <br>} <br><br><br><br>object IResourceProvider.GetObject(string ResourceKey, CultureInfo Culture) <br>{ <br>string CultureName = null; <br>if (Culture != null) <br>CultureName = Culture.Name; <br>else <br>CultureName = CultureInfo.CurrentUICulture.Name; <br><br><br>return this.GetObjectInternal(ResourceKey, CultureName); <br>} <br><br><br><br>object GetObjectInternal(string ResourceKey, string CultureName) <br>{ <br>IDictionary Resources = this.GetResourceCache(CultureName); <br><br>object value = null; <br>if (Resources == null) <br>value = null; <br>else <br>value = Resources[ResourceKey]; <br><br>// *** If we're at a specific culture (en-Us) and there's no value fall back <br>// *** to the generic culture (en) <br>if (value == null && CultureName.Length > 3) <br>{ <br>// *** try again with the 2 letter locale <br>return GetObjectInternal(ResourceKey,CultureName.Substring(0,2) ); <br>} <br><br><br>// *** If the value is still null get the invariant value <br>if (value == null) <br>{ <br>Resources = this.GetResourceCache(""); <br>if (Resources == null) <br>value = null; <br>else <br>value = Resources[ResourceKey]; <br>} <br><br><br>// *** If the value is still null and we're at the invariant culture <br>// *** let's add a marker that the value is missing <br>// *** this also allows the pre-compiler to work and never return null <br>if (value == null && string.IsNullOrEmpty(CultureName)) <br>{ <br>// *** No entry there <br>value = ""; <br><br><br>// *** DEPENDENCY HERE (#2): using DbResourceConfiguration and DbResourceDataManager to optionally <br>// add missing resource keys <br><br><br>// *** Add a key in the repository at least for the Invariant culture <br>// *** Something's referencing but nothing's there <br>if (DbResourceConfiguration.Current.AddMissingResources) <br>new DbResourceDataManager().AddResource(ResourceKey, value.ToString(), "", this._ResourceSetName); <br><br><br>} <br><br><br>return value; <br>} <br><br> <br>3.Reader类 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>public class DbSimpleResourceReader : IResourceReader <br>{ <br>private IDictionary _resources; <br><br><br>public DbSimpleResourceReader(IDictionary resources) <br>{ <br>_resources = resources; <br>} <br>IDictionaryEnumerator IResourceReader.GetEnumerator() <br>{ <br>return _resources.GetEnumerator(); <br>} <br>void IResourceReader.Close() <br>{ <br>} <br>IEnumerator IEnumerable.GetEnumerator() <br>{ <br>return _resources.GetEnumerator(); <br>} <br>void IDisposable.Dispose() <br>{ <br>} <br>} <br> <br>完毕。 <br>本人没有测试过,待测试通过,献上最精炼的源代码.敬请稍候.</li><li> </li><li> </li></ol></pre></li></ol></pre></li></ol></pre>