The Bing API allows you to get results from multiple sourcetypes, one of them being the Translation SourceType.

More Info on the API can be found here.

The SDK comes with some nice samples and can be found here.

As a practise we are going to do a query from a BizTalk Pipeline component  to translate a field configured by an XPath.

In design we need to define a source language, destination language and the XPath to the field we will translate.

TranslationLanguage is just an enum to facilitate configuration in pipeline design time.

 

private Helper.TranslationLanguage _SourceLang;

private Helper.TranslationLanguage _DestLang;

private string _XPath;

 

public Helper.TranslationLanguage SourceLanguage

getset; }

 

public Helper.TranslationLanguage DestinationLanguage

getset; }

 

public string XPath

getset; }


Next we modify the Execute function of the component to call the Tranlate method we will define further on, to modify the message.

A good way to do this is by using the XPathMutatorStream. More info on XPathMutatorStream can be found here.

 

IBaseMessage Microsoft.BizTalk.Component.Interop.IComponent            Execute(IPipelineContext pContext, IBaseMessage pInMsg)

{

    IBaseMessage biztalkMessage = pInMsg;

    XmlReader reader = XmlReader.Create(pInMsg.BodyPart.Data);

    XPathCollection xpaths = new XPathCollection();

    xpaths.Add(this._XPath);

 

    ValueMutator mutator = new ValueMutator(handleXpathFound);

    pInMsg.BodyPart.Data = new XPathMutatorStream(reader, xpaths, mutator);

    return pInMsg;

}

 

private void handleXpathFound(int matchIdx, Microsoft.BizTalk.XPath.XPathExpression matchExpr, string origVal, refstring finalVal)

{

    finalVal = Helper.Translate(SourceLanguage, DestinationLanguage, origVal);

}

 

Finally we’ll send a request to the Bing API using the XML protocols ( we are BizTalk Developpers after all … ).

We build the query using the arguments provided and return the translation:

 

public static string Translate(TranslationLanguage SourceLang, 

 TranslationLanguage DestLang, string text)

{

    LoadLanguages();

    HttpWebRequest request = BuildRequest(_Languages[SourceLang],

 _Languages[DestLang], text);

 

    try

    {

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        XmlDocument document = new XmlDocument();

        document.Load(response.GetResponseStream());

 

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);

        nsmgr.AddNamespace(“tra”, “http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation”); 

return document.DocumentElement.SelectSingleNode(“./tra:Translation/

tra:Results/tra:TranslationResult”, nsmgr).InnerText;

    }

    catch (WebException ex)

    {

        throw new ApplicationException(“Web Error while translating”,ex);

    }

}

 

static HttpWebRequest BuildRequest(string SourceLang, string DestLang, string text)

{

    string requestString = “http://api.bing.net/xml.aspx?”

        + “AppId=” + AppId

        + “&Query=” + text

        + “&Sources=Translation”

        + “&Version=2.2”

        + “&Translation.SourceLanguage=” + SourceLang

        + “&Translation.TargetLanguage=” + DestLang;


    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(

        requestString);

 

    return request;

}


 

Time to test: Create a BizTalk project with

A schema for testing:

 

A pipeline with our new component. We define the xpath to the Value Record, the source and destinationlanguage. I created pipelines to translate to Dutch and French from English:

 

Deploy in BiZtalk, create Receiveport with Receivelocation, 2 Sendports with a filter on the Receiveportname and our pipeline to translate to French and Dutch.

 

Drop a test message:


<ns0:TestTranslatorSchema 

xmlns:ns0=http://TestTranslator.TestTranslatorSchema>

  <Value>What language is this written in?</Value>

</ns0:TestTranslatorSchema>

 

And observe the results:

 

<ns0:TestTranslatorSchema 

xmlns:ns0=http://TestTranslator.TestTranslatorSchema>

  <Value>Quelle langue c’est écrit ?</Value>

</ns0:TestTranslatorSchema>

 

<ns0:TestTranslatorSchema 

xmlns:ns0=http://TestTranslator.TestTranslatorSchema>

  <Value>In welke taal is dit geschreven?</Value>

</ns0:TestTranslatorSchema>

 


Korneel Vanhie, CODit