|
|
|
|
www.wesnetdesigns.com Joined: 2/18/2005
Posts: 3253
|
|
|
Needing to localize a GridView control's header text as well as the captions on the buttons in a CommandField, I wrote the following:
Public Shared Sub LocalizeGridView(ByVal gv As GridView, ByVal ResourceFile As String)
Dim key As String
Dim localizedText As String
Dim pi As System.Reflection.PropertyInfo
For Each col As DataControlField In gv.Columns
key = col.HeaderText
If key <> "" Then
localizedText = Localization.GetString(key & ".Header", ResourceFile)
If localizedText <> "" Then
col.HeaderText = localizedText
End If
End If
'Localize text of Cancel, Delete, Edit, Insert, Select, New, Update buttons
If TypeOf col Is CommandField Then
Dim cmdField As CommandField = DirectCast(col, CommandField)
For Each cmdName As String In New String() {"Cancel", "Delete", "Edit", "Insert", "Select", "New", "Update"}
pi = cmdField.GetType.GetProperty(cmdName & "Text", GetType(String))
key = CType(pi.GetValue(cmdField, Nothing), String)
If Not String.IsNullOrEmpty(key) Then
localizedText = Localization.GetString(key & ".CommandText", ResourceFile)
If localizedText <> "" Then
pi.SetValue(cmdField, localizedText, Nothing)
End If
End If
Next
End If
Next
End Sub
In the local resource file, the keys for Header text should end with .Header while the keys for the CommandField button captions should end with .CommandText. In a CommandField, the EditText, CancelText, etc. default to "Edit", "Cancel", etc. so unless the defaults are overridden in the CommandField declaration, use keys of "Edit.CommandText", "Cancel.CommandText", etc. in the resource file.
I'm not sure if my use of reflection in getting and setting the captions of the CommandField buttons is the most efficient performance wise, it was quick to write using fewer statements.
Hope this is helpful to someone!
Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
|
|
|
|
| |
|
|
Joined: 5/31/2003
Posts: 219
|
|
|
First thank your for sharing the code. Using reflection is very cool. I tried and it worked great.
|
|
|
|
| |
|
|
Joined: 5/24/2007
Posts: 2
|
|
|
Could you show me the way to translate in to C# language.
I tried to translate:
private void LocalizeGridView(GridView gv)
{
string key;
string localizedText;
System.Reflection.PropertyInfo pi;
foreach (DataControlField col in gv.Columns)
{
key = col.HeaderText;
if (key != "")
{
localizedText = Localization.GetString(key + ".Header", this.LocalResourceFile);
if (localizedText != "")
{
col.HeaderText = localizedText;
}
}
//Localize text of Cancel, Delete, Edit, Insert, Select, New, Update buttons
if (col is CommandField)
{
CommandField cmdField = (CommandField)col;
string[] strCommandName = new string[4] {"Cancel", "Delete", "Edit", "Update"};
foreach (string cmdName in strCommandName)
{
pi = cmdField.GetType.GetProperty(cmdName & "Text", GetType(String))
key = pi.GetValue(cmdField, null).ToString();
if (string.IsNullOrEmpty(key) == false)
{
localizedText = Localization.GetString(key + ".CommandText", this.LocalResourceFile);
if (localizedText != "")
{
pi.SetValue(cmdField, localizedText, null);
}
}
}
}
}
}
Help me convert red code !
|
|
|
|
| |
|
|
Joined: 5/24/2007
Posts: 2
|
|
|
imagemaker wrote
Needing to localize a GridView control's header text as well as the captions on the buttons in a CommandField, I wrote the following:
Public Shared Sub LocalizeGridView(ByVal gv As GridView, ByVal ResourceFile As String) Dim key As String Dim localizedText As String Dim pi As System.Reflection.PropertyInfo
For Each col As DataControlField In gv.Columns key = col.HeaderText If key <> "" Then localizedText = Localization.GetString(key & ".Header", ResourceFile) If localizedText <> "" Then col.HeaderText = localizedText End If End If
'Localize text of Cancel, Delete, Edit, Insert, Select, New, Update buttons If TypeOf col Is CommandField Then Dim cmdField As CommandField = DirectCast(col, CommandField) For Each cmdName As String In New String() {"Cancel", "Delete", "Edit", "Insert", "Select", "New", "Update"} pi = cmdField.GetType.GetProperty(cmdName & "Text", GetType(String)) key = CType(pi.GetValue(cmdField, Nothing), String) If Not String.IsNullOrEmpty(key) Then localizedText = Localization.GetString(key & ".CommandText", ResourceFile) If localizedText <> "" Then pi.SetValue(cmdField, localizedText, Nothing) End If End If Next End If Next End Sub
In the local resource file, the keys for Header text should end with .Header while the keys for the CommandField button captions should end with .CommandText. In a CommandField, the EditText, CancelText, etc. default to "Edit", "Cancel", etc. so unless the defaults are overridden in the CommandField declaration, use keys of "Edit.CommandText", "Cancel.CommandText", etc. in the resource file.
I'm not sure if my use of reflection in getting and setting the captions of the CommandField buttons is the most efficient performance wise, it was quick to write using fewer statements.
Hope this is helpful to someone!
Could you show me the way to translate all into C Sharp language !
Thank so much !
|
|
|
|
| |
|
|
Joined: 10/1/2007
Posts: 1
|
|
|
pi = cmdField.GetType().GetProperty(cmdName + "Text", typeof(String));
give that a try
|
|
|
|
| |