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
Convert text from one script to another within the same language, preserving pronunciation and meaning. Supports conversions like Latin to Cyrillic, Arabic to Latin, Devanagari to Latin, and many other script pairs for supported languages.
Converts characters or letters of input text from one script to corresponding characters in a target script while maintaining linguistic meaning.
def transliterate(
body: Union[List[str], List[InputTextItem], IO[bytes]],
*,
language: str,
from_script: str,
to_script: str,
client_trace_id: Optional[str] = None,
**kwargs: Any
) -> List[TransliteratedText]Parameters:
body: Text to transliterate (strings, InputTextItem objects, or binary data)language: Language code for the text (e.g., "zh-Hans", "ar", "hi")from_script: Source script identifier (e.g., "Hans", "Arab", "Deva")to_script: Target script identifier (e.g., "Latn", "Cyrl")client_trace_id: Client-generated GUID for request trackingReturns: List of transliteration results with script information
from azure.ai.translation.text import TextTranslationClient
from azure.core.credentials import AzureKeyCredential
client = TextTranslationClient(
credential=AzureKeyCredential("your-api-key"),
region="your-region"
)
# Chinese to Latin transliteration
chinese_response = client.transliterate(
body=["这是个测试。"],
language="zh-Hans",
from_script="Hans",
to_script="Latn"
)
for result in chinese_response:
print(f"Original script: {result.script}")
print(f"Transliterated: {result.text}")
# Output: "zhè shì gè cè shì。"
# Arabic to Latin transliteration
arabic_response = client.transliterate(
body=["مرحبا بالعالم"],
language="ar",
from_script="Arab",
to_script="Latn"
)
# Hindi to Latin transliteration
hindi_response = client.transliterate(
body=["नमस्ते दुनिया"],
language="hi",
from_script="Deva",
to_script="Latn"
)
# Russian Cyrillic to Latin transliteration
russian_response = client.transliterate(
body=["Привет мир"],
language="ru",
from_script="Cyrl",
to_script="Latn"
)
# Multiple texts in single request
multiple_response = client.transliterate(
body=["第一个测试", "第二个例子"],
language="zh-Hans",
from_script="Hans",
to_script="Latn"
)
for i, result in enumerate(multiple_response):
print(f"Text {i+1}: {result.text}")class InputTextItem:
text: str # Text content to transliterateclass TransliteratedText:
script: str # Target script identifier used in output
text: str # Transliterated text in target scriptUse the language support endpoint to discover available script conversions:
# Get transliteration languages and their supported scripts
response = client.get_supported_languages(scope="transliteration")
if response.transliteration:
for lang_code, lang_info in response.transliteration.items():
print(f"\nLanguage: {lang_code} ({lang_info.name})")
for script in lang_info.scripts:
print(f" From script: {script.code} ({script.name})")
print(f" Available conversions:")
for target_script in script.to_scripts:
print(f" -> {target_script.code} ({target_script.name})")Hans: Simplified Chinese charactersHant: Traditional Chinese charactersArab: Arabic scriptDeva: Devanagari (Hindi, Sanskrit)Cyrl: Cyrillic (Russian, Bulgarian, etc.)Gujr: Gujarati scriptGuru: Gurmukhi (Punjabi)Hebr: Hebrew scriptJpan: Japanese (Hiragana/Katakana/Kanji)Kore: Korean (Hangul/Hanja)Thai: Thai scriptLatn: Latin alphabetCyrl: Cyrillic scriptArab: Arabic scriptHans: Simplified ChineseHant: Traditional Chinesefrom azure.core.exceptions import HttpResponseError
try:
response = client.transliterate(
body=["Invalid text for script"],
language="zh-Hans",
from_script="InvalidScript", # Invalid script code
to_script="Latn"
)
except HttpResponseError as error:
if error.error:
print(f"Error Code: {error.error.code}")
print(f"Message: {error.error.message}")Install with Tessl CLI
npx tessl i tessl/pypi-azure-ai-translation-text