Each DevExpress component or library has a specific Localizer class that provides localized strings (see the table below). For example, in the Data Grid control, this is the ASPxGridViewLocalizer class.
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
The localization process via custom localizers is as follows.
- Create a descendant of a corresponding Localizer class and override its GetLocalizedString method, which should return strings for specific string resource identifiers.
Define the Activate method, which creates an instance of your custom localizer with a localizer provider, and sets this provider as an 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) {...}
}
Imports DevExpress.Web.Localization
Imports DevExpress.Utils.Localization.Internal
...
Public Class MyEditorsLocalizer
Inherits ASPxEditorsLocalizer
Public Shared Sub Activate()
Dim localizer As New MyEditorsLocalizer()
Dim provider As New DefaultActiveLocalizerProvider(Of ASPxEditorsStringId)(localizer)
MyEditorsLocalizer.SetActiveLocalizerProvider(provider)
End Sub
Public Overrides Function GetLocalizedString(ByVal id As ASPxEditorsStringId) As String
...
End Function
End Class
Call the Activate method in the Application_Start event handler within the Global.asax file.
protected void Application_Start(object sender, EventArgs e) {
MyEditorsLocalizer.Activate();
}
Protected Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
MyEditorsLocalizer.Activate()
End Sub
The following table lists Localizer classes and Resource String enumerations for DevExpress ASP.NET controls.
Example
The following example demonstrates how to localize the ASPxGridView control via a custom localizer. For another localization approach, refer to Localizing ASP.NET Controls via Satellite Resource Assemblies.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CustomLocalizer._Default" %>
<%@ Register Assembly="DevExpress.Web.v15.2, Version=15.2.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v15.2, Version=15.2.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>How to use a custom ASPxGridView localizer</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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:GridViewCommandColumn>
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="1" Width="150px">
</dxwgv:GridViewDataTextColumn>
</Columns>
<Settings ShowFilterBar="Visible" ShowFilterRow="True" ShowGroupPanel="True" />
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
using DevExpress.Utils.Localization.Internal;
using DevExpress.Web.Localization;
namespace CustomLocalizer {
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);
}
}
}
using System;
using System.Data;
namespace CustomLocalizer {
public partial class _Default : System.Web.UI.Page {
protected void Page_Init(object sender, EventArgs e) {
ASPxGridView1.DataSource = GetDemoData();
}
protected void Page_Load(object sender, EventArgs e) {
ASPxGridView1.DataBind();
}
private DataTable GetDemoData() {
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Rows.Add(1);
return table;
}
protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
CancelEditing(e);
}
protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
CancelEditing(e);
}
protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
CancelEditing(e);
}
private void CancelEditing(System.ComponentModel.CancelEventArgs e) {
e.Cancel = true;
ASPxGridView1.CancelEdit();
}
}
}
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);
}
}
}
using DevExpress.Utils.Localization.Internal;
using DevExpress.Web.Localization;
namespace CustomLocalizer {
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);
}
}
}
Imports DevExpress.Web.Localization
Imports DevExpress.Utils.Localization.Internal
Namespace CustomLocalizer
Public Class MyWebLocalizer
Inherits ASPxperienceLocalizer
Public Shared Sub Activate()
Dim localizer As New MyWebLocalizer()
Dim provider As DefaultActiveLocalizerProvider(Of ASPxperienceStringId) = _
New DefaultActiveLocalizerProvider(Of ASPxperienceStringId)(localizer)
MyWebLocalizer.SetActiveLocalizerProvider(provider)
End Sub
Public Overrides Function GetLocalizedString(ByVal id As ASPxperienceStringId) As String
Dim result As String = MyBase.GetLocalizedString(id)
Return String.Format("web:{0}", result)
End Function
End Class
End Namespace
Imports DevExpress.Web.Localization
Imports DevExpress.Utils.Localization.Internal
Namespace CustomLocalizer
Public Class MyGridLocalizer
Inherits ASPxGridViewLocalizer
Public Shared Sub Activate()
Dim localizer As New MyGridLocalizer()
Dim provider As DefaultActiveLocalizerProvider(Of ASPxGridViewStringId) = _
New DefaultActiveLocalizerProvider(Of ASPxGridViewStringId)(localizer)
MyGridLocalizer.SetActiveLocalizerProvider(provider)
End Sub
Public Overrides Function GetLocalizedString(ByVal id As ASPxGridViewStringId) As String
If id = ASPxGridViewStringId.GroupPanel Then
Return "Testing: group panel caption"
End If
Dim result As String = MyBase.GetLocalizedString(id)
Return String.Format("grid:{0}", result)
End Function
End Class
End Namespace
Imports DevExpress.Web.Localization
Imports DevExpress.Utils.Localization.Internal
Namespace CustomLocalizer
Public Class MyEditorsLocalizer
Inherits ASPxEditorsLocalizer
Public Shared Sub Activate()
Dim localizer As New MyEditorsLocalizer()
Dim provider As DefaultActiveLocalizerProvider(Of ASPxEditorsStringId) = _
New DefaultActiveLocalizerProvider(Of ASPxEditorsStringId)(localizer)
MyEditorsLocalizer.SetActiveLocalizerProvider(provider)
End Sub
Public Overrides Function GetLocalizedString(ByVal id As ASPxEditorsStringId) As String
Dim result As String = MyBase.GetLocalizedString(id)
Return String.Format("editors:{0}", result)
End Function
End Class
End Namespace
<%@ Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="CustomLocalizer._Default" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.10.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v15.1, Version=15.1.10.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web" TagPrefix="dxwgv" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to use a custom ASPxGridView localizer</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" Theme="Mulberry"
AutoGenerateColumns="False" KeyFieldName="ID" Width="420px"
OnRowDeleting="ASPxGridView1_RowDeleting"
OnRowInserting="ASPxGridView1_RowInserting"
OnRowUpdating="ASPxGridView1_RowUpdating">
<Columns>
<dxwgv:GridViewCommandColumn ShowNewButtonInHeader="true" ShowDeleteButton="true" ShowClearFilterButton="true" ShowEditButton="true" VisibleIndex="0">
</dxwgv:GridViewCommandColumn>
<dxwgv:GridViewDataTextColumn FieldName="ID" VisibleIndex="1" Width="150px">
</dxwgv:GridViewDataTextColumn>
</Columns>
<Settings ShowFilterBar="Visible" ShowFilterRow="True" ShowGroupPanel="True" />
</dxwgv:ASPxGridView>
</div>
</form>
</body>
</html>
Imports System
Imports System.Data
Namespace CustomLocalizer
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
ASPxGridView1.DataSource = GetDemoData()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
ASPxGridView1.DataBind()
End Sub
Private Function GetDemoData() As DataTable
Dim table As New DataTable()
table.Columns.Add("ID", GetType(Integer))
table.Rows.Add(1)
Return table
End Function
Protected Sub ASPxGridView1_RowInserting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInsertingEventArgs)
CancelEditing(e)
End Sub
Protected Sub ASPxGridView1_RowUpdating(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataUpdatingEventArgs)
CancelEditing(e)
End Sub
Protected Sub ASPxGridView1_RowDeleting(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataDeletingEventArgs)
CancelEditing(e)
End Sub
Private Sub CancelEditing(ByVal e As System.ComponentModel.CancelEventArgs)
e.Cancel = True
ASPxGridView1.CancelEdit()
End Sub
End Class
End Namespace