研究了一下WSS3.0 WebPart 多语言支持的实现,共享一下.
WSS3.0支持两中类型的WebPart:
wss风格的WebPart - 从
Microsoft.SharePoint.WebPartPages.WebPart继承;
asp.net风格的WebPart - 从
System.Web.UI.WebControls.WebParts.WebPart继承;
对wss风格的webpart,采用系统提供的Microsoft.SharePoint.WebPartPages.ResourcesAttribute指定资源字符串key,然后
重载LoadResource方法处理资源文件的加载,示例如下:
ResTestWP
[Guid("137b67b0-6155-40dc-bff0-4ab766c87e86")]
public class ResTestWP : Microsoft.SharePoint.WebPartPages.WebPart // System.Web.UI.WebControls.WebParts.WebPart
{
public ResTestWP()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(GetResourceString("avreport_report14_part2"));
}
private string _Url;
[WebBrowsable]
[Personalizable]
[Resources(Category = "AddWrkfl_PermManageList",
Name = "AddWrkfl_PermManageList")]
public string Url
{
get { return _Url; }
set { _Url = value; }
}
public override string LoadResource(string id)
{
return GetResourceString( id ) ;
}
private string GetResourceString(string key )
{
const string resource = "wss";
//此处为资源文件名,这些资源文件放在App_GlobalResources,以wss.resx,wss.zh-CN.resx的规则命名
//wss为系统默认的资源文件,你可以用myresource.resx,myresource.zh_CN.resx来命名
string value = HttpContext.GetGlobalResourceObject(resource, key) as string;
if (value == null || value == "")
return key;
else
return value;
}
}
若是asp.netf风格的webpart,则实现稍微复杂一点,需要重新实现CategoryAttribute,WebDisplayNameAttribute的相关方法:
ResTestWP
[Guid("137b67b0-6155-40dc-bff0-4ab766c87e86")]
public class ResTestWP : System.Web.UI.WebControls.WebParts.WebPart
{
public ResTestWP()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(ResourceHelper.GetResourceString("avreport_report14_part2"));
}
private string _Url;
[WebBrowsable]
[Personalizable]
[ResWebDisplayName("AddWrkfl_PermManageList")]
[ResCategory("AddWrkfl_PermManageList")]
public string Url
{
get { return _Url; }
set { _Url = value; }
}
}
class ResourceHelper
{
public static string GetResourceString(string key )
{
const string resource = "wss";
//此处为资源文件名,这些资源文件放在App_GlobalResources,以wss.resx,wss.zh-CN.resx的规则命名
//wss为系统默认的资源文件,你可以用myresource.resx,myresource.zh_CN.resx来命名
string value = HttpContext.GetGlobalResourceObject(resource, key) as string;
if (value == null || value == "")
return key;
else
return value;
}
}
public class ResCategory : CategoryAttribute
{
public ResCategory( string key ) : base(key)
{ }
protected override string GetLocalizedString(string value)
{
return ResourceHelper.GetResourceString(value) ;
}
}
public class ResWebDisplayName : WebDisplayNameAttribute
{
public ResWebDisplayName(string key) : base(key)
{ }
public override string DisplayName
{
get
{
return ResourceHelper.GetResourceString(base.DisplayName);
}
}
}
通过以上的代码,可以实现WebPart编辑面板的多语言支持,webpart内部代码也可以通过ResourceHelper.GetResourceString获取资源字符串.
其他的Attribute类也可用上面的方法重新实现。
以上示例代码的命名不是很规范,ResCategory 应改成 ResCategoryAttribute.
ResWebDisplayName 应改成 ResWebDisplayNameAttribute. 编辑器不好用啊,就不改了~
下载代码:
/Files/jianyi0115/ResTestWP.rar
posted on
2007-12-24 21:29
jianyi
阅读(
1763)
评论()
编辑
收藏
举报