<asp:Label ID="Label2" runat="server" Text="Input the Company Name" resourcekey="Label2Resource1"></asp:Label> label控件并没有resourcekey属性,而在dotnetnuke里面却可以根据这个属性获取资源文件里面相应的数据,这是为什么呢?
dotnetnuke里的每个aspx页面都是继承DotNetNuke.Framework.CDefault,CDefault同时又继承DotNetNuke.Framework.pagebase类。
cdfault类只包含一些简单的属性,我想应该在pagebase里面做了文章。研究了半天,终于确定的确是在这里实现根据resourcekey获取资源文件的内容。
Private Overloads Sub IterateControls(ByVal controls As ControlCollection, ByVal affectedControls As ArrayList, ByVal ResourceFileRoot As String)
For Each c As Control In controls
ProcessControl(c, affectedControls, True, ResourceFileRoot)
Next
End Sub 'IterateControls
通过上面方法循环对页面中的控件进行处理,从资源文件获取resoucekey的value并把他赋给控件,例如label控件,设
label.Text=value,处理方法为:
Friend Sub ProcessControl(ByVal c As Control, ByVal affectedControls As ArrayList, ByVal includeChildren As Boolean, ByVal ResourceFileRoot As String)
获取resourcekey的value的方法是:
value = Services.Localization.Localization.GetString(key, ResourceFileRoot)
而怎样获取方法中的key呢?比如就是获取顶端的resoucekey的值Label2Resource1,方法如下:
Dim key As String = GetControlAttribute(c, affectedControls) 方法完整代码如下:
Friend Shared Function GetControlAttribute(ByVal c As Control, ByVal affectedControls As ArrayList) As String
Dim ac As System.Web.UI.AttributeCollection = Nothing
Dim key As String = Nothing
If TypeOf c Is LiteralControl Then ' LiteralControls don't have an attribute collection
key = Nothing
ac = Nothing
Else
If TypeOf c Is WebControl Then
Dim w As WebControl = DirectCast(c, WebControl)
ac = w.Attributes
key = ac(Services.Localization.Localization.KeyName)
Else
If TypeOf c Is HtmlControl Then
Dim h As HtmlControl = DirectCast(c, HtmlControl)
ac = h.Attributes
key = ac(Services.Localization.Localization.KeyName)
Else
If TypeOf c Is UserControl Then
Dim u As UserControl = DirectCast(c, UserControl)
ac = u.Attributes
key = ac(Services.Localization.Localization.KeyName)
' Use reflection to check for attribute key. This is a last ditch option
Else
Dim controlType As Type = c.GetType()
Dim attributeProperty As PropertyInfo = controlType.GetProperty("Attributes",
GetType(System.Web.UI.AttributeCollection))
If Not attributeProperty Is Nothing Then
ac = DirectCast(attributeProperty.GetValue(c, Nothing),
System.Web.UI.AttributeCollection)
key = ac(Services.Localization.Localization.KeyName)
End If
End If
End If
End If ' If the key was found add this AttributeCollection
'to the list that should have the key removed during Render
End If
If Not key Is Nothing And Not affectedControls Is Nothing Then
affectedControls.Add(ac)
End If
Return key
End Function 'GetControlAttribute
先验证控件类型,然后获取控件属性集合,从中获取keyname的value,keyname就是“resourcekey”。
就这么多了,
然后简要介绍dotnetnuke本地化方法:
如何使用aspx页面本地化资源文件?
直接在控件中加resourcekey属性,如:
<asp:Label ID="Label2" runat="server" Text="Input the Company Name" resourcekey="Label2Resource1"></asp:Label><br />
resourcekey可以为例中的Lable2Resource1,也可以为Lable2Resource1.Text。
如何使用用户控件ascx页面的资源文件?
使用方法:
Label2.Text = Services.Localization.Localization.GetString("label2",
Services.Localization.Localization.GetResourceFile(Me, MyFileName))
即可。
Tag标签: DotNetNuke,DNN,本地化,LocalResource,Resource