Localizer Objects
- 4 minutes to read
DevExpress ASP.NET MVC extensions use Localizer objects to obtain strings for UI elements. You can use the following technique to override these strings at runtime:
- Use the global XtraLocalizer class to localize all DevExpress extensions.
- Implement a custom localizer for a specific UI extension.
The table below list differences in this techniques.
XtraLocalizer Technique | Custom Localizer Technique |
---|---|
Specify strings for different products in a single event. | Create a custom localizer for every localized product. |
Localize form elements. | Form elements cannot be localized. |
Get strings that require translation in your application. | You cannot get untranslated strings. |
Use XtraLocalizer to Localize DevExpress Extensions
The XtraLocalizer class implements a localization-related API that allows you to translate DevExpress UI extensions and forms into different languages and develop multicultural enterprise applications.
- The QueryLocalizedString and QueryLocalizedStringContainerResource events allow you to localize resources for all DevExpress UI extensions and their built-in data forms in your application. These events fire when a control or data form requests a resource string. Handle these events to translate or modify resource strings as needed.
- The QueryLocalizedStringNonTranslated allows you to focus on the strings that require translation in your application. Handle this event to collect non-localized resource strings for translation.
using DevExpress.Web.Localization;
using DevExpress.Utils.Localization.Internal;
using DevExpress.Utils.Localization;
// ...
XtraLocalizer.QueryLocalizedString +=
new EventHandler<XtraLocalizer.QueryLocalizedStringEventArgs>(XtraLocalizer_QueryLocalizedString);
// ...
static private void XtraLocalizer_QueryLocalizedString(object sender, XtraLocalizer.QueryLocalizedStringEventArgs e) {
if (e.StringIDType == typeof(ASPxEditorsStringId)) {
if ((ASPxEditorsStringId)e.StringID == ASPxEditorsStringId.Calendar_Today)
e.Value = "Select Today";
if ((ASPxEditorsStringId)e.StringID == ASPxEditorsStringId.Calendar_Clear)
e.Value = "Clear Selection";
}
}
Implement a Custom Localizer
Follow the steps below to create a custom localizer:
Create a descendant of a Localizer class that corresponds to the control. See the table below for a lists of localizer classes.
- 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.
Note, if you localize your application via satellite resource assemblies or global resources, use the Resource Localizer class for runtime localization. This class provides the same functionality as the Localizer class, but contains localized resource string values.
Product
Localizer Class
Resource Localizer Class
Resource String Enumeration
ChartStringId
Gauges
GaugesCoreStringId
Override the GetLocalizedString(T) method to return localized strings for specific resource identifiers.
Implement a method (
Activate
in the code sample below) that creates an instance of your custom localizer and a localizer provider. Set this provider as active for the localizer.using DevExpress.Web.Localization; using DevExpress.Utils.Localization.Internal; ... public class MyEditorsLocalizer : ASPxEditorsLocalizer { public override string GetLocalizedString(ASPxEditorsStringId id) { if (id == ASPxEditorsStringId.Calendar_Today) return "Select Today"; if (id == ASPxEditorsStringId.Calendar_Clear) return "Clear Selection"; return base.GetLocalizedString(id); } public static void Activate() { MyEditorsLocalizer localizer = new MyEditorsLocalizer(); DefaultActiveLocalizerProvider<ASPxEditorsStringId> provider = new DefaultActiveLocalizerProvider<ASPxEditorsStringId>(localizer); MyEditorsLocalizer.SetActiveLocalizerProvider(provider); } }
Call the implemented method in the
Application_Start
event handler in the Global.asax file.
Important
Form resources, such as the XtraReports Search dialog, cannot be translated via Localizer classes. Use XtraLocalizer class or satellite resource assemblies techniques to translate form resources.