在项目中经常需要实现多语言其中包括webpart的属性也需要。那么如何实现呢?
首先需要资源文件,利用资源文件实现语言的翻译,如下图:
创建好资源后,下面我们来实现webpart属性的多语言。方法代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;
//****************************************************************
//编制人:XX
//编制作用:本地化WebPart的Category、WebDisplayName 和 WebDescription 属性
//编制时间:2013-05-07
//编制单位:XX
//****************************************************************
14: namespace TCL.EP.SPCommon
15: {
//WebPart类别本地化
class LocalizedCategoryAttribute : CategoryAttribute
18: {
public LocalizedCategoryAttribute(string category)
20: : base(category)
21: { }
22:
// Override this method to return values from the webpart's resource file.
value)
25: {
value, CultureInfo.CurrentUICulture.LCID);
27: }
28: }
29: #endregion
30:
//WebPart显示名称本地化
class LocalizedWebDisplayNameAttribute : WebDisplayNameAttribute
33: {
34: bool m_isLocalized;
35:
public LocalizedWebDisplayNameAttribute(string displayName)
37: : base(displayName)
38: { }
39:
// Override this property to return values from the webpart's resource file.
public override string DisplayName
42: {
get
44: {
if (!m_isLocalized)
46: {
47: this.DisplayNameValue = LocalizedUtility.GetLocalizedString(base.DisplayName, CultureInfo.CurrentUICulture.LCID);
true;
49: }
return base.DisplayName;
51: }
52: }
53: }
54: #endregion
55:
//WebPart描述本地化
class LocalizedWebDescriptionAttribute : WebDescriptionAttribute
58: {
59: bool m_isLocalized;
60:
public LocalizedWebDescriptionAttribute(string description)
62: : base(description)
63: { }
64:
// Override this property to return values from the webpart's resource file.
public override string Description
67: {
get
69: {
if (!m_isLocalized)
71: {
72: this.DescriptionValue = LocalizedUtility.GetLocalizedString(base.Description, CultureInfo.CurrentUICulture.LCID);
true;
74: }
return base.Description;
76: }
77: }
78: }
79: #endregion
80: }