DotNetNuke 本地化技术

         < 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))
即可。

转载于:https://www.cnblogs.com/iam_wangzhe/archive/2009/08/20/1551000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值