通過(guò)Localizer對(duì)象本地化WinForms控件
DevExpress WinForms控件通過(guò) Localizer對(duì)象獲取其UI元素的默認(rèn)文本(例如,Data Grid使用GridLocalizer, PivotGrid使用PivotGridLocalizer等),以下方法允許您使用本地化程序?qū)⒛J(rèn)資源替換為新的文本:
- 為特定控件/庫(kù)創(chuàng)建自定義本地化器
- 使用全局XtraLocalizer類(lèi),它允許您為所有DevExpress .NET控件提供本地化文本。
提示:有些控件只有附屬程序集才能翻譯(例如,XtraReports中的Search對(duì)話(huà)框),我們建議您使用附屬程序集來(lái)翻譯應(yīng)用程序。另外,請(qǐng)注意,如果使用自定義本地化程序,則會(huì)忽略附屬程序集。
自定義本地化
使用自定義本地化程序提供本地化字符串,如下所示。
- 創(chuàng)建與特定控件/庫(kù)對(duì)應(yīng)的本地化程序類(lèi)的后代,下面列出了默認(rèn)的本地化程序類(lèi)。
- 覆蓋GetLocalizedString方法來(lái)返回特定字符串資源標(biāo)識(shí)符的本地化字符串,或者您可以覆蓋PopulateStringTable方法來(lái)轉(zhuǎn)換資源。
- 將本地化程序類(lèi)的實(shí)例分配給您繼承的本地化器類(lèi)的靜態(tài)Active屬性。
示例
下面的例子演示了如何使用本地化程序?qū)⒆址g成 German:
本例為Data Grid UI和Editors UI創(chuàng)建本地化程序,這些本地化程序是GridLocalizer和Localizer類(lèi)的后代。
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類(lèi)的實(shí)例分配給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
默認(rèn)本地化程序
下表列出了DevExpress WinForms產(chǎn)品的默認(rèn)本地化程序類(lèi)和相應(yīng)的資源字符串標(biāo)識(shí)符:
XtraLocalizer
靜態(tài) XtraLocalizer.QueryLocalizedString 事件允許您為應(yīng)用程序中的所有DevExpress控件提供本地化字符串,如果應(yīng)用程序使用來(lái)自多個(gè)庫(kù)的控件(例如,數(shù)據(jù)網(wǎng)格、編輯器、圖表和Pivot網(wǎng)格),則在這些控件請(qǐng)求本地化字符串時(shí)會(huì)觸發(fā)QueryLocalizedString事件。
提示:QueryLocalizedString事件處理程序應(yīng)該是表單的方法,否則垃圾收集器可以收集對(duì)委托的引用。
使用e.StringID事件參數(shù)來(lái)標(biāo)識(shí)控件當(dāng)前處理的字符串,此屬性返回控件的資源字符串枚舉的值(請(qǐng)參見(jiàn)上面的表),事件參數(shù)來(lái)標(biāo)識(shí)控件當(dāng)前處理的字符串,此屬性返回控件的資源字符串枚舉的值(請(qǐng)參見(jiàn)上面的表)。
示例
下面的例子處理 XtraLocalizer.QueryLocalizedString 事件,為數(shù)據(jù)網(wǎng)格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