Back to Devexpress

Localizing WPF Controls with Localizer Objects

wpf-7543-localization-localizing-wpf-controls-via-localizer-objects.md

latest15.4 KB
Original Source

Localizing WPF Controls with Localizer Objects

  • Oct 01, 2024
  • 5 minutes to read

Each DevExpress component or library has a specific Localizer class (see the table below) that contains localized strings. For example, in the Data Grid control, this is the GridControlLocalizer class.

Note

Important : Not all strings can be translated via Localizer classes. Some components contain form resources (e.g., the XtraReports Search dialog), and the only way to translate them is to create satellite assemblies. Thus, localization via resources is the preferable solution.

Localization Process

Use custom localizers to provide translated strings for localization as follows:

  • Create a descendant of a corresponding Localizer class and override either the GetLocalizedString method (which returns strings for specific string resource identifiers) or PopulateStringTable method (which adds strings for specified resource identifiers).
  • To use this localizer, assign an instance of this class to the static Active property of the localizer class from which you inherited.

The following table lists Localizer classes and Resource String enumerations for DevExpress WPF products:

|

Product

|

Localizer Class

|

Resource String Enumeration

|

Namespace

| | --- | --- | --- | --- | |

Data Grid

|

GridControlLocalizer

|

GridControlStringId

|

DevExpress.Xpf.Grid

| |

Property Grid

|

DevExpress.Xpf.PropertyGrid.PropertyGridControlLocalizer

|

DevExpress.Xpf.PropertyGrid.PropertyGridControlStringID

|

DevExpress.Xpf.PropertyGrid

| |

Gantt Control

|

DevExpress.Xpf.Gantt.Localization.GanttControlLocalizer

|

DevExpress.Xpf.Gantt.Localization.GanttControlStringId

|

DevExpress.Xpf.Gantt.Localization

| |

Dock and Layout Manager

|

DockingLocalizer

|

DevExpress.Xpf.Docking.Base.DockingStringId

|

DevExpress.Xpf.Docking.Base

| |

Diagram Control

|

DiagramControlLocalizer

|

DevExpress.Diagram.Core.Localization.DiagramControlStringId

|

DevExpress.Xpf.Diagram

| |

Bars / Ribbon

|

BarsLocalizer

|

DevExpress.Xpf.Bars.BarsStringId

|

DevExpress.Xpf.Bars

| |

Charts Suite

|

ChartLocalizer

|

ChartStringId

|

DevExpress.Xpf.Charts.Localization

| |

Pivot Grid

|

PivotGridLocalizer

|

PivotGridStringId

|

DevExpress.XtraPivotGrid.Localization

| |

Scheduler

|

DevExpress.Xpf.Scheduling.Common.SchedulerLocalizer

|

DevExpress.Xpf.Scheduling.Common.SchedulerStringId

|

DevExpress.Xpf.Scheduling.Common

| |

WPF Rich Text Editor

|

DevExpress.Xpf.RichEdit.Localization.XpfRichEditLocalizer

|

DevExpress.Xpf.RichEdit.RichEditControl.StringId

|

DevExpress.Xpf.RichEdit.Localization

DevExpress.Xpf.RichEdit

| |

WPF Spreadsheet

|

DevExpress.Xpf.Spreadsheet.Localization.XpfSpreadsheetLocalizer

|

DevExpress.Xpf.Spreadsheet.SpreadsheetControlStringId

|

DevExpress.Xpf.Spreadsheet.Localization

DevExpress.Xpf.Spreadsheet

| |

Gauge Controls

|

GaugeLocalizer

|

DevExpress.Xpf.Gauges.Localization.GaugeStringId

|

DevExpress.Xpf.Gauges.Localization

| |

Printing-Exporting

|

PrintingLocalizer

PreviewLocalizer

|

PrintingStringId

PreviewStringId

|

DevExpress.Xpf.Printing

DevExpress.XtraPrinting.Localization

| |

End-User Report Designer for WPF

|

ReportDesignerLocalizer

|

ReportDesignerStringId

|

DevExpress.Xpf.Reports.UserDesigner.Localization

| |

Data Editors

|

EditorLocalizer

|

EditorStringId

|

DevExpress.Xpf.Editors

| |

Navigation Bar

|

NavBarLocalizer

|

NavBarStringId

|

DevExpress.Xpf.NavBar

| |

PDF Viewer for WPF

|

PdfViewerLocalizer

|

PdfViewerStringId

|

DevExpress.Xpf.PdfViewer

| |

Spell Checker

|

SpellCheckerLocalizer

|

DevExpress.XtraSpellChecker.Localization.SpellCheckerStringId

|

DevExpress.XtraSpellChecker.Localization

| |

Wizard Control

|

DevExpress.Xpf.Controls.WizardLocalizer

|

DevExpress.Xpf.Controls.WizardLocalizerStringId

|

DevExpress.Xpf.Controls

| |

Hamburger Menu

|

DevExpress.Xpf.WindowsUI.HamburgerMenuLocalizer

|

DevExpress.Xpf.WindowsUI.HamburgerMenuStringId

|

DevExpress.Xpf.WindowsUI

| |

Conditional Formatting

|

DevExpress.Xpf.Core.ConditionalFormatting.ConditionalFormattingLocalizer

|

DevExpress.Xpf.Core.ConditionalFormatting.ConditionalFormattingStringId

|

DevExpress.Xpf.Core.ConditionalFormatting

|

Examples

Find Localization Strings to Override

The following code sample demonstrates how to handle the XtraLocalizer.QueryLocalizedString event to display localization string ids instead of localized strings:

csharp
public partial class MainWindow {
    public MainWindow() {
        InitializeComponent();
        DevExpress.Utils.Localization.XtraLocalizer.QueryLocalizedString += XtraLocalizer_QueryLocalizedString;
    }
    void XtraLocalizer_QueryLocalizedString(object sender, XtraLocalizer.QueryLocalizedStringEventArgs e) {
        e.Value = $"{e.StringIDType.Name}.{e.StringID}";
    }
}
vb
Public Partial Class MainWindow
    Public Sub New()
        InitializeComponent()
        DevExpress.Utils.Localization.XtraLocalizer.QueryLocalizedString += AddressOf XtraLocalizer_QueryLocalizedString
    End Sub

    Private Sub XtraLocalizer_QueryLocalizedString(ByVal sender As Object, ByVal e As XtraLocalizer.QueryLocalizedStringEventArgs)
        e.Value = $"{e.StringIDType.Name}.{e.StringID}"
    End Sub
End Class

Use GridControlLocalizer to Localize the Grid

This example uses the GridControlLocalizer to replace the following GridControl‘s strings:

  • Customize… → Customize Totals
  • Totals for ‘Column Name’ → Totals Editor
  • Items → Summary Items

View Example: Use the GridControlLocalizer Class to Localize the Grid

csharp
public partial class MainWindow : Window {
    public MainWindow() {
        // ...
    }
    static MainWindow() {
        GridControlLocalizer.Active = new CustomDXGridLocalizer();
    }
}
public class CustomDXGridLocalizer : GridControlLocalizer {
    protected override void PopulateStringTable() {
        base.PopulateStringTable();
        // Changes the caption of the menu item used to invoke the Total Summary Editor.
        AddString(GridControlStringId.MenuFooterCustomize, "Customize Totals");

        // Changes the Total Summary Editor's default caption.
        AddString(GridControlStringId.TotalSummaryEditorFormCaption, "Totals Editor");

        // Changes the default caption of the tab page that lists total summary items.
        AddString(GridControlStringId.SummaryEditorFormItemsTabCaption, "Summary Items");
    }
}
vb
Public Partial Class MainWindow
    Inherits Window

    Public Sub New()
    ' ...
    End Sub

    Private Shared Sub New()
        GridControlLocalizer.Active = New CustomDXGridLocalizer()
    End Sub
End Class

Public Class CustomDXGridLocalizer
    Inherits GridControlLocalizer

    Protected Overrides Sub PopulateStringTable()
        MyBase.PopulateStringTable()
        ' Changes the caption of the menu item used to invoke the Total Summary Editor.
        AddString(GridControlStringId.MenuFooterCustomize, "Customize Totals")

        ' Changes the Total Summary Editor's default caption.
        AddString(GridControlStringId.TotalSummaryEditorFormCaption, "Totals Editor")

        ' Changes the default caption of the tab page that lists total summary items.
        AddString(GridControlStringId.SummaryEditorFormItemsTabCaption, "Summary Items")
    End Sub
End Class

Change Localization Strings and Keep the Resource Localization

If you wish to change localized strings obtained from Satellite Resource Assemblies, use *ResXLocalizer counterparts for localizer objects listed in the table above. For example, use the GridControlResXLocalizer object instead of GridControlLocalizer.

The following code sample demonstrates how to specify custom localized strings with the Japanese localization applied:

csharp
public partial class MainWindow {
    public MainWindow() {
        // ...
    }
    static MainWindow() {
         GridControlLocalizer.Active = new CustomDXGridLocalizer();
    }
}
public class CustomDXGridLocalizer : GridControlResXLocalizer {
    protected override void PopulateStringTable() {
        base.PopulateStringTable();
        AddString(GridControlStringId.MenuFooterSum, "合計");
        AddString(GridControlStringId.MenuFooterMin, "最小値");
        AddString(GridControlStringId.MenuFooterMax, "最大値");
    }
}
vb
Public Partial Class MainWindow
    Public Sub New()
        ' ...
    End Sub

    Private Shared Sub New()
        GridControlLocalizer.Active = New CustomDXGridLocalizer()
    End Sub
End Class

Public Class CustomDXGridLocalizer
    Inherits GridControlResXLocalizer

    Protected Overrides Sub PopulateStringTable()
        MyBase.PopulateStringTable()
        AddString(GridControlStringId.MenuFooterSum, "合計")
        AddString(GridControlStringId.MenuFooterMin, "最小値")
        AddString(GridControlStringId.MenuFooterMax, "最大値")
    End Sub
End Class

Change GridControl Strings at Runtime

This example shows how to change the default string displayed in the Group Panel at runtime.

View Example: Localize Individual Strings at Runtime

xaml
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew">
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view" AutoWidth="True"/>
    </dxg:GridControl.View>
</dxg:GridControl>
<StackPanel Grid.Row="1" Orientation="Horizontal">
    <TextBox x:Name="textBox" Width="200" KeyDown="textBox_KeyDown" Margin="3,3,3,3"/>
    <Button Content="Apply" Click="button_Click" Margin="0,3,0,3"/>
</StackPanel>
csharp
void LocalizeGroupPanelText() {
    var NewText = textBox.Text;
    var localization = new GridRuntimeStringCollection();
    localization.Add(new RuntimeStringIdInfo(GridControlRuntimeStringId.GridGroupPanelText, NewText));
    view.RuntimeLocalizationStrings = localization;
}
void button_Click(object sender, RoutedEventArgs e) {
    LocalizeGroupPanelText();
}

void textBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) {
    if (e.Key == System.Windows.Input.Key.Enter) {
        LocalizeGroupPanelText();
    }
}
vb
Private Sub LocalizeGroupPanelText()
    Dim NewText = textBox.Text
    Dim localization = New GridRuntimeStringCollection()
    localization.Add(New RuntimeStringIdInfo(GridControlRuntimeStringId.GridGroupPanelText, NewText))
    view.RuntimeLocalizationStrings = localization
End Sub

Private Sub button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    LocalizeGroupPanelText()
End Sub

Private Sub textBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs)
    If e.Key = System.Windows.Input.Key.Enter Then
        LocalizeGroupPanelText()
    End If
End Sub