Tom Woolway
Tom Woolway

Reputation: 3

How to exclude text from translation using /TranslateArray

We're trying to use the Microsoft Translator API to batch translate text. Each piece of text may contain text we don't want translated (normally social network @handles or hashtags). We've tried to wrap these parts of the text like is shown in the documentation:

<div class="notranslate">This will not be translated.</div>

This works fine when passing text to the /Translate single API. However, when we pass multiple pieces of text to the /TranslateArray API, we can't work out the correct syntax. Any text item which contains the notranslate div is not returned in the response.

Here's the body we're trying to use:

curl -i -X POST \
-H "Ocp-Apim-Subscription-Key:******" \
-H "Content-Type:text/html" \
-d \
'<TranslateArrayRequest>
     <AppId />
     <From>en</From>
     <Options>
         <ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/html</ContentType>
     </Options>
     <Texts>
      <div xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">With great power comes great <div class="notranslate">#responsibility</div> </div>
      <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Hello World</string>
     </Texts>
     <To>fr</To>
 </TranslateArrayRequest>' \
'https://api.microsofttranslator.com/V2/Http.svc/TranslateArray'

Any ideas on the correct format to pull this off?

Upvotes: 0

Views: 776

Answers (1)

Paul Dempsey
Paul Dempsey

Reputation: 663

The section as posted doesn't match the schema for the request: the first <div> needs to be a <string> element.

<Texts>
  <div xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">With great power comes great <div class="notranslate">#responsibility</div> </div>
  <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Hello World</string>
 </Texts>

Try:

<Texts>
  <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">With great power comes great <div class="notranslate">#responsibility</div> </string>
  <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Hello World</string>
 </Texts>

If this doesn't work, then it's possible that because the request is XML, you may also need to XML-escape the markup within the string element:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
With great power comes great &lt;div class="notranslate"&gt;#responsibility&lt;/div&gt;
</string>

Upvotes: 1

Related Questions