Localizer Objects
- 5 minutes to read
DevExpress controls and libraries have Localizer classes (see the table below) that provide resource strings for UI elements and allow you to override these strings at runtime.
Localization Process
To localize a control or library via a localizer object, follow the steps below.
- Create a descendant of the corresponding Localizer class and override its GetLocalizedString(T) method, which should return strings for specific string resource identifiers.
Define the Activate method, which creates an instance of your custom localizer and a localizer provider. Set this provider as the active provider for the localizer.
using DevExpress.Web.Localization; using DevExpress.Utils.Localization.Internal; ... public class MyEditorsLocalizer : ASPxEditorsLocalizer { public static void Activate() { MyEditorsLocalizer localizer = new MyEditorsLocalizer(); DefaultActiveLocalizerProvider<ASPxEditorsStringId> provider = new DefaultActiveLocalizerProvider<ASPxEditorsStringId>(localizer); MyEditorsLocalizer.SetActiveLocalizerProvider(provider); } public override string GetLocalizedString(ASPxEditorsStringId id) {...} }
Call the Activate method in the Application_Start event handler within the Global.asax file.
Note
If you localize your application via satellite resource assemblies or global resources, use the Resource Localizer class (see the table below) for runtime localization. This class provides the same functionality as the Localizer class, but contains localized resource string values.
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 resource assemblies.
Localization Classes
The table below lists the following classes related to localization functionality:
- Localizer Class allows you to access default (en) culture resource string values and override them at runtime.
- Resource Localizer Class allows you to access localized resource string values and override them at runtime.
- Resource String Enumeration contains strings that can be localized for the control or library.
Product | Localizer Class | Resource Localizer Class | Resource String Enumeration |
---|---|---|---|
ChartStringId | |||
GaugesCoreStringId | |||
Example
The following example demonstrates how to localize the ASPxGridView control via a custom localizer.
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" KeyFieldName="ID"
OnRowDeleting="ASPxGridView1_RowDeleting" OnRowInserting="ASPxGridView1_RowInserting"
OnRowUpdating="ASPxGridView1_RowUpdating" Width="420px">
<Columns>
<dxwgv:GridViewCommandColumn ShowEditButton="true" ShowClearFilterButton="true"
ShowNewButtonInHeader="true" ShowDeleteButton="true" VisibleIndex="0" />
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="1" Width="150px" />
</Columns>
<Settings ShowFilterBar="Visible" ShowFilterRow="True" ShowGroupPanel="True" />
</dxwgv:ASPxGridView>
using DevExpress.Utils.Localization.Internal;
using DevExpress.Web.Localization;
namespace CustomLocalizer {
public class MyGridLocalizer : ASPxGridViewLocalizer {
public static void Activate() {
MyGridLocalizer localizer = new MyGridLocalizer();
DefaultActiveLocalizerProvider<ASPxGridViewStringId> provider =
new DefaultActiveLocalizerProvider<ASPxGridViewStringId>(localizer);
MyGridLocalizer.SetActiveLocalizerProvider(provider);
}
public override string GetLocalizedString(ASPxGridViewStringId id) {
if (id == ASPxGridViewStringId.GroupPanel)
return "Testing: group panel caption";
string result = base.GetLocalizedString(id);
return string.Format("grid:{0}", result);
}
}
public class MyEditorsLocalizer : ASPxEditorsLocalizer {
public static void Activate() {
MyEditorsLocalizer localizer = new MyEditorsLocalizer();
DefaultActiveLocalizerProvider<ASPxEditorsStringId> provider =
new DefaultActiveLocalizerProvider<ASPxEditorsStringId>(localizer);
MyEditorsLocalizer.SetActiveLocalizerProvider(provider);
}
public override string GetLocalizedString(ASPxEditorsStringId id) {
string result = base.GetLocalizedString(id);
return string.Format("editors:{0}", result);
}
}
public class MyWebLocalizer : ASPxperienceLocalizer {
public static void Activate() {
MyWebLocalizer localizer = new MyWebLocalizer();
DefaultActiveLocalizerProvider<ASPxperienceStringId> provider =
new DefaultActiveLocalizerProvider<ASPxperienceStringId>(localizer);
MyWebLocalizer.SetActiveLocalizerProvider(provider);
}
public override string GetLocalizedString(ASPxperienceStringId id) {
string result = base.GetLocalizedString(id);
return string.Format("web:{0}", result);
}
}
}