学习023-08 How to: Create a Custom Translation Provider for the Localization Tool(如何:为本地化工具创建自定义翻译提供程序

How to: Create a Custom Translation Provider for the Localization Tool(如何:为本地化工具创建自定义翻译提供程序)

The Localization Tool utilizes the Microsoft Translator as the translation provider. However, one of the notable features of our localization tool is the capability to use custom translation services. That means that you can use alternative translators. In this example, custom translation provider implementation is demonstrated.
本地化工具使用微软翻译器作为翻译提供程序。不过,我们的本地化工具的一个显著特性是能够使用自定义翻译服务。这意味着您可以使用其他翻译器。本示例将演示自定义翻译提供程序的实现。

To create a custom translation provider, you need to create and register a class, implementing the ITranslatorProvider interface. As an alternative to implementing it from scratch, you can create a descendant of the TranslatorProviderBase class, which already encompasses a good deal of functionality. This class also collects localized values in blocks to shorten the translation based on the online translation services. In this example, we will utilize the second approach.
要创建自定义翻译提供程序,您需要创建并注册一个实现ITranslatorProvider接口的类。作为从零开始实现的替代方案,您可以创建TranslatorProviderBase类的派生类,该类已经包含了大量功能。此类还会将本地化值按块收集,以基于在线翻译服务缩短翻译过程。在本示例中,我们将采用第二种方法。

The TranslatorProviderBase class descendant should implement some methods and properties of the ITranslatorProvider interface, which are not implemented by the base class.
TranslatorProviderBase类的派生类需要实现ITranslatorProvider接口中未被基类实现的一些方法和属性。

  • ITranslatorProvider.Caption
  • ITranslatorProvider.Description
  • ITranslatorProvider.GetLanguages

Additionally, overriding the ITranslatorProvider.Translate method is required.
此外,还需要重写ITranslatorProvider.Translate方法。

Perform the following steps to implement the translation provider.
执行以下步骤来实现翻译提供程序。

  • Add the following class into the platform-agnostic module project (e.g., MySolution.Module), or add a new module to your solution and implement this class in it.
    将以下类添加到与平台无关的模块项目中(例如,MySolution.Module),或者向您的解决方案中添加一个新模块并在其中实现此类。
C#
using DevExpress.ExpressApp.Utils;
// ...
public class MyTranslatorProvider : TranslatorProviderBase {
    public MyTranslatorProvider() : base("<br />", 5000) { }


    #region ITranslatorProvider Members
    public override string Caption {
        get { return "My Translate Provider"; }
    }
    public override string Description {
        get { return "My Description"; }
    }
    public override string[] GetLanguages() {
        string[] supportedLanguages = {"en", "fr", "de"};
        return supportedLanguages;
    }
    public override string Translate(string text, string sourceLanguageCode,
        string desinationLanguageCode) {
        string result = "";
        // Place the code that translates 'text' from 'sourceLanguageCode' to 'desinationLanguageCode' here
        return result;
    }
    #endregion
}

Pass the maximum block length and separator to the base class constructor.
将最大块长度和分隔符传递给基类构造函数。

Note
You can also override the auxiliary CalculateSentences method if you need to split the localized value into several parts to be localized separately. For instance, this is required when the translator service incorrectly handles special symbols, and also when you need to exclude strings enclosed in quotes and other special symbols. The TranslatorProviderBase class collects localized values, and splits them into small parts based on the CalculateSentences method, and then composes blocks from these smaller parts and processes them with the Translate method.

如果需要将本地化值拆分为多个部分以分别进行本地化,您也可以重写辅助方法CalculateSentences。例如,当翻译服务无法正确处理特殊符号,或者需要排除引号内的字符串及其他特殊符号时,就需要这样做。TranslatorProviderBase类会收集本地化值,并根据CalculateSentences方法将其拆分为小部分,然后从这些小部分组成块并使用Translate方法进行处理。

Tip
To see a complete example, refer to the sources of the built-in BingTranslatorProvider available at C:\Program Files\DevExpress 24.1\Components\Sources\DevExpress.ExpressApp\DevExpress.ExpressApp.Win\Core\ModelEditor\Localization\TranslatorProviders.cs

要查看完整示例,请参考内置BingTranslatorProvider的源代码,其位置为:C:\Program Files\DevExpress 24.1\Components\Sources\DevExpress.ExpressApp\DevExpress.ExpressApp.Win\Core\ModelEditor\Localization\TranslatorProviders.cs

  • Modify the module’s constructor to register a custom translation provider class.
    修改模块的构造函数以注册自定义翻译提供程序类。
C# 
public sealed partial class MySolutionModule : ModuleBase {
    public MySolutionModule() {
        InitializeComponent();
        TranslatorProvider.RegisterProvider(new MyTranslatorProvider());
    }
}
  • Rebuild your solution, and this custom translation provider will replace the default provider used by the Localization Tool in Visual Studio and at runtime.
    重新生成您的解决方案,此自定义翻译提供程序将替换Visual Studio中以及运行时本地化工具所使用的默认提供程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值