如何通过/TranslateArray排除翻译文本?微软翻译API批量翻译问题
Fixing "notranslate" Issue in Microsoft Translator API's TranslateArray
Hey there, I’ve run into exactly this problem before! The single /Translate API plays nice with the <div class="notranslate"> tag, but the batch /TranslateArray API needs a slightly different approach to preserve untranslatable content like @handles or hashtags. Here are two reliable solutions I’ve used successfully:
1. Use Custom Terminology to Lock in Untranslatable Content
This method is perfect if you have a set of recurring terms you never want translated:
- First, create a custom terminology list in the Azure Translator service. Add every @handle, hashtag, or static phrase you need to keep, setting both the source and target language versions to the exact same text (e.g., source:
@TomWoolway, target:@TomWoolway). - When calling
TranslateArray, include theCustomCategoryparameter in your request, pointing to the ID of your custom terminology list. - The API will automatically match these terms in your batch texts and return them unmodified, no HTML tags needed.
2. Use TextType=html with <span class="notranslate">
If you prefer using inline tags (similar to the single API), you just need to adjust the tag type and explicitly set the text format:
- Swap out
<div class="notranslate">for<span class="notranslate">—divs can cause parsing issues in batch requests since they’re block-level elements. - In your
TranslateArrayrequest, set thetextTypeparameter tohtml(the default isplain, which ignores all HTML tags). - Example JSON request body:
{ "inputs": [ { "Text": "Hey <span class=\"notranslate\">@TomWoolway</span>, have you seen this <span class=\"notranslate\">#APIProTip</span>?" }, { "Text": "Don't forget to follow <span class=\"notranslate\">@AzureTranslator</span> for updates!" } ], "to": "es", "textType": "html" }
- The translated response will keep all the
<span class="notranslate">content exactly as it was, while translating the rest of the text.
Quick Notes
- Make sure your Azure Translator resource has the necessary permissions for custom terminology or HTML processing (most tiers include this by default, but double-check if you’re on a free plan).
- If you’re using an SDK (like C# or Python), don’t forget to set the
TextTypeproperty in your request object—don’t just add it to the raw JSON. - Test with a small batch first to confirm the untranslatable content is preserved correctly before scaling up.
内容的提问来源于stack exchange,提问作者Tom Woolway
相关产品推荐
相关产品推荐

