Localizing WinForms Controls with Localizer Objects
- 6 minutes to read
DevExpress WinForms UI controls use Localizer objects to obtain strings for UI elements (for instance, the Data Grid uses GridLocalizer). Localization-related APIs (Localizer objects) take priority over satellite resource assemblies.
Use one of the following methods to modify resources:
- Implement a custom localizer for a specific UI control.
- Use the global
XtraLocalizer
class to localize all DevExpress UI controls.
Note
DevExpress UI controls support two methods of localization: Satellite Resource Assemblies and Localization-Related API (Custom Localizers). There are several UI controls that can only be localized with satellite resource assemblies (for example, the Search dialog in XtraReports
).
Custom Localizers
Follow the steps below to create a custom localizer:
- Create a descendant of a Localizer class that corresponds to a UI control. See the following table for information: Default Localizers.
- Override the
GridLocalizer.GetLocalizedString
method to return localized strings for specific resource identifiers.Tip
You can override the
GridLocalizer.PopulateStringTable
method to translate specific resources. - Assign a custom localizer to the
Active
property of the Localizer class from which you inherited.
Example - How to Create Custom Localizers
The following example demonstrates how to create localizers to translate resources (menu items) of the Data Grid and Picture Editor into German:
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.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);
}
}
Assign instances of the GermanGridLocalizer
and GermanEditorsLocalizer
classes to the GridLocalizer.Active
and Localizer.Active
properties before application startup to activate custom localizers.
public Form1() {
GridLocalizer.Active = new GermanGridLocalizer();
Localizer.Active = new GermanEditorsLocalizer();
InitializeComponent();
}
Default Localizers
The following table lists default Localizer classes and corresponding resource string identifiers for DevExpress WinForms UI controls:
XtraLocalizer - Event Based Localization
The XtraLocalizer
class implements the QueryLocalizedString
static event that allows you to localize resources for all DevExpress UI controls in your application. This event fires when the control requests a resource string and allows you to translate or modify it as needed.
Event Arguments
e.StringID
- Gets the resource string requested by the control.e.StringIDType
- Gets the resource string identifier. Use this parameter to get the type of the control that is requesting the resource string.e.Value
- Gets or sets the resource string requested by the control.
Note
The QueryLocalizedString
event is a weak event. You should implement the event handler as a method. Otherwise, the garbage collector can collect a reference to your delegate.
Example
using System;
using System.Windows.Forms;
using DevExpress.Utils.Localization;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Localization;
namespace DXApplication {
internal static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
XtraLocalizer.QueryLocalizedString += XtraLocalizer_QueryLocalizedString;
Application.Run(new Form1());
}
static private void XtraLocalizer_QueryLocalizedString(object sender, XtraLocalizer.QueryLocalizedStringEventArgs e) {
if (e.StringIDType == typeof(GridStringId)) {
if ((GridStringId)e.StringID == GridStringId.GridGroupPanelText)
e.Value = "Gruppenregion";
}
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";
}
}
}
}