Localizing WinForms Controls via Localizer Objects

  • 6 minutes to read

DevExpress WinForms controls obtain the default text for their UI elements via Localizer objects (for instance, Data Grid uses GridLocalizer, Pivot Grid uses PivotGridLocalizer, etc). The following approaches allow you to use localizers to replace the default resources with new text:

  • Create a custom localizer for a specific control/library

  • Use the global XtraLocalizer class which allows you to provide localized text for all DevExpress .NET controls.

NOTE

Not all strings can be translated via Localizers. Specific components contain form resources (for example, the XtraReports Search dialog), which should use satellite assemblies for translation.

Custom Localizers

Use custom localizers to provide localized strings as follows.

  • Create a descendant of a Localizer class that corresponds to a specific control/library. Default Localizer classes are listed below.
  • Override the GetLocalizedString method to return localized strings for specific string resource identifiers

    Alternatively, you can override the PopulateStringTable method to translate resources.

  • Assign an instance of your Localizer class to the static Active property of the Localizer class you inherited from.

Example

The following example demonstrates how to use localizers to translate strings into German:

This example creates localizers for the Data Grid UI and Editors UI. These localizers are GridLocalizer and Localizer class descendants.

cdLocalization_2


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 "";
   }
}

To use these localizers, assign instances of the GermanGridLocalizer and GermanEditorsLocalizer classes to the GridLocalizer.Active and Localizer.Active properties, respectively.

public Form1() {
    GridLocalizer.Active = new GermanGridLocalizer();
    Localizer.Active = new GermanEditorsLocalizer();
    InitializeComponent();
}

Default Localizers

The following table lists the default Localizer classes and corresponding resource string identifiers for DevExpress WinForms products:

Product Localizer Class Resource String Identifiers (Enumeration) Namespace
Chart Control ChartLocalizer ChartStringId DevExpress.XtraCharts.Localization
Data Access Library DataAccessUILocalizer DataAccessUIStringId DevExpress.DataAccess.UI.Localization
Data Grid GridLocalizer GridStringId DevExpress.XtraGrid.Localization
Diagrams DiagramControlLocalizer DevExpress.Diagram.Core.Localization.DiagramControlStringId DevExpress.Diagram.Core.Localization
Editors and Controls Localizer StringId DevExpress.XtraEditors.Controls
Gauges GaugesCoreLocalizer GaugesCoreStringId DevExpress.XtraGauges.Core.Localization
Layout and Data Layout Controls LayoutLocalizer LayoutStringId DevExpress.XtraLayout.Localization
Navigation Bar NavBarLocalizer NavBarStringId DevExpress.XtraNavBar
PDF Viewer PdfCoreLocalizer / XtraPdfViewerLocalizer PdfCoreStringId / XtraPdfViewerStringId DevExpress.Pdf.Localization / DevExpress.XtraPdfViewer.Localization
Pivot Grid PivotGridLocalizer PivotGridStringId DevExpress.XtraPivotGrid.Localization
Printing-Exporting PreviewLocalizer PreviewStringId DevExpress.XtraPrinting.Localization
Reporting ReportLocalizer ReportStringId DevExpress.XtraReports.Localization
Ribbon, Bars and Menu BarLocalizer BarString DevExpress.XtraBars.Localization
Rich Text Editor XtraRichEditLocalizer, RichEditExtensionsLocalizer / OfficeLocalizer XtraRichEditStringId, RichEditExtensionsStringId / OfficeStringId DevExpress.XtraRichEdit.Localization / DevExpress.Office.Localization
Scheduler SchedulerLocalizer SchedulerStringId DevExpress.XtraScheduler.Localization
Snap SnapLocalizer / SnapExtensionsLocalizer/ OfficeLocalizer SnapStringId / SnapExtensionsStringId / OfficeStringId DevExpress.Snap.Localization / DevExpress.Snap.Extensions.Localization / DevExpress.Office.Localization
Spell Checker SpellCheckerLocalizer SpellCheckerStringId DevExpress.XtraSpellChecker.Localization
Spreadsheet XtraSpreadsheetLocalizer, XtraSpreadsheetFunctionNameLocalizer, XtraSpreadsheetCellErrorNameLocalizer / OfficeLocalizer XtraSpreadsheetStringId / XtraSpreadsheetFunctionNameStringId / XtraSpreadsheetCellErrorNameStringId / OfficeStringId DevExpress.XtraSpreadsheet.Localization / DevExpress.Office.Localization
Tree List TreeListLocalizer TreeListStringId DevExpress.XtraTreeList.Localization
Vertical Grid VGridLocalizer VGridStringId DevExpress.XtraVerticalGrid.Localization
Data Filtering FilterUIElementResXLocalizer FilterUIElementLocalizerStringId DevExpress.Ultils.Filtering.Internal
XtraDialog DialogsLocalizer DialogsStringId DevExpress.Dialogs.Core.Localization

XtraLocalizer

The static XtraLocalizer.QueryLocalizedString event allows you to provide localized strings for all DevExpress controls in your application. If your application uses controls from multiple libraries (for example, Data Grid, Editors, Charts, and Pivot Grid), the QueryLocalizedString event fires when these controls request localized strings.

NOTE

Your QueryLocalizedString event handler should be your form's method. Otherwise, the garbage collector can collect a reference to your delegate.

Use the e.StringID event argument to identify a control's currently processed string. This property returns a value of the control's resource string enumeration (see the table above).

Example

The following example handles the XtraLocalizer.QueryLocalizedString event to localize specific strings for the Data Grid UI and Editors UI:

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";
    }
}