The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Localize Custom String Constants

  • 4 min to read

This topic demonstrates how to localize custom strings, using confirmation messages as an example. The Application Model has the Localization node, which allows for the localization of various constants. You can use this node to localize any custom strings used in your applications.

For instance, a feature that you implement in your application could use the following code.

using System.Windows.Forms;
// ...
if (MessageBox.Show("Do you wish to cancel your changes?", "", MessageBoxButtons.YesNo) 
   == DialogResult.Yes) {
   // ...
}

Add Localization Items in the Model Editor

To make the text of the confirmation message above localizable, you can add a LocalizationItem child node to the Localization | Messages node, and specify its Value property in the required language. The following image demonstrates the necessary changes to be done in the Application Model.

LocalizeCustomConstants_ModelEditor3

In this instance, you can get the required message text from the new node, using the static CaptionHelper.GetLocalizedText method.

using System.Windows.Forms;
using DevExpress.ExpressApp.Utils;
// ...
if (MessageBox.Show(CaptionHelper.GetLocalizedText("Messages", "DoYouWishToCancelChanges"), 
    "", MessageBoxButtons.YesNo) == DialogResult.Yes) {
    // ...
}

If you decide that you will have to localize multiple custom strings, you can add a special LocalizationGroup node to the Localization node, and specify your strings within it. Following our example, call this node Confirmations. The following image demonstrates these changes.

LocalizeCustomConstants_ModelEditor2

In this instance, use the following code.

using System.Windows.Forms;
using DevExpress.ExpressApp.Utils;
// ...
if (MessageBox.Show(CaptionHelper.GetLocalizedText(
    @"Messages\Confirmations", "DoYouWishToCancelChanges"), 
        "", MessageBoxButtons.YesNo) == DialogResult.Yes) {
    // ...
}

The LocalizationGroup node can have a multilevel structure. You can create a complex hierarchy and access a required item by passing the path to the CaptionHelper.GetLocalizedText method. Do not include the Localization node itself to the path. For instance, to access the Localization | Messages | Confirmations | DataChangingConfirmations group, use the "Messages\Confirmations\DataChangingConfirmations" path.

Export Localization Items from Resource Files

Alternatively, you can add Localization Items from the resource file. Assume you have the following MyResource.resx resource file in your XAF solution.

LocalizeCustomConstants_ResourceFile

To export values from this file to the Application Model, implement a Resource Localizer (XafResourceLocalizer descendant).

using DevExpress.ExpressApp.Localization;
// ...
public class MyXafResourceLocalizer : XafResourceLocalizer {
    protected override IXafResourceManagerParameters GetXafResourceManagerParameters() {
        string[] localizationGroupPath = new string[] { "Messages", "Confirmations" };
        return new XafResourceManagerParameters(localizationGroupPath, 
            "MySolution.Module.MyResource", "", GetType().Assembly);
    }
}

Here, "MySolution.Module.MyResource" is the resource name. Items from this resource will be added to the Localization | Messages | Confirmations Localization Group. Register this Resource Localizer within a module by adding its type to the ModuleBase.ResourcesExportedToModel collection.

public sealed partial class MySolutionModule : ModuleBase {
    public MySolutionModule() {
        // ...
        ResourcesExportedToModel.Add(typeof(MyXafResourceLocalizer));
    }
    // ...
}

Alternatively, you can register a resource localizer within the overridden ModuleBase.GetXafResourceLocalizerTypes method.

public sealed partial class ResourceLocalizerModule : ModuleBase {
    // ...
    public override ICollection<Type> GetXafResourceLocalizerTypes() {
        ICollection<Type> localizers = base.GetXafResourceLocalizerTypes();
        localizers.Add(typeof(MyXafResourceLocalizer));
        return localizers;
    }
}

In this instance, you will have an ability to add or remove a localizer in the Application Designer.

LocalizeCustomConstants_ResourcesExportedToModel

Note

Localizer types, which are defined in the current module, are not displayed in the Resources exported to model dialog of the Module Designer. This behavior is by design and specific to the work of the Visual Studio design time environment, which cannot get the list of localizer types of the module itself, because this information is not serializable.

Rebuild the solution and invoke the Model Editor. You will see that items from the MyResource.resx resource are exported to the Application Model.

LocalizeCustomConstants_ModelEditor

Now, you can use the CaptionHelper.GetLocalizedText method to access localizable values from your code.

See Also