当前位置:Gxlcms > asp.net > 建立自定义的数据驱动的本地化资源provider

建立自定义的数据驱动的本地化资源provider

时间:2021-07-01 10:21:17 帮助过:14人阅读

原文很长,为了便于阅读和理解,特将该文章改写成通俗易懂而且内容精炼的中文.

预备知识:系统默认的处理资源和本地化的方法是使用resx文件存储资源.

要使用自定义的resource provider,需要2个步骤:
a) 修改web.config 文件,以便系统使用自定义的资源提供者
b) 建立自定义资源提供者类,最少包括3个:
1.ResourceProviderFactory,工厂类,用来建立ResourceProvider对象.
2.ResourceProvider,实现IResourceProvider,IImplicitResourceProvider,IwwResourceProvider 接口.
3.ResourceReader 实现IResourceReader.


修改web.config 文件,以使用自定义的资源提供者。
代码如下:
  1. <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>

人气教程排行