正确的本地化DevExpress控件的方法

 

Localization is the process of translating an application's resources into localized versions for a specific culture or locale. This allows you to build translated versions of your applications in order to provide a complete native language interface to end users. This article guides you through the entire process of localizing Developer Express .NET products.

This article contains the following sections:

What should be localized?

All Developer Express .NET products have localizable resources that represent various dialog boxes, captions for buttons, menu items, confirmation and error messages, etc. All these resource strings can easily be translated into various languages or replaced by their equivalents. This can be done in one of two ways:

creating satellite resource assemblies;
using a "Localizer" object.

Creating satellite resource assemblies is the standard approach used when creating multi-language applications. Localizing via a "Localizer" is the feature provided by Developer Express which is useful in the following cases:

You are developing an application for a single culture and you want to translate the resources into the appropriate language or you just wish to change the default resources (for English (United States) culture) to their equivalents.
You don't want to use the available satellite assemblies and you don't have the source code to create your own localized assemblies.

The image below shows some editors from the XtraEditors Library localized into English and German.

Creating Satellite Resource Assemblies

This approach to localizing Developer Express .NET products implies creating new satellite assemblies which contain the localized resources translated into the appropriate target languages. This is the more common approach when developing world-ready applications.

Some of our customers have translated our resources into other languages. They've been kind enough to share these translated resources with the community, so you can freely download them here. The download includes resources translated to the following languages:

Chinese, Czech, Danish, Dutch, French, German, Italian, Japanese, Norwegian, Portuguese, Russian, Slovenian, Spanish, Vietnamese.

For other languages you can manually translate the resource strings provided that you have purchased the version of the component which ships with its source code. For detailed information on how to manually translate the resources and use satellite assemblies see the Localization document in the XtraEditors help file and the Localizing Applications document in MSDN Library.

Using a Localizer

This is another way of localizing Developer Express .NET products which allows resources to be localized on the fly.

What Is a Localizer?

A Localizer is a class that provides the means to localize the control's interface elements. Each Developer Express .NET control has its own localizer class, they are listed in the table below:

Control
Class
XtraBars
DevExpress.XtraBars.Localization.BarLocalizer
XtraEditors Library
DevExpress.XtraEditors.Controls.Localizer
XtraGrid
DevExpress.XtraGrid.Localization.GridLocalizer
XtraNavBar
DevExpress.XtraNavBar.NavBarLocalizer
XtraPivotGrid
DevExpress.XtraPivotGrid.Localization.PivotGridLocalizer
XtraPrinting Library
DevExpress.XtraPrinting.Localization.PreviewLocalizer
XtraReports
DevExpress.XtraReports.Localization.ReportLocalizer
XtraScheduler
DevExpress.XtraScheduler.Localization.SchedulerLocalizer
XtraTreeList
DevExpress.XtraTreeList.Localization.TreeListLocalizer
XtraVerticalGrid
DevExpress.XtraVerticalGrid.Localization.VGridLocalizer

The process of localizing is rather simple. First you should create a descendant of the appropriate localizer and override its GetLocalizedString method in order to translate or modify the control's resource strings. After the localizer descendant has been created, you must instantiate it and assign it to the static Active property of the localizer class you derived it from.

Examples

The following example demonstrates how to localize the XtraGrid's and XtraEditors Library's UI into German. To do this, descendants of the DevExpress.XtraGrid.Localization.GridLocalizer and DevExpress.XtraEditors.Controls.Localizer classes with localized resource strings are created.

C#

public class GermanGridLocalizer : GridLocalizer {
   public override string Language { get { return "Deutsch"; }}
   public override string GetLocalizedString(GridStringId id) {
      string ret = "";
      switch(id) {
         // ...
         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 = "";
            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 "";
   }
}

VB

Public Class GermanGridLocalizer
   Inherits GridLocalizer
   Public Overrides ReadOnly Property Language() As String
      Get
         Return "Deutsch"
      End Get
   End Property

   Public Overrides Function GetLocalizedString(ByVal id As GridStringId) As String
      Dim ret As String = ""
      Select Case id
         ' ...
         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)"
         ' ...
         Case Else
            ret = ""
      End Select
      Return ret
   End Function
End Class

Public Class GermanEditorsLocalizer
   Inherits Localizer
   Public Overrides ReadOnly Property Language() As String
      Get
         Return "Deutsch"
      End Get
   End Property

   Public Overrides Function GetLocalizedString(ByVal id As StringId) As String
      Select Case 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"
         ' ...
      End Select
      Return ""
   End Function
End Class

Then these localizers are instantiated and assigned to their ancestors' Active property.

C#

private void Form1_Load(object sender, System.EventArgs e) {
   GridLocalizer.Active = new GermanGridLocalizer();
   Localizer.Active = new GermanEditorsLocalizer();
}

VB

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   GridLocalizer.Active = New GermanGridLocalizer()
   Localizer.Active = New GermanEditorsLocalizer()
End Sub

The result of localizing the runtime grid interface is shown below:

As mentioned above the technique described in this section can also be used when it is necessary to replace the default resources (for English (Unites States) culture) with their equivalents. For example, you can change such strings as "equals", "less then", etc. which are used in the grid's Custom Filter Dialog to their numerical equivalents "=", "<" as shown below:

C#

private void Form1_Load(object sender, System.EventArgs e) {
   GridLocalizer.Active = new MyGridLocalizer();
}

public class MyGridLocalizer : GridLocalizer {
   public override string GetLocalizedString(GridStringId id) {
      string ret = "";
      switch(id) {
         // ...
         case GridStringId.CustomFilterDialogConditionEQU : return "=";
         case GridStringId.CustomFilterDialogConditionNEQ : return "<>";
         case GridStringId.CustomFilterDialogConditionGT : return ">";
         case GridStringId.CustomFilterDialogConditionGTE : return ">=";
         case GridStringId.CustomFilterDialogConditionLT : return "<";
         case GridStringId.CustomFilterDialogConditionLTE : return "<=";
         // ...
         default:
            ret = "";
            break;
      }
      return ret;
   }
}

VB

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   GridLocalizer.Active = New MyGridLocalizer()
End Sub

Public Class MyGridLocalizer
   Inherits GridLocalizer
   Public Overrides Function GetLocalizedString(ByVal id As GridStringId) As String
      Dim ret As String = ""
      Select Case id
         ' ...
         Case GridStringId.CustomFilterDialogConditionEQU : Return "="
         Case GridStringId.CustomFilterDialogConditionNEQ : Return "<>"
         Case GridStringId.CustomFilterDialogConditionGT : Return ">"
         Case GridStringId.CustomFilterDialogConditionGTE : Return ">="
         Case GridStringId.CustomFilterDialogConditionLT : Return "<"
         Case GridStringId.CustomFilterDialogConditionLTE : Return "<="
         ' ...
         Case Else
            ret = ""
      End Select
      Return ret
   End Function
End Class

The result is shown below:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值