DevExpress WinForm 控件汉化方法 DevExpress
使用
DevExpress控件开发时,汉化一直是一个很纠结的问题,各种方法均存在很多不足,在此不作评论。
现将本人实际使用中找到的方法提出共享,多提宝贵意见。
一、本地化对象。DevExpress的文档中列出的是14个。
Product |
Localizer Class |
Resource String Enumeration |
XtraBars |
BarLocalizer |
BarString |
XtraCharts |
ChartLocalizer |
ChartStringId |
XtraEditors |
Localizer |
StringId |
XtraGauges |
GaugeCoreLocalizer |
GaugeCoreStringId |
XtraGrid |
GridLocalizer |
GridStringId |
XtraLayoutControl |
LayoutLocalizer |
LayoutStringId |
XtraNavBar |
NavBarLocalizer |
NavBarStringId |
XtraPivotGrid |
PivotGridLocalizer |
PivotGridStringId |
XtraPrinting |
PreviewLocalizer |
PreviewStringId |
XtraReports |
ReportLocalizer |
ReportStringId |
XtraScheduler |
SchedulerLocalizer |
SchedulerStringId |
XtraSpellChecker |
SpellCheckerLocalizer |
SpellCheckerStringId |
XtraTreeList |
TreeListLocalizer |
TreeListStringId |
XtraVerticalGrid |
VGridLocalizer |
VGridStringId |
另外还有:
DevExpress.Accessibility.AccLocalizer |
AccStringId |
DevExpress.XtraRichEdit.Localization.XtraRichEditLocalizer |
XtraRichEditStringId |
DevExpress.XtraWizard.Localization.WizardLocalizer |
WizardStringId |
DevExpress.XtraScheduler.Localization.SchedulerExtensionsLocalizer |
SchedulerExtensionsStringId |
DevExpress.XtraRichEdit.Localization.RichEditExtensionsLocalizer |
RichEditExtensionsStringId |
二、 资源字符串枚举。需要确切取得所有的Resource String Enumeration成员及其值(英语),才能汉化。首先可以到源码中查找,如可以在C:\Program Files (x86)\DevExpress 2011.2\Components\Sources\DevExpress.XtraBars\DevExpress.XtraBars中找到 Localizer.cs文件,有一个protected override void PopulateStringTable()过程,里面的代码是加入枚举字符串的值:
AddString(BarString.None, "");
AddString(BarString.PopupMenuEditor, "Popup Menu Editor");
AddString(BarString.AddOrRemove, "&Add or Remove Buttons");
AddString(BarString.ResetBar, "Are you sure you want to reset the changes made to the '{0}' toolbar?");
AddString(BarString.ResetBarCaption, "Customize");
AddString(BarString.ResetButton, "&Reset Toolbar");
AddString(BarString.CustomizeButton, "&Customize ...");
AddString(BarString.ToolBarMenu, "&Reset$&Delete$!&Name$!Defau< style$&Text Only (Always)$Text &Only (in Menus)$" +
"Image &and Text$!Begin a &Group$&Visible$&Most recently used");
AddString(BarString.ToolbarNameCaption, "&Toolbar Name:");
AddString(BarString.NewToolbarCaption, "New Toolbar");
AddString(BarString.NewToolbarCustomNameFormat, "Custom {0}");
AddString(BarString.NewMenuName, "Main menu");
AddString(BarString.NewToolbarName, "Tools");
AddString(BarString.NewStatusBarName, "Status bar");
AddString(BarString.RenameToolbarCaption, "Rename Toolbar");
AddString(BarString.CustomizeWindowCaption, "Customization");
AddString(BarString.MenuAnimationSystem, "(System default)");
......
也 就是说,如果你可以重新编译源码,可以直接在这里翻译后编译。当然本人未采用这种方法。但是不同的控件使用的程序文件名不太一致,或是位置不太一致,所以 有的也不太好找,或是不全。后来发现每个Localizer 一般会提供一个CreateDefaultLocalizer.WriteToXml方法,但XtraPivotGrid控件的好象没有,还好又找到一个 Localizer.Active.WriteToXml可能用,效果相同。
试着导出一看是这样的:
<?xml version="1.0" encoding="utf-8"?>
<root>
<data name="BarString.None">
<value>
</value>
</data>
<data name="BarString.PopupMenuEditor">
<value>Popup Menu Editor</value>
</data>
<data name="BarString.AddOrRemove">
<value>&Add or Remove Buttons</value>
</data>
<data name="BarString.ResetBar">
<value>Are you sure you want to reset the changes made to the '{0}' toolbar?</value>
</data>
<data name="BarString.ResetBarCaption">
<value>Customize</value>
</data>
<data name="BarString.ResetButton">
<value>&Reset Toolbar</value>
</data>
<data name="BarString.CustomizeButton">
<value>&Customize ...</value>
</data>
......
用这个方法导出所有字符串枚举名称及值到.XML文件中。
三、翻译。上面的内容有了,剩下就是翻译了,我相信大家都会想,不会这么全都重新开始吧,当然,原来可用资源应当用上才对。如官方汉化。我是这样做的,如下:
(1)在数据库建立一个表,包括name,EN(原文,英语),CN(中文简体),TW(中文繁体)等字段。将上述XML文件内容全部导入。
(2)编写一段代码。将官方汉化的资源文件的内容取出(当然是不全啦)。我写的是VB代码,谨供参考啦(如有需要C#请自己改一下吧)。
Private Sub SimpleButton1_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton1.Click
OpenFileDialog1.Filter = "resx文件|*.resx"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
resxfilename = OpenFileDialog1.FileName
End If
rsxr = New ResXResourceReader(resxfilename)
If Not IsNothing(rsxr) Then
resxdt = getDTread("SELECT * FROM Localizer WHERE name LIKE '" & TextEdit1.Text.Trim & "%' AND CN='' ORDER BY ID")
‘getDTread是读取表过程。
Dim d As DictionaryEntry
Dim s As Integer = 0
Dim dtrow() As DataRow
Dim cr As Integer = -1
MemoEdit1.Text = ""
Dim str As String = ""
For Each d In rsxr
If InStr(d.Key.ToString, TextEdit1.Text.Trim) > 0 Then
dtrow = resxdt.Select("name ='" & d.Key.ToString & "'")
If dtrow.Length > 0 Then
cr = resxdt.Rows.IndexOf(dtrow(0))
If cr >= 0 Then
resxdt.Rows(cr).Item("CN") = d.Value.ToString()
s += 1
str += d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString() + Chr(13) + Chr(10)
End If
End If
End If
Next d
MemoEdit1.Text = str
If s > 0 Then
getDTsave("Localizer", 1, resxdt.GetChanges)
‘getDTsave是提交表过程
End If
XXshow("共" & s.ToString.Trim & "行", Me)
MemoEdit1.Focus()
End If
rsxr.Close()
End Sub
在 打开文件对话框中找到官方汉化包中的LocalizationRes.zh-CHS.resx 文件,把已经有的汉化内容存入数据库表中。可以同时将繁体中文(LocalizationRes.zh-CHT.resx)也取出存入,便于以后使用或用 来参照,并且它还比简体中文内容多一些,但也缺很多。
这样处理之后,完成了一半左右。
(3)查询未经翻译的记录并导入到EXCEL文件。这样在参考繁体时可以块复制再简繁体转换,同时利于使用使用bing翻译,方法是将英文列块复制到WORD文档中,然使用WORD的全文翻译,之后在网页复制结果可成块粘贴到EXCEL中,之后再修改。
四、对象汉化代码。编写下列代码(示例)。重写GetLocalizedString方法。
Imports System.Resources
Imports System.Reflection
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Localization
Public Class XLocallizerBar
Inherits DevExpress.XtraBars.Localization.BarLocalizer
Public Overrides ReadOnly Property Language() As String
Get
Return "Chinese"
End Get
End Property
Public Overrides Function GetLocalizedString (ByVal id As DevExpress.XtraBars.Localization.BarString) As String
Dim ret As String = ""
Select Case id
Case BarString.AddOrRemove : Return "添加或删除按钮(&A)" '&Add or Remove Buttons
Case BarString.BarAllItems : Return "(所有项)" '(All Items)
Case BarString.BarUnassignedItems : Return "(未设定项)" '(Unassigned Items)
Case BarString.CloseButton : Return "关闭" 'Close
Case BarString.CustomizeButton : Return "自定义(&C)..." '&Customize ...
Case BarString.CustomizeRibbon : Return "自定义功能区(&R)..." 'Customize the &Ribbon...
Case BarString.CustomizeWindowCaption : Return "自定义" 'Customization
Case BarString.HelpButton : Return "帮助" 'Help
Case BarString.MaximizeButton : Return "最大化" 'Maximize
Case BarString.RenameToolbarCaption : Return "重命名工具栏" 'Rename Toolbar
Case BarString.ResetBarCaption : Return "自定义" 'Customize
......
Case Else
ret = id.ToString
End Select
Return ret
End Function
End Class
其中的CASE语句从数据库查询自动生成,生成时带上原文,便于以后修改:
SELECT 'CASE '+name+': RETURN "' + CN + '" '''+EN as case语句
FROM Localizer where name LIKE 'BarString%'
ORDER BY name;
五、完成Localizer的替换。在主程序中加入下列代码:
DevExpress.Accessibility.AccLocalizer.Active = New XLocallizerAcc
DevExpress.XtraBars.Localization.BarLocalizer.Active = New XLocallizerBar
DevExpress.XtraCharts.Localization.ChartLocalizer.Active = New XLocallizerCharts
DevExpress.XtraEditors.Controls.Localizer.Active = New XLocallizerEditors
DevExpress.XtraGauges.Core.Localization.GaugesCoreLocalizer.Active = New XLocallizerGauges
DevExpress.XtraGrid.Localization.GridLocalizer.Active = New XLocallizerGrid
DevExpress.XtraLayout.Localization.LayoutLocalizer.Active = New XLocallizerLayout
DevExpress.XtraNavBar.NavBarLocalizer.Active = New XLocallizerNavBar
DevExpress .XtraPivotGrid.Localization.PivotGridLocalizer.Active = New XLocallizerPivotGrid
DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = New XLocallizerPrinting
DevExpress.XtraReports.Localization.ReportLocalizer.Active = New XLocallizerReports
DevExpress.XtraRichEdit.Localization.XtraRichEditLocalizer.Active = New XLocallizerRichEdit
DevExpress.XtraRichEdit.Localization.RichEditExtensionsLocalizer.Active = New XLocallizerRichEditExtensionsjsonStore加载远程和本地数据实例
DevExpress.XtraScheduler.Localization.SchedulerLocalizer.Active = New XLocallizerScheduler
DevExpress.XtraScheduler.Localization.SchedulerExtensionsLocalizer.Active = New XLocallizerSchedulerExtensions
DevExpress.XtraSpellChecker.Localization.SpellCheckerLocalizer.Active = New XLocallizerSpellChecker
DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active = New XLocallizerTreeList
DevExpress.XtraVerticalGrid.Localization.VGridLocalizer.Active = New XLocallizerVerticalGrid
DevExpress.XtraWizard.Localization.WizardLocalizer.Active = New XLocallizerWizard
总 结。这种看上去麻烦,实际比较简单实用,后面发现错误时,可在源代码中搜索并替换,比较方便。当然其他方法也可以使用。由于本人是开发工程中完成的,代码 也比较长,所以暂时不能提供完整的程序包,如果网友有需要,请在留言中留下QQ号码,可以提供源码。文中有哪些错漏,亦请各位指正。
在DEVEXPRESS使用中,发现功能确实不错,便运行速度还是很慢,不知哪位有好的办法吗?
推荐阅读
-
Winform中双击DevExpress的TreeList的树形节点怎样获取当前节点
-
C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法
-
Winform中对DevExpress的RadopGroup的Description、Value、Tag、Text的理解与使用
-
WinForm遍历窗体所有子控件的方法
-
修改Devexpress DateEdit控件默认的日期格式和日历风格
-
一、winForm-DataGridView操作——控件绑定事件的两种方法
-
Winform中设置和获取DevExpress的RadioGroup的选中项的value值
-
winform子窗口调用父窗口的控件及方法-一般调用
-
DevExpress+Winform(四)
-
Winform中通过代码设置DevExpress的TextEdit的类型为Numbernic