Web Service Translators


Many free and commercial machine translators are implemented as Web services. You can find Web service translators from Web service directories such as these:

  • XMethods (http://www.xmethods.com)

  • SalCentral (http://www.salcentral.com)

In this section, I show an example of a Web Service translator using the WebServiceX translator at http://www.webservicex.net/TranslateService.asmx. This Web service provides machine-translation services for European, Chinese, Japanese, and Korean languages. The code for this book also includes a translator for the Closer-Far service, at http://www.closerfar.com/engtoarabic.asmx, which translates English to Arabic.

Start by adding a reference to the Web service (in Solution Explorer, right-click the project, select Add Web Reference..., enter the address in the URL text box, click Go, and click the Add Reference button). This is the WebServiceXTranslator class:

 public class WebServiceXTranslator: Translator {     private net.webservicex.www.TranslateService translateService;     public WebServiceXTranslator():         base("WebServiceX Translator", new string[,]         {             {"en", "zh-CHS"},             {"en", "fr"},             {"en", "de"},             {"en", "it"},             {"en", "ja"},             {"en", "ko"},             {"en", "pt"},             {"en", "es"},             {"fr", "en"},             {"fr", "de"},             {"de", "en"},             {"de", "fr"},             {"it", "en"},             {"es", "en"}         })     {     }     public override string Translate(         string inputLanguage, string outputLanguage, string text)     {         if (! IsSupported(inputLanguage, outputLanguage))             throw new LanguageCombinationNotSupportedException(                 "Language combination is not supported",                 inputLanguage, outputLanguage);         if (translateService == null)             translateService =                 new net.webservicex.www.TranslateService();         string translatedText = translateService.Translate(             TranslationServiceLanguage(             inputLanguage, outputLanguage), text);         return ConvertFromEscapedNumerics(translatedText);     } } 


The constructor calls the base class, passing in the name and the list of supported language pairs. As mentioned in previous chapters, the Chinese (Simplified) language is not a simple two-letter code like most other languages. Although it is uncommon, this is not the only language that is not identified by two letters.

The translate method calls the Web service's TRanslate method, passing in an enumeration that identifies the language pair and the text to translate. The TRanslationServiceLanguage method is simply a conversion from a language pair to the required enum.




.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