Translation Engine


The translation engine is a library of machine translators that support one or more translation language pairs (e.g., English to French). In all but one of the translators in this chapter, the language pairs are culture neutral. The translators shown in this chapter represent examples of free translation sources, but you can add your own translator classes. Figure 9.1 shows the translators included with the code for this book.

Figure 9.1. Translator Class Hierarchy


Translators implement the ITranslator interface (shown later), which has a method called translate. In this example, we are creating a new WebServiceXTranslator object (which we cover in the section on Web service translators) and are calling the TRanslate method to translate "Eddies in the space time continuum" from English to German:

 ITranslator translator = new WebServiceXTranslator(); string text = translator.Translate(     "en", "de", "Eddies in the space time continuum"); 


Translators support a method called IsSupported so that they can be interrogated to see if they support a given language pair:

 ITranslator translator = new WebServiceXTranslator(); if (translator.IsSupported("en", "de"))     string text = translator.Translate(         "en", "de", "Eddies in the space time continuum"); 


Typically, you won't use just one translator; you'll use a collection of translators. Not all translators support all possible language pairs, so a collection of translators enables you to support the sum of language pairs for many translators. In addition, not all translators are always online, so having a language pair redundancy enables you to fall back to other translators when one becomes unavailable. For this purpose, there is the TRanslatorCollection class:

 TranslatorCollection translators = new TranslatorCollection(); translators.Add(new PseudoTranslator()); translators.Add(new WebServiceXTranslator()); translators.Add(new CloserFarTranslator()); translators.Add(new AltaVistaTranslator()); translators.Add(new FreeTranslationTranslator()); translators.Add(new Office2003ResearchServicesTranslator()); 


In this example, we create a new translatorCollection and add a number of translators that support overlapping and unique language pairs. You can then get a translator from the collection for a given language pair:

 ITranslator translator = translators.GetTranslator("en", "de") string text = translator.Translate(     "en", "de", "Eddies in the space time continuum"); 


The algorithm used to get the translator is a simple sequential search, but you could change the algorithm to be more sophisticated, to favor certain translators for certain language pairs.

The ITranslator Interface

Translators are defined by the ITranslator interface:

 public interface ITranslator {     bool IsSupported(string inputLanguage, string outputLanguage);     string Translate(string inputLanguage, string outputLanguage,         string text);     string Name {get;}     bool Enabled {get;set;}     string[,] LanguagePairs {get;} } 


You have already seen the IsSupported and translate methods. The Name property is the name of the translator. The Enabled property specifies whether the translator is enabled. This can be used to turn off a translator. For example, the Resource Administrator (in Chapter 10) catches exceptions thrown by the TRanslate method and sets Enabled to false. This isn't the same as removing a translator from the collection because, in a long-running application, you might want to "resurrect" translators to give them a second chance.

The LanguagePairs property is an array of language pairs supported by the translator. The array is intended for informational purposes only. To determine whether a language pair is supported, use the IsSupported method instead of scanning the LanguagePairs array. The IsSupported method is more accurate than walking through the LanguagePairs array, mainly because the LanguagePairs array can contain wildcards ("*") to denote "any language," but also because some translators could support language pairs that can be tested only dynamically.

The Translator Class

The translator class implements the ITranslator interface and acts as a base class for all the translators in this chapter. Of course, if you write your own translator classes, you do not need to inherit from translatoryou only need to support the ITranslator interface. The translator class implements the basic functionality for the properties and the IsSupported method. The translate method is left for subclasses to implement.

 public abstract class Translator: ITranslator {     private bool enabled = true;     private string name;     private string[,] languagePairs;     public Translator(string name)     {         this.name = name;     }     public Translator(string name, string[,] languagePairs)     {         this.name = name;         this.languagePairs = languagePairs;     }     public string Name     {         get {return name;}     }     public bool Enabled     {         get {return enabled;}         set {enabled = value;}     }     public virtual string[,] LanguagePairs     {         get {return languagePairs;}     }     public virtual bool IsSupported(         string inputLanguage, string outputLanguage)     {         if (languagePairs == null)             return false;         if (inputLanguage.Length < 2 || outputLanguage.Length < 2)             return false;         for(int pairNumber = 0;             pairNumber < languagePairs.GetLength(0); pairNumber++)         {             if ((String.Compare(inputLanguage,                 languagePairs[pairNumber, 0], true,                 CultureInfo.InvariantCulture) == 0                 || languagePairs[pairNumber, 0] == "*")                 && String.Compare(outputLanguage,                 languagePairs[pairNumber, 1], true,                 CultureInfo.InvariantCulture) == 0)                 return true;         }         return false;     }     public abstract string Translate(         string inputLanguage, string outputLanguage, string text); } 


In addition, the translator class supports three conversion helper methods that can be used as needed.

The TranslatorCollection Class

The TRanslatorCollection class is a collection of objects that implement the ITranslator interface:

 public class TranslatorCollection : List<ITranslator> 


(In .NET Framework 1.1, the base class is CollectionBase instead of List<ITranslator>).

translatorCollection implements the following methods:

 public int IndexOf(string name) {     for(int index = 0; index < Count; index++)     {         if (this[index].Name == name)             return index;     }     return -1; } public ITranslator GetTranslator(     string inputLanguage, string outputLanguage) {     int index = IndexOf(inputLanguage, outputLanguage);     if (index == -1)         return null;     return this[index]; } public ITranslator GetEnabledTranslator(     string inputLanguage, string outputLanguage) {     int index = EnabledIndexOf(inputLanguage, outputLanguage);     if (index == -1)         return null;     return this[index]; } public int IndexOf(string inputLanguage, string outputLanguage) {     for(int index = 0; index < Count; index++)     {         if (this[index].IsSupported(inputLanguage, outputLanguage))             return index;     }     return -1; } public int EnabledIndexOf(     string inputLanguage, string outputLanguage) {     for(int index = 0; index < Count; index++)     {         ITranslator translator = this[index];         if (translator.Enabled &&             translator.IsSupported(inputLanguage, outputLanguage))             return index;     }     return -1; } 


You have already seen GetTRanslator, which gets a translator from the list when given a language pair. GetTRanslator is based upon an overloaded IndexOf method, which accepts the same parameters. GetEnabledTranslator (based upon EnabledIndexOf) performs the same search as Gettranslator, except that it looks for only enabled translators. This method is useful if your application catches translator exceptions and "turns off" translators, or, alternatively, if you offer the user the capability to turn translators on and off.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net