WebPart多语言的实现:

WPResource.cs

View Code
using System;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.ComponentModel;
using System.Web;

namespace WebPartCollection.Common
{

public static class WPResource
{
const string RESOURCE_FILE_NAME = "Anson";
/// <summary>
/// 获取资源字符串的值
/// </summary>
/// <param name="key">资源字符串key</param>
/// <returns></returns>
public static string GetString(string key)
{
try
{
string value = "" + HttpContext.GetGlobalResourceObject(RESOURCE_FILE_NAME, key);
//value = ""+HttpContext.GetGlobalResourceObject(RESOURCE_FILE_NAME, key, System.Threading.Thread.CurrentThread.CurrentCulture);
if (value == "")
return key;
else
return value;
}
catch (Exception ex)
{
throw new SPException("access resource file [" + RESOURCE_FILE_NAME + "] error,please confirm files in App_GlobalResources.", ex);
}
}
}

/// <summary>
/// 属性分类,名称从资源中获取
/// </summary>
public class ResCategoryAttribute : CategoryAttribute
{
public ResCategoryAttribute(string key)
: base(key)
{ }

protected override string GetLocalizedString(string value)
{
return WPResource.GetString(value);
}
}

/// <summary>
/// 属性显示名,从资源中获取
/// </summary>
public class ResWebDisplayNameAttribute : WebDisplayNameAttribute
{
public ResWebDisplayNameAttribute(string key)
: base(key)
{ }

public override string DisplayName
{
get
{
return WPResource.GetString(base.DisplayName);
}
}
}


public class ResConnectionProviderAttribute : ConnectionProviderAttribute
{
public ResConnectionProviderAttribute(string name) : base(name)
{
}

public ResConnectionProviderAttribute(string name,string id) : base(name,id)
{
}

public override string DisplayName
{
get
{
return WPResource.GetString(base.DisplayName);
}
}
}

public class ResConnectionConsumerAttribute : ConnectionConsumerAttribute
{
public ResConnectionConsumerAttribute(string name)
: base(name)
{
}

public ResConnectionConsumerAttribute(string name, string id)
: base(name, id)
{
}

public override string DisplayName
{
get
{
return WPResource.GetString(base.DisplayName);
}
}
}
}

WebPart文件代码:

View Code
using System;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls.WebParts;

using WebPartCollection.Common;

namespace WebPartCollection
{
[Guid("c91e5b6c-702f-4716-b4df-2d70858e57cc")]
public class MutilanguageWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public MutilanguageWebPart()
{
}

private string _URL;
[WebBrowsable,Personalizable]
[ResCategory("XMLURL")]
[ResWebDisplayName("XMLURL")]
public string URL
{
get { return _URL; }
set { _URL = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
}
}
}

最后一步,新建两个资源文件,分别是Anson.resx和Anson.zh-CN.resxy,在两个两个文件添加key为XMLURL,及value,最后保存,把这两个文件放端口下的App_GlobalResources文件夹,如90\App_GlobalResources

 

posted on 2011-10-04 14:03  gzh4455  阅读(377)  评论(0编辑  收藏  举报