通过Localizer对象本地化WinForms控件
DevExpress WinForms控件通过 Localizer对象获取其UI元素的默认文本(例如,Data Grid使用, PivotGrid使用等),以下方法允许您使用本地化程序将默认资源替换为新的文本:
- 为特定控件/库创建自定义本地化器
- 使用全局XtraLocalizer类,它允许您为所有DevExpress .NET控件提供本地化文本。
提示:有些控件只有附属程序集才能翻译(例如,XtraReports中的Search对话框),我们建议您使用附属程序集来翻译应用程序。另外,请注意,如果使用自定义本地化程序,则会忽略附属程序集。
自定义本地化
使用自定义本地化程序提供本地化字符串,如下所示。
- 创建与特定控件/库对应的本地化程序类的后代,下面列出了默认的本地化程序类。
- 覆盖GetLocalizedString方法来返回特定字符串资源标识符的本地化字符串,或者您可以覆盖PopulateStringTable方法来转换资源。
- 将本地化程序类的实例分配给您继承的本地化器类的静态Active属性。
示例
下面的例子演示了如何使用本地化程序将字符串翻译成 German:
本例为Data Grid UI和Editors UI创建本地化程序,这些本地化程序是GridLocalizer和类的后代。
C#:
using DevExpress.XtraEditors.Controls; using DevExpress.XtraGrid.Localization; public class GermanGridLocalizer : GridLocalizer { public override string Language { get { return "Deutsch"; } } public override string GetLocalizedString(GridStringId id) { string ret = ""; switch (id) { // ... case GridStringId.GridGroupPanelText: return "Ziehen Sie eine Spaltenüberschrift in diesen Bereich, um nach dieser zu gruppieren"; case GridStringId.MenuColumnClearSorting: return "Sortierung entfernen"; case GridStringId.MenuGroupPanelHide: return "Gruppierungsfeld ausblenden"; case GridStringId.MenuColumnRemoveColumn: return "Spalte entfernen"; case GridStringId.MenuColumnFilterEditor: return "Filter &bearbeiten"; case GridStringId.MenuColumnFindFilterShow: return "Suche einblenden"; case GridStringId.MenuColumnAutoFilterRowShow: return "Zeige Auto Filterzeile"; case GridStringId.MenuColumnSortAscending: return "Aufsteigend sortieren"; case GridStringId.MenuColumnSortDescending: return "Absteigend sortieren"; case GridStringId.MenuColumnGroup: return "Gruppieren fur dieses Feld"; case GridStringId.MenuColumnUnGroup: return "Gruppierung aufheben"; case GridStringId.MenuColumnColumnCustomization: return "Laufzeit benutzerdefinierte Spalte"; case GridStringId.MenuColumnBestFit: return "Optimale Breite"; case GridStringId.MenuColumnFilter: return "Kann gruppieren"; case GridStringId.MenuColumnClearFilter: return "Filter aufheben"; case GridStringId.MenuColumnBestFitAllColumns: return "Optimale Breite (alle Spalten)"; // ... default: ret = base.GetLocalizedString(id); break; } return ret; } } public class GermanEditorsLocalizer : Localizer { public override string Language { get { return "Deutsch"; }} public override string GetLocalizedString(StringId id) { switch(id) { // ... case StringId.NavigatorTextStringFormat: return "Zeile {0} von {1}"; case StringId.PictureEditMenuCut: return "Ausschneiden"; case StringId.PictureEditMenuCopy: return "Kopieren"; case StringId.PictureEditMenuPaste: return "Einfugen"; case StringId.PictureEditMenuDelete: return "Loschen"; case StringId.PictureEditMenuLoad: return "Laden"; case StringId.PictureEditMenuSave: return "Speichern"; // ... } return base.GetLocalizedString(id); } }
VB.NET:
Imports DevExpress.XtraEditors.Controls Imports DevExpress.XtraGrid.Localization Public Class GermanGridLocalizer Inherits GridLocalizer Public Overrides ReadOnly Property Language() As String Get Return "Deutsch" End Get End Property Public Overrides Function GetLocalizedString(id As GridStringId) As String Dim ret As String = "" Select Case id ' ... Case GridStringId.GridGroupPanelText Return "Ziehen Sie eine Spaltenüberschrift in diesen Bereich, um nach dieser zu gruppieren" Case GridStringId.MenuColumnClearSorting Return "Sortierung entfernen" Case GridStringId.MenuGroupPanelHide Return "Gruppierungsfeld ausblenden" Case GridStringId.MenuColumnRemoveColumn Return "Spalte entfernen" Case GridStringId.MenuColumnFilterEditor Return "Filter &bearbeiten" Case GridStringId.MenuColumnFindFilterShow Return "Suche einblenden" Case GridStringId.MenuColumnAutoFilterRowShow Return "Zeige Auto Filterzeile" Case GridStringId.MenuColumnSortAscending Return "Aufsteigend sortieren" Case GridStringId.MenuColumnSortDescending Return "Absteigend sortieren" Case GridStringId.MenuColumnGroup Return "Gruppieren fur dieses Feld" Case GridStringId.MenuColumnUnGroup Return "Gruppierung aufheben" Case GridStringId.MenuColumnColumnCustomization Return "Laufzeit benutzerdefinierte Spalte" Case GridStringId.MenuColumnBestFit Return "Optimale Breite" Case GridStringId.MenuColumnFilter Return "Kann gruppieren" Case GridStringId.MenuColumnClearFilter Return "Filter aufheben" Case GridStringId.MenuColumnBestFitAllColumns Return "Optimale Breite (alle Spalten)" Case Else ' ... ret = MyBase.GetLocalizedString(id) Exit Select End Select Return ret End Function End Class Public Class GermanEditorsLocalizer Inherits Localizer Public Overrides ReadOnly Property Language() As String Get Return "Deutsch" End Get End Property Public Overrides Function GetLocalizedString(ByVal id As StringId) As String Select Case id ' ... Case StringId.NavigatorTextStringFormat : Return "Zeile {0} von {1}" Case StringId.PictureEditMenuCut : Return "Ausschneiden" Case StringId.PictureEditMenuCopy : Return "Kopieren" Case StringId.PictureEditMenuPaste : Return "Einfugen" Case StringId.PictureEditMenuDelete : Return "Loschen" Case StringId.PictureEditMenuLoad : Return "Laden" Case StringId.PictureEditMenuSave : Return "Speichern" ' ... End Select Return MyBase.GetLocalizedString(id) End Function End Class
要使用这些本地化程序,分别将GermanGridLocalizer和GermanEditorsLocalizer类的实例分配给GridLocalizer.Active和Localizer.Active属性。
C#:
public Form1() { GridLocalizer.Active = new GermanGridLocalizer(); Localizer.Active = new GermanEditorsLocalizer(); InitializeComponent(); }
VB.NET:
Public Sub Form1() GridLocalizer.Active = New GermanGridLocalizer() Localizer.Active = New GermanEditorsLocalizer() InitializeComponent() End Sub
默认本地化程序
下表列出了DevExpress WinForms产品的默认本地化程序类和相应的资源字符串标识符:
XtraLocalizer
静态 XtraLocalizer.QueryLocalizedString 事件允许您为应用程序中的所有DevExpress控件提供本地化字符串,如果应用程序使用来自多个库的控件(例如,数据网格、编辑器、图表和Pivot网格),则在这些控件请求本地化字符串时会触发QueryLocalizedString事件。
提示:QueryLocalizedString事件处理程序应该是表单的方法,否则垃圾收集器可以收集对委托的引用。
使用e.StringID事件参数来标识控件当前处理的字符串,此属性返回控件的资源字符串枚举的值(请参见上面的表),事件参数来标识控件当前处理的字符串,此属性返回控件的资源字符串枚举的值(请参见上面的表)。
示例
下面的例子处理 XtraLocalizer.QueryLocalizedString 事件,为数据网格UI和编辑器UI本地化特定的字符串:
C#:
using DevExpress.Utils.Localization; using DevExpress.XtraEditors.Controls; using DevExpress.XtraGrid.Localization; private void Form1_Load(object sender, EventArgs e) { XtraLocalizer.QueryLocalizedString += XtraLocalizer_QueryLocalizedString; } private void XtraLocalizer_QueryLocalizedString(object sender, XtraLocalizer.QueryLocalizedStringEventArgs e) { // Data Grid's UI if (e.StringIDType == typeof(GridStringId)) { if ((GridStringId)e.StringID == GridStringId.GridGroupPanelText) e.Value = "Gruppenregion"; } // Editors UI if (e.StringIDType == typeof(StringId)) { if ((StringId)e.StringID == StringId.PictureEditMenuCut) e.Value = "Ausschneiden"; if ((StringId)e.StringID == StringId.PictureEditMenuCopy) e.Value = "Kopieren"; if ((StringId)e.StringID == StringId.PictureEditMenuPaste) e.Value = "Einfugen"; } }
VB.NET:
Imports DevExpress.Utils.Localization Imports DevExpress.XtraEditors.Controls Imports DevExpress.XtraGrid.Localization Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler XtraLocalizer.QueryLocalizedString, AddressOf XtraLocalizer_QueryLocalizedString End Sub Private Sub XtraLocalizer_QueryLocalizedString(ByVal sender As Object, ByVal e As XtraLocalizer.QueryLocalizedStringEventArgs) ' Data Grid's UI If e.StringIDType Is GetType(GridStringId) Then If CType(e.StringID, GridStringId) = GridStringId.GridGroupPanelText Then e.Value = "Gruppenregion" End If End If ' Editors UI If e.StringIDType Is GetType(StringId) Then If CType(e.StringID, StringId) = StringId.PictureEditMenuCut Then e.Value = "Ausschneiden" End If If CType(e.StringID, StringId) = StringId.PictureEditMenuCopy Then e.Value = "Kopieren" End If If CType(e.StringID, StringId) = StringId.PictureEditMenuPaste Then e.Value = "Einfugen" End If End If End Sub