Azure Text Translation client library for Python that provides neural machine translation technology for quick and accurate source-to-target text translation in real time across all supported languages
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Neural machine translation service that renders source text into multiple target languages simultaneously. Supports advanced features including automatic language detection, profanity filtering, text alignment analysis, custom translation models, and sentence boundary detection.
Translates input text from source language to one or more target languages using neural machine translation with extensive customization options.
def translate(
body: Union[List[str], List[InputTextItem], IO[bytes]],
*,
to_language: List[str],
client_trace_id: Optional[str] = None,
from_language: Optional[str] = None,
text_type: Optional[Union[str, TextType]] = None,
category: Optional[str] = None,
profanity_action: Optional[Union[str, ProfanityAction]] = None,
profanity_marker: Optional[Union[str, ProfanityMarker]] = None,
include_alignment: Optional[bool] = None,
include_sentence_length: Optional[bool] = None,
suggested_from: Optional[str] = None,
from_script: Optional[str] = None,
to_script: Optional[str] = None,
allow_fallback: Optional[bool] = None,
**kwargs: Any
) -> List[TranslatedTextItem]Parameters:
body: Text to translate (strings, InputTextItem objects, or binary data)to_language: Target language codes (e.g., ["es", "fr", "de"])from_language: Source language code (auto-detected if omitted)text_type: Content type (Plain or Html)category: Custom translator model category IDprofanity_action: Profanity handling (NoAction, Marked, Deleted)profanity_marker: Profanity marking style (Asterisk, Tag)include_alignment: Include word alignment projectioninclude_sentence_length: Include sentence boundary informationsuggested_from: Fallback language for failed detectionfrom_script/to_script: Script specifications for transliterationallow_fallback: Allow fallback to general models for custom categoriesReturns: List of translation results with metadata
from azure.ai.translation.text import TextTranslationClient
from azure.ai.translation.text.models import TextType, ProfanityAction
from azure.core.credentials import AzureKeyCredential
client = TextTranslationClient(
credential=AzureKeyCredential("your-api-key"),
region="your-region"
)
# Basic translation to multiple languages
response = client.translate(
body=["Hello, how are you?"],
to_language=["es", "fr", "de"]
)
for translation in response:
print(f"Detected language: {translation.detected_language.language}")
for result in translation.translations:
print(f"{result.to}: {result.text}")
# HTML content translation with profanity filtering
html_response = client.translate(
body=["<p>This is a <b>damn</b> good example!</p>"],
to_language=["es"],
text_type=TextType.HTML,
profanity_action=ProfanityAction.MARKED,
profanity_marker="Asterisk"
)
# Translation with alignment and sentence information
detailed_response = client.translate(
body=["Hello world. How are you today?"],
to_language=["es"],
include_alignment=True,
include_sentence_length=True
)
translation = detailed_response[0].translations[0]
print(f"Alignment: {translation.alignment.proj}")
print(f"Source sentences: {translation.sent_len.src_sent_len}")
print(f"Target sentences: {translation.sent_len.trans_sent_len}")
# Custom model translation
custom_response = client.translate(
body=["Technical documentation text"],
to_language=["ja"],
from_language="en",
category="your-custom-model-id",
allow_fallback=False
)class InputTextItem:
text: str # Text content to translateclass TextType(str, Enum):
PLAIN = "Plain" # Plain text content (default)
HTML = "Html" # HTML markup contentclass ProfanityAction(str, Enum):
NO_ACTION = "NoAction" # Leave profanity unchanged (default)
MARKED = "Marked" # Mark profanity with specified marker
DELETED = "Deleted" # Remove profanity entirely
class ProfanityMarker(str, Enum):
ASTERISK = "Asterisk" # Replace with asterisks (default)
TAG = "Tag" # Wrap with XML tagsclass TranslatedTextItem:
translations: List[TranslationText] # Translation results per target language
detected_language: Optional[DetectedLanguage] # Auto-detected source language
source_text: Optional[SourceText] # Original text in default scriptclass TranslationText:
text: str # Translated text
to: str # Target language code
alignment: Optional[TranslatedTextAlignment] # Word alignment data
sent_len: Optional[SentenceBoundaries] # Sentence length information
transliteration: Optional[dict] # Script conversion resultclass DetectedLanguage:
language: str # Detected language code
score: float # Confidence score (0.0 to 1.0)class TranslatedTextAlignment:
proj: str # Alignment projection string
# Format: [[SourceStart:SourceEnd–TargetStart:TargetEnd] ...]class SentenceBoundaries:
src_sent_len: List[int] # Source sentence lengths
trans_sent_len: List[int] # Translated sentence lengthsclass SourceText:
text: str # Input text in default script of source languageInstall with Tessl CLI
npx tessl i tessl/pypi-azure-ai-translation-text