locked
How do you use TranslateArray in HTML mode? RRS feed

  • Question

  • I'm trying to post HTML content to TranslateArray but am unsure of what form the content should be in.

    Normally posting text is done like this:

    POST http://api.microsofttranslator.com/V2/Http.svc/TranslateArray

    with headers:

    Authorization = Bearer <auth token>
    Content-Type = text/xml

    and body:

    <TranslateArrayRequest>
      <AppId />
      <From>en</From>
      <Texts>
        <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">cat</string>
        <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">dog</string>
      </Texts>
      <To>fr</To>
    </TranslateArrayRequest>

    But if I want to send HTML, then how do I do that please?

    I've read that I need Content-Type = text/html

    but what form do I post the body in? If I try:

    ...

      <Texts>
        <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><html><body><p>one</p></body></html></string>
      </Texts>

    ...

    that doesn't work, and neither does posting just the HTML on it's own and sending the 'from' and 'to' as headers.

    I'd be really grateful if someone could help me out and post a simple example (headers & body).

    Many thanks,

    Mike

    Thursday, August 6, 2015 5:11 PM

Answers

  • OK, I've found the answer...

    For some reason I hadn't noticed the Options. The other thing to note is the need to send HTML tags as entities: <p> becomes &lt;p&gt;

    So, here is what I was looking for:

    POST http://api.microsofttranslator.com/V2/Http.svc/TranslateArray

    with headers:

    Authorization = Bearer <auth token>
    Content-Type = text/xml

    and body:

    <TranslateArrayRequest>
      <AppId />
      <From>en</From>
      <Options>
        <ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/html</ContentType>
      </Options>
      <Texts>
        <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        &lt;p1&gt;one&lt;/p1&gt;&lt;p1&gt;two&lt;/p1&gt;&lt;p1&gt;three&lt;/p1&gt;
        </string>
      </Texts>
      <To>fr</To>
    </TranslateArrayRequest>

    will return:

    <ArrayOfTranslateArrayResponse xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <TranslateArrayResponse>
            <From>en</From>
            <OriginalTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                <a:int>40</a:int>
            </OriginalTextSentenceLengths>
            <State/>
            <TranslatedText>
    &lt;p1&gt;un&lt;/p1&gt; &lt;p1&gt;deux&lt;/p1&gt; &lt;p1&gt;trois&lt;/p1&gt;
    </TranslatedText>
            <TranslatedTextSentenceLengths xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                <a:int>42</a:int>
            </TranslatedTextSentenceLengths>
        </TranslateArrayResponse>
    </ArrayOfTranslateArrayResponse>


    • Marked as answer by frudev Friday, August 7, 2015 8:31 AM
    Friday, August 7, 2015 8:30 AM