How to: Localize Custom String Constants(如何:本地化自定义字符串常量)
This topic demonstrates how to localize custom strings. This example uses confirmation messages. The Application Model has the Localization node that allows localization of various constants. You can use this node to localize custom strings used in your applications.
本主题演示如何本地化自定义字符串。本示例使用确认消息。应用模型(Application Model)包含一个Localization节点,该节点允许对各种常量进行本地化。您可以使用此节点来本地化应用程序中使用的自定义字符串。
For instance, a feature that you implement in your application uses the following code:
例如,您在应用程序中实现的某个功能使用以下代码:
C#
using System.Windows.Forms;
// ...
if (MessageBox.Show("Do you wish to cancel your changes?", "", MessageBoxButtons.YesNo)
== DialogResult.Yes) {
// ...
}
To localize custom strings from the WebApi context, refer to the following article: Access Caption Helper in Custom Endpoint Methods.
要从WebApi上下文本地化自定义字符串,请参考以下文章:《在自定义端点方法中访问标题助手》。

To localize the text of the confirmation message above, add the LocalizationItem child node to the Localization | Messages node, and specify its Value property in the required language:
要本地化上述确认消息的文本,请将LocalizationItem子节点添加到Localization | Messages节点,并在所需语言中指定其Value属性:
You can use the static CaptionHelper.GetLocalizedText method to get the required message text from the new node.
您可以使用静态方法CaptionHelper.GetLocalizedText从新节点获取所需的消息文本。
C#
using System.Windows.Forms;
using DevExpress.ExpressApp.Utils;
// ...
if (MessageBox.Show(CaptionHelper.GetLocalizedText("Messages", "DoYouWishToCancelChanges"),
"", MessageBoxButtons.YesNo) == DialogResult.Yes) {
// ...
}
If you need to localize multiple custom strings, add the LocalizationGroup node to the Localization node and specify your strings there. In this example, this node is called Confirmations:
如果需要本地化多个自定义字符串,请将LocalizationGroup节点添加到Localization节点,并在其中指定您的字符串。在本示例中,此节点名为Confirmations:
C#
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 use the CaptionHelper.GetLocalizedText method to create a complex hierarchy. This method accepts a path as an input parameter. For instance, to access the Localization | Messages | Confirmations | DataChangingConfirmations group, use the “Messages\Confirmations\DataChangingConfirmations” path. Do not include the Localization node itself in the path.
LocalizationGroup节点可以具有多级结构。您可以使用CaptionHelper.GetLocalizedText方法创建复杂的层次结构。此方法接受路径作为输入参数。例如,要访问Localization | Messages | Confirmations | DataChangingConfirmations组,请使用“Messages\Confirmations\DataChangingConfirmations”路径。不要在路径中包含Localization节点本身。
Export Localization Items from Resource Files(从资源文件导出本地化项)
You can add Localization Items from a resource file. Assume you have the following MyResource.resx resource file in your XAF solution:
您可以从资源文件添加本地化项。假设您的XAF解决方案中有以下MyResource.resx资源文件:
To export values from this file to the Application Model, implement a Resource Localizer (XafResourceLocalizer descendant):
要将此文件中的值导出到应用模型,请实现资源本地化器(XafResourceLocalizer的派生类):
C#
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);
}
}
In this snippet, “MySolution.Module.MyResource” is the resource name. Items from this resource are added to the Localization | Messages | Confirmations Localization Group. Add the Resource Localizer type to the ModuleBase.ResourcesExportedToModel collection to register the Resource Localizer in a module.
在本代码片段中,“MySolution.Module.MyResource”是资源名称。此资源中的项会被添加到Localization | Messages | Confirmations本地化组中。要在模块中注册资源本地化器,请将资源本地化器类型添加到ModuleBase.ResourcesExportedToModel集合中。
C#
public sealed partial class MySolutionModule : ModuleBase {
public MySolutionModule() {
// ...
ResourcesExportedToModel.Add(typeof(MyXafResourceLocalizer));
}
// ...
}
Alternatively, you can register a resource localizer in the overridden ModuleBase.GetXafResourceLocalizerTypes method.
或者,您可以在重写的ModuleBase.GetXafResourceLocalizerTypes方法中注册资源本地化器。
C#
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 can add or remove a localizer in the Application Designer.
在这种情况下,您可以在应用程序设计器中添加或移除本地化器。
Note
Localizer types that 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 how the Visual Studio design-time environment works. It cannot get the list of localizer types of the module itself because this information is not serializable.
在当前模块中定义的本地化器类型不会显示在模块设计器的“导出到模型的资源”对话框中。此行为是设计使然,与Visual Studio设计时环境的工作方式有关。由于该信息不可序列化,因此无法获取模块自身的本地化器类型列表。
Rebuild the solution and invoke the Model Editor. You can see that items from the MyResource.resx resource are exported to the Application Model.
重建解决方案并调用模型编辑器。您会看到MyResource.resx资源中的项已导出到应用模型中。
You can now use the CaptionHelper.GetLocalizedText method to access localizable values from code.
现在,您可以使用CaptionHelper.GetLocalizedText方法从代码中访问可本地化的值。