How to translate text between 100+ languages using the Claude API in Python. Covers single-string translation, batch translation, language detection, and quality-controlled output with Pydantic.
Claude translates between 100+ languages with strong contextual understanding — it handles idioms, technical terminology, and register (formal/informal) better than pure machine translation systems. This guide covers every common translation pattern in Python.
pip install anthropic
import anthropic
client = anthropic.Anthropic()
def translate(text: str, target_language: str) -> str:
message = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=2048,
system=f"You are a professional translator. Translate the user's text into {target_language}. Return only the translated text, no explanation.",
messages=[{"role": "user", "content": text}]
)
return message.content[0].text
result = translate("Hello, how are you today?", "Spanish")
print(result) # "Hola, ¿cómo estás hoy?"
import json
def translate_with_metadata(text: str, target_language: str) -> dict:
message = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=2048,
system=(
"You are a professional translator. Return a JSON object with keys: "
"'source_language' (detected), 'translation' (translated text), "
"'confidence' ('high'/'medium'/'low'). No markdown fences."
),
messages=[{"role": "user", "content": f"Translate to {target_language}:
{text}"}]
)
return json.loads(message.content[0].text)
result = translate_with_metadata("Bonjour le monde", "English")
# {"source_language": "French", "translation": "Hello, world", "confidence": "high"}
def translate_markup(html: str, target_language: str) -> str:
message = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=4096,
system=(
f"Translate human-readable text to {target_language}. "
"Preserve all HTML tags, attributes, href values, and Markdown syntax exactly. "
"Return only the translated markup."
),
messages=[{"role": "user", "content": html}]
)
return message.content[0].text
html_input = "Welcome to our platform. Learn more.
"
print(translate_markup(html_input, "German"))
strings = [
"Save changes",
"Cancel",
"Are you sure you want to delete this item?",
"Profile updated successfully.",
]
target = "Japanese"
requests = [
{
"custom_id": f"str-{i}",
"params": {
"model": "claude-haiku-4-5-20251001",
"max_tokens": 512,
"system": f"Translate to {target}. Return only the translation.",
"messages": [{"role": "user", "content": s}]
}
}
for i, s in enumerate(strings)
]
batch = client.messages.batches.create(requests=requests)
print(f"Batch submitted: {batch.id}") # poll within 24h for results at 50% cost
MEDICAL_GLOSSARY = {
"myocardial infarction": "心肌梗塞",
"hypertension": "高血压",
"electrocardiogram": "心电图",
}
glossary_text = "
".join(f"{k} → {v}" for k, v in MEDICAL_GLOSSARY.items())
def translate_medical(text: str) -> str:
message = client.messages.create(
model="claude-sonnet-4-6", # use Sonnet for critical medical content
max_tokens=4096,
system=(
f"Translate medical text to Simplified Chinese.
"
f"Apply this glossary exactly:
{glossary_text}
"
"Preserve all medical term precision."
),
messages=[{"role": "user", "content": text}]
)
return message.content[0].text
| Criterion | Claude API | Google Translate | DeepL |
|---|---|---|---|
| Languages supported | 100+ | 133 | 33 |
| Context/idiom handling | Excellent | Good | Very good |
| Terminology/glossary | Prompt-based, flexible | Custom model (paid) | Glossary API |
| Formatting preservation | Instruction-based | HTML mode built-in | Tag handling built-in |
| Speed (short strings) | ~1–2s | ~100ms | ~200ms |
| Cost per 1M chars | ~$1–6 (Haiku–Sonnet) | $20 | $25 |
| Best for | Long-form, nuanced, domain-specific | Real-time, high-volume | European language pairs |
For bulk translation jobs, estimate costs with the Claude API Cost Calculator. For text summarization across languages, see the summarization guide.