sharepoint2010如何本地化WebPart的Category、WebDisplayName 和 WebDescription 属性

在项目中经常需要实现多语言其中包括webpart的属性也需要。那么如何实现呢?

首先需要资源文件,利用资源文件实现语言的翻译,如下图:

image

创建好资源后,下面我们来实现webpart属性的多语言。方法代码如下:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using System.Globalization;
   5: using System.Web;
   6: using System.Web.UI.WebControls.WebParts;
   7: using System.ComponentModel;
   8: //****************************************************************
   9: //编制人:XX
  10: //编制作用:本地化WebPart的Category、WebDisplayName 和 WebDescription 属性
  11: //编制时间:2013-05-07
  12: //编制单位:XX
  13: //****************************************************************
  14: namespace TCL.EP.SPCommon
  15: {
  16:     #region//WebPart类别本地化
  17:     public sealed class LocalizedCategoryAttribute : CategoryAttribute
  18:     {
  19:         public LocalizedCategoryAttribute(string category)
  20:             : base(category)
  21:         { }
  22:  
  23:         // Override this method to return values from the webpart's resource file.
  24:         protected override string GetLocalizedString(string value)
  25:         {
  26:             return LocalizedUtility.GetLocalizedString(value, CultureInfo.CurrentUICulture.LCID);
  27:         }
  28:     }
  29:     #endregion
  30:  
  31:     #region//WebPart显示名称本地化
  32:     public sealed class LocalizedWebDisplayNameAttribute : WebDisplayNameAttribute
  33:     {
  34:         bool m_isLocalized;
  35:  
  36:         public LocalizedWebDisplayNameAttribute(string displayName)
  37:             : base(displayName)
  38:         { }
  39:  
  40:         // Override this property to return values from the webpart's resource file.
  41:         public override string DisplayName
  42:         {
  43:             get
  44:             {
  45:                 if (!m_isLocalized)
  46:                 {
  47:                     this.DisplayNameValue = LocalizedUtility.GetLocalizedString(base.DisplayName, CultureInfo.CurrentUICulture.LCID);
  48:                     m_isLocalized = true;
  49:                 }
  50:                 return base.DisplayName;
  51:             }
  52:         }
  53:     }
  54:     #endregion
  55:  
  56:     #region//WebPart描述本地化
  57:     public sealed class LocalizedWebDescriptionAttribute : WebDescriptionAttribute
  58:     {
  59:         bool m_isLocalized;
  60:  
  61:         public LocalizedWebDescriptionAttribute(string description)
  62:             : base(description)
  63:         { }
  64:  
  65:         // Override this property to return values from the webpart's resource file.
  66:         public override string Description
  67:         {
  68:             get
  69:             {
  70:                 if (!m_isLocalized)
  71:                 {
  72:                     this.DescriptionValue = LocalizedUtility.GetLocalizedString(base.Description, CultureInfo.CurrentUICulture.LCID);
  73:                     m_isLocalized = true;
  74:                 }
  75:                 return base.Description;
  76:             }
  77:         }
  78:     }
  79:     #endregion
  80: }

调用例子如下:

 

   1: /// <summary>
   2:       /// 图片打开的URL
   3:       /// </summary>
   4:       [WebBrowsable(true)]
   5:       [LocalizedWebDisplayName("TCL.WebPartAttribute.PhotoImageUrl")]
   6:       [LocalizedWebDescription("TCL.WebPartAttribute.PhotoImageUrl")]
   7:       [Personalizable(PersonalizationScope.Shared)]
   8:       [LocalizedCategory("TCL.WebPartAttribute.SelfName")]
   9:       public string PhotoImageUrl { get; set; }
posted @ 2013-09-01 13:21  love007  阅读(527)  评论(1编辑  收藏  举报