You’ll recall that I already blogged about the new Localization tool coming in 10.2? By default, this tool utilizes the Microsoft Translator ™ service (originally we used the Google Translate ™ service, but we had to switch for licensing reasons). However, one of the notable features of our localization tool is it’s capability to use a custom translation services. That means that you can use alternative translators, such as Google Translate, etc., should you so desire.
In order to create a custom translator, simply create a custom translation provider class, implementing the ITranslatorProvider interface, or descend your class from the default TranslatorProviderBase class, which already implements the required interface. Both entities are available in the DevExpress.ExpressApp.Utils namespace.
The ITranslatorProvider interface is quite simple:
public interface ITranslatorProvider
{
string Caption { get; }
string Description { get; }
string[] GetLanguages();
string[] Translate(string[] text, string sourceLanguageCode, string desinationLanguageCode);
event EventHandler<TranslationProgressEventArgs> TranslationProgressChanged;
}
Where Caption specifies the name of a custom translation provider; Description specifies a short description of the provider, for displaying in the translation window; GetLanguages is a method that returns a list of cultures, for which translations are possible; Translate is a method that performs the actual translation, and accepts and returns an array of localized values. Finally, TranslationProgressChanged is an event that is required to inform and update the UI during the translation process. The Progress parameter of the event arguments may be within the 0…1 range. You can also cancel the translation by setting the Cancel parameter to True.
As an alternative to implementing the ITranslatorProvider interface from scratch, you can create a descendant of the TranslatorProviderBase class, which already encapsulates a good deal of functionality. This class also collects localized values in blocks to shorten the translation based on the online translation services. You should specify the size of the block as well as the separator symbol in the constructor of the TranslatorProviderBase class:
TranslatorProviderBase(string separator, int textBlockSize)
For example, the maximum block length for the Google Translate service is 5000, the separator is "<br />" (an html tag representing a new line)
The TranslatorProviderBase class descendant should also implement methods and properties of the ITranslatorProvider interface, which are not implemented by the base class:
string Caption { get; }
string Description { get; }
string[] GetLanguages();
Of course, you’ll have to override the Translate method as required:
public virtual string Translate(string text, string sourceLanguageCode, string desinationLanguageCode);
because this method should query the required service for the translation.
You can also override the auxiliary CalculateSentences method if you need to split the localized value into several parts to be localized separately:
public virtual IEnumerable<string> CalculateSentences(string text);
The TranslatorProviderBase collects localized values, then splits them into small parts based on the CalculateSentences method and then composes blocks from these smaller parts and processes them via the Translate method.
To register our custom translation provider class, we can add the following line into the constructor of the platform-agnostic module of our XAF solution:
using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Utils;
namespace GoogleTranslatorProvider
{
public sealed partial class GoogleTranslatorProviderModule : ModuleBase
{
public GoogleTranslatorProviderModule()
{
InitializeComponent();
TranslatorProvider.RegisterProvider(new GoogleTranslatorProvider());
}
}
}
After registration, the custom translation provider will be available from both Visual Studio and the runtime Model Editor.
There are other reasons for creating a custom provider, not just preferring one service over another. For example, those who are doing the actual translations in your applications may not have an internet connection and so you may want to create a custom translator provider, utilizing a local dictionary.
You can download a sample, demonstrating how to create a custom translation provider, utilizing the Google Translate Service, from Code Central (once 10.2 ships
).
Note, of course, that by using the provider from this example you automatically agree to the terms of use of the Google Translate Service, you can find a copy of these terms of use at http://code.google.com/intl/sv/terms.html
Just before I close, I want to thank Vladimir Boborykin, a software engineer from Russia, who first explored the idea of localising XAF applications using online translation services, and then created the free XAF Model Translator (XafMT) tool for the XAF community. This tool is publicly available at http://xafmt.narod.ru . He has been maintaining this tool since 9.3, and his work has been greatly appreciated by the XAF community.
Well that’s it for this post, so until next time, happy XAF’ing. 