建立自定义的数据驱动的本地化资源provider
程序员文章站
2024-03-08 13:54:16
原文很长,为了便于阅读和理解,特将该文章改写成通俗易懂而且内容精炼的中文. 预备知识:系统默认的处理资源和本地化的方法是使用resx文件存储资源. 要使用自定义的resou...
原文很长,为了便于阅读和理解,特将该文章改写成通俗易懂而且内容精炼的中文.
预备知识:系统默认的处理资源和本地化的方法是使用resx文件存储资源.
要使用自定义的resource provider,需要2个步骤:
a) 修改web.config 文件,以便系统使用自定义的资源提供者
b) 建立自定义资源提供者类,最少包括3个:
1.resourceproviderfactory,工厂类,用来建立resourceprovider对象.
2.resourceprovider,实现iresourceprovider,iimplicitresourceprovider,iwwresourceprovider 接口.
3.resourcereader 实现iresourcereader.
修改web.config 文件,以使用自定义的资源提供者。
<configuration>
<system.web>
<globalization resourceproviderfactorytype="westwind.globalization.dbsimpleresourceproviderfactory,westwind.globalization" />
</system.web>
</configuration>
建立自定义资源提供者类:
1.工厂类
[designtimeresourceproviderfactoryattribute(typeof(dbdesigntimeresourceproviderfactory))]
public class dbsimpleresourceproviderfactory : resourceproviderfactory
{
public override iresourceprovider createglobalresourceprovider(string classname)
{
return new dbsimpleresourceprovider(null, classname);
}
public override iresourceprovider createlocalresourceprovider(string virtualpath)
{
string resourcesetname = dbresourceconfiguration.current.stripvirtualpath(virtualpath);
return new dbsimpleresourceprovider(null,resourcesetname.tolower());
}
}
2.提供者类
public class dbsimpleresourceprovider : iresourceprovider, iimplicitresourceprovider
{
private string _resourcesetname;
private idictionary _resourcecache;
private dbsimpleresourceprovider()
{ }
public dbsimpleresourceprovider(string virtualpath, string classname)
{
_resourcesetname = classname;
}
private idictionary getresourcecache(string culturename)
{
if (culturename == null)
culturename = "";
if (this._resourcecache == null)
this._resourcecache = new listdictionary();
idictionary resources = this._resourcecache[culturename] as idictionary;
if (resources == null)
{
// *** dependency here (#1): using dbresourcedatamanager to retrieve resources
// *** use datamanager to retrieve the resource keys from the database
dbresourcedatamanager data = new dbresourcedatamanager();
resources = data.getresourceset(culturename as string, this._resourcesetname);
this._resourcecache[culturename] = resources;
}
return resources;
}
public void clearresourcecache()
{
this._resourcecache.clear();
}
object iresourceprovider.getobject(string resourcekey, cultureinfo culture)
{
string culturename = null;
if (culture != null)
culturename = culture.name;
else
culturename = cultureinfo.currentuiculture.name;
return this.getobjectinternal(resourcekey, culturename);
}
object getobjectinternal(string resourcekey, string culturename)
{
idictionary resources = this.getresourcecache(culturename);
object value = null;
if (resources == null)
value = null;
else
value = resources[resourcekey];
// *** if we're at a specific culture (en-us) and there's no value fall back
// *** to the generic culture (en)
if (value == null && culturename.length > 3)
{
// *** try again with the 2 letter locale
return getobjectinternal(resourcekey,culturename.substring(0,2) );
}
// *** if the value is still null get the invariant value
if (value == null)
{
resources = this.getresourcecache("");
if (resources == null)
value = null;
else
value = resources[resourcekey];
}
// *** if the value is still null and we're at the invariant culture
// *** let's add a marker that the value is missing
// *** this also allows the pre-compiler to work and never return null
if (value == null && string.isnullorempty(culturename))
{
// *** no entry there
value = "";
// *** dependency here (#2): using dbresourceconfiguration and dbresourcedatamanager to optionally
// add missing resource keys
// *** add a key in the repository at least for the invariant culture
// *** something's referencing but nothing's there
if (dbresourceconfiguration.current.addmissingresources)
new dbresourcedatamanager().addresource(resourcekey, value.tostring(), "", this._resourcesetname);
}
return value;
}
3.reader类
public class dbsimpleresourcereader : iresourcereader
{
private idictionary _resources;
public dbsimpleresourcereader(idictionary resources)
{
_resources = resources;
}
idictionaryenumerator iresourcereader.getenumerator()
{
return _resources.getenumerator();
}
void iresourcereader.close()
{
}
ienumerator ienumerable.getenumerator()
{
return _resources.getenumerator();
}
void idisposable.dispose()
{
}
}
完毕。
本人没有测试过,待测试通过,献上最精炼的源代码.敬请稍候.
预备知识:系统默认的处理资源和本地化的方法是使用resx文件存储资源.
要使用自定义的resource provider,需要2个步骤:
a) 修改web.config 文件,以便系统使用自定义的资源提供者
b) 建立自定义资源提供者类,最少包括3个:
1.resourceproviderfactory,工厂类,用来建立resourceprovider对象.
2.resourceprovider,实现iresourceprovider,iimplicitresourceprovider,iwwresourceprovider 接口.
3.resourcereader 实现iresourcereader.
修改web.config 文件,以使用自定义的资源提供者。
复制代码 代码如下:
<configuration>
<system.web>
<globalization resourceproviderfactorytype="westwind.globalization.dbsimpleresourceproviderfactory,westwind.globalization" />
</system.web>
</configuration>
建立自定义资源提供者类:
1.工厂类
复制代码 代码如下:
[designtimeresourceproviderfactoryattribute(typeof(dbdesigntimeresourceproviderfactory))]
public class dbsimpleresourceproviderfactory : resourceproviderfactory
{
public override iresourceprovider createglobalresourceprovider(string classname)
{
return new dbsimpleresourceprovider(null, classname);
}
public override iresourceprovider createlocalresourceprovider(string virtualpath)
{
string resourcesetname = dbresourceconfiguration.current.stripvirtualpath(virtualpath);
return new dbsimpleresourceprovider(null,resourcesetname.tolower());
}
}
2.提供者类
复制代码 代码如下:
public class dbsimpleresourceprovider : iresourceprovider, iimplicitresourceprovider
{
private string _resourcesetname;
private idictionary _resourcecache;
private dbsimpleresourceprovider()
{ }
public dbsimpleresourceprovider(string virtualpath, string classname)
{
_resourcesetname = classname;
}
private idictionary getresourcecache(string culturename)
{
if (culturename == null)
culturename = "";
if (this._resourcecache == null)
this._resourcecache = new listdictionary();
idictionary resources = this._resourcecache[culturename] as idictionary;
if (resources == null)
{
// *** dependency here (#1): using dbresourcedatamanager to retrieve resources
// *** use datamanager to retrieve the resource keys from the database
dbresourcedatamanager data = new dbresourcedatamanager();
resources = data.getresourceset(culturename as string, this._resourcesetname);
this._resourcecache[culturename] = resources;
}
return resources;
}
public void clearresourcecache()
{
this._resourcecache.clear();
}
object iresourceprovider.getobject(string resourcekey, cultureinfo culture)
{
string culturename = null;
if (culture != null)
culturename = culture.name;
else
culturename = cultureinfo.currentuiculture.name;
return this.getobjectinternal(resourcekey, culturename);
}
object getobjectinternal(string resourcekey, string culturename)
{
idictionary resources = this.getresourcecache(culturename);
object value = null;
if (resources == null)
value = null;
else
value = resources[resourcekey];
// *** if we're at a specific culture (en-us) and there's no value fall back
// *** to the generic culture (en)
if (value == null && culturename.length > 3)
{
// *** try again with the 2 letter locale
return getobjectinternal(resourcekey,culturename.substring(0,2) );
}
// *** if the value is still null get the invariant value
if (value == null)
{
resources = this.getresourcecache("");
if (resources == null)
value = null;
else
value = resources[resourcekey];
}
// *** if the value is still null and we're at the invariant culture
// *** let's add a marker that the value is missing
// *** this also allows the pre-compiler to work and never return null
if (value == null && string.isnullorempty(culturename))
{
// *** no entry there
value = "";
// *** dependency here (#2): using dbresourceconfiguration and dbresourcedatamanager to optionally
// add missing resource keys
// *** add a key in the repository at least for the invariant culture
// *** something's referencing but nothing's there
if (dbresourceconfiguration.current.addmissingresources)
new dbresourcedatamanager().addresource(resourcekey, value.tostring(), "", this._resourcesetname);
}
return value;
}
3.reader类
复制代码 代码如下:
public class dbsimpleresourcereader : iresourcereader
{
private idictionary _resources;
public dbsimpleresourcereader(idictionary resources)
{
_resources = resources;
}
idictionaryenumerator iresourcereader.getenumerator()
{
return _resources.getenumerator();
}
void iresourcereader.close()
{
}
ienumerator ienumerable.getenumerator()
{
return _resources.getenumerator();
}
void idisposable.dispose()
{
}
}
完毕。
本人没有测试过,待测试通过,献上最精炼的源代码.敬请稍候.
推荐阅读
-
建立自定义的数据驱动的本地化资源provider
-
建立自定义的数据驱动的本地化资源provider
-
Apache+PHP+MySQL建立数据库驱动的动态网站_PHP
-
Apache+PHP+MySQL建立数据库驱动的动态网站_PHP
-
在ASP.NET 2.0中操作数据之六十:创建一个自定义的Database-Driven Site Map Provider
-
在ASP.NET 2.0中操作数据之六十:创建一个自定义的Database-Driven Site Map Provider
-
weblogic服务建立数据源连接测试更新mysql驱动包的问题及解决方法
-
weblogic服务建立数据源连接测试更新mysql驱动包的问题及解决方法