Build a Multilingual Translation App with the Claude API
Building a translation app with the Claude API means sending text plus a target language to the Messages endpoint, using a system prompt that constrains the model to output only the translation, and handling streaming so users see results as they're generated. Unlike rule-based machine translation engines, Claude understands context, idiom, tone, and formatting, which makes it well suited for product UIs, support tools, and content localization pipelines — not just raw sentence-to-sentence conversion.
This guide covers the practical pieces: prompt structure, handling multiple languages in one request, preserving formatting, streaming for responsiveness, and cost control when translation volume scales. If you're prototyping quickly or don't want to manage Anthropic billing and keys yourself, SubToAPI gives you an HTTPS API over your existing Claude access with application keys and usage metadata, which is handy once a translation feature moves from side project to something your team depends on.
Why Claude for translation instead of a dedicated MT API
Dedicated translation APIs (Google Translate, DeepL) are optimized for raw accuracy on short segments. Claude's advantage shows up when you need:
- Context-aware translation — resolving ambiguous words based on surrounding sentences rather than translating line by line
- Tone and register control — formal vs. casual, marketing copy vs. legal text
- Mixed-content handling — translating text embedded in JSON, Markdown, or HTML while preserving structure
- Instruction following — "translate but keep product names in English" or "translate and summarize"
If your app only needs literal phrase translation at massive scale, a dedicated MT API is cheaper per request. If you need nuance, formatting preservation, or translation as one step in a larger content pipeline, Claude is the better fit.
Designing the prompt
The most common mistake is letting the model add commentary ("Here's the translation:") around the actual output. Lock this down with a strict system prompt.
const systemPrompt = `You are a translation engine. Translate the user's text into ${targetLanguage}.
Rules:
- Output ONLY the translated text, nothing else.
- Preserve line breaks, Markdown formatting, and punctuation style of the original.
- Do not translate proper nouns, brand names, or code snippets.
- If the input is already in ${targetLanguage}, return it unchanged.`;
Keep the user message as just the raw text to translate — don't wrap it in extra instructions, since that tends to leak into the output.
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
system: systemPrompt,
messages: [{ role: "user", content: sourceText }]
})
});
See /docs/messages for the full request/response shape.
Handling multiple target languages per request
For apps that need to show several translations at once (e.g., a dashboard translating a product description into five languages), batch them into a single structured request rather than firing five separate API calls:
const systemPrompt = `Translate the user's text into each of the following languages: ${languages.join(", ")}.
Return a JSON object where each key is the language code and the value is the translated text.
Output ONLY valid JSON, no explanation.`;
This cuts latency and cost compared to sequential calls, at the expense of a slightly larger response you need to parse. Validate the JSON before trusting it in production — ask the model for JSON, but still wrap JSON.parse in a try/catch and fall back to a retry if parsing fails.
Preserving formatting in real content
Translating marketing copy, UI strings, or documentation means the source often contains Markdown, HTML tags, or placeholders like {{username}}. Be explicit that these must survive untouched:
Do not translate text inside {{ }} placeholders.
Do not alter HTML tags or their attributes — only translate the text nodes.
Preserve all Markdown syntax (headings, bold, links) exactly as structured in the source.
For UI string files (e.g., a JSON locale file with hundreds of keys), send the whole file as structured input and ask Claude to return the same structure with translated values. This is more reliable than translating each string independently because the model can infer tone consistency across related keys.
Streaming for responsiveness
Translation requests on longer documents can take several seconds. Streaming lets you render partial output immediately instead of showing a spinner.
const stream = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 2048,
stream: true,
system: systemPrompt,
messages: [{ role: "user", content: sourceText }]
})
});
Parse the SSE chunks and append text deltas directly to your UI's output box as they arrive. Full event details are in /docs/streaming.
Detecting source language automatically
If users don't specify a source language, let Claude detect it rather than running a separate language-detection call:
Detect the language of the input text automatically. Then translate it into ${targetLanguage}.
If the detected language is the same as the target, return the text unchanged.
This saves a round trip and an extra API call compared to running a dedicated detection step first.
Controlling cost as volume grows
Translation apps tend to have high request volume with relatively small payloads, which makes model choice and token budgeting matter more than in low-volume chat apps. Practical steps:
- Use a smaller/faster model for short UI strings, and reserve larger models for long-form or tone-sensitive content
- Cache translations for strings that don't change often (static UI copy, legal boilerplate)
- Batch multiple short strings into one request instead of one call per string
- Track usage per feature or customer so you can see where spend concentrates
If you're building this as a product feature across a team, SubToAPI gives each environment or integration its own sub_live_ key with usage metadata, so you can see translation volume separately from other Claude-powered features without building that tracking yourself. Plans start at Solo €9/month, with team seats on the Team (€19/seat) and Scale (€49/seat) tiers — see /pricing.
Getting started quickly
If you want to go from idea to working endpoint today:
- Sign up and grab an API key at /signup
- Follow /docs/quickstart to send your first translation request
- Add streaming per /docs/streaming once the basic flow works
- If your app needs the model to call other services (e.g., fetching glossary terms before translating), check /docs/tools for tool use
Questions
Can Claude translate between any language pair? Yes — Claude handles a wide range of languages without needing a specific language pair model, since it's not using fixed bilingual dictionaries. Quality varies by language, so test your specific pairs, especially for low-resource languages.
How do I stop Claude from adding explanations to the translation? Use a strict system prompt that explicitly says to output only the translated text, and keep the user message limited to the raw source text. Avoid phrasing like "please translate this" in the user turn, which invites conversational replies.
Is Claude cheaper than a dedicated translation API for high volume? Not usually on a per-character basis. Claude is worth the cost when you need context-aware translation, formatting preservation, or translation combined with other tasks like summarization or tone adjustment in the same request.