Translation pipelines often break when they hit context windows or balloon in cost as source documents grow. In this tutorial, I will walk through a small but robust Python translator that uses Oxlo.ai's OpenAI-compatible API to convert text while preserving formatting and placeholders. It is useful for developers automating localization, documentation, or multilingual content workflows.
What you'll need
Before starting, gather the following:
- Python 3.10 or newer
- An Oxlo.ai API key from https://portal.oxlo.ai
- The OpenAI SDK installed with
pip install openai
I also recommend exporting your key as an environment variable so it never leaks into source control.
Step 1: Set up the Oxlo.ai client
Create a file named translator.py and initialize the client. I keep the base URL and key at the top so the rest of the script stays portable.
from openai import OpenAI
import os
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key=os.environ.get("OXLO_API_KEY"),
)
Step 2: Define the system prompt
A good translator needs strict instructions. The system prompt below locks the model into returning only the translation, and it protects placeholders and markdown.
SYSTEM_PROMPT = """You are a professional translation engine. Translate the user's text into the target language they specify. Follow these rules exactly:
- Preserve all markdown, HTML tags, and code blocks.
- Do not alter placeholders such as {variable}, {{template}}, or %s.
- Do not add explanations, preambles, or quotation marks around the output.
- Return only the translated text."""
Step 3: Build the core translate function
Now I will add a function that sends text to Oxlo.ai. I keep the temperature low for deterministic output. Because Oxlo.ai uses flat per-request pricing rather than tokens, sending a long paragraph costs the same as a short one, which makes bulk document translation predictable. You can review the exact structure at https://oxlo.ai/pricing.
def translate(text: str, target_language: str, model: str = "llama-3.3-70b") -> str:
user_message = f"Translate the following text into {target_language}:\n\n{text}"
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
temperature=0.3,
)
return response.choices[0].message.content.strip()
Step 4: Stream large responses
For long files, streaming lets us process tokens as they arrive instead of buffering everything in memory. I switch to qwen-3-32b here because it offers strong multilingual reasoning.
def translate_stream(text: str, target_language: str, model: str = "qwen-3-32b"):
user_message = f"Translate the following text into {target_language}:\n\n{text}"
stream = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
temperature=0.3,
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print()
Step 5: Wrap it in a CLI
Finally, I will add a small command-line interface that reads an input file, translates the contents, and writes the result to disk.
if __name__ == "__main__":
import sys
if len(sys.argv) != 4:
print("Usage: python translator.py <input_file> <target_language> <output_file>")
sys.exit(1)
_, in_path, target_lang, out_path = sys.argv
with open(in_path, "r", encoding="utf-8") as f:
source_text = f.read()
print(f"Translating {len(source_text)} characters to {target_lang}...")
result = translate(source_text, target_lang, model="deepseek-v3.2")
with open(out_path, "w", encoding="utf-8") as f:
f.write(result)
print(f"Done. Output written to {out_path}")
Run it
Create a file named sample.txt with the following content:
The API key is stored in the {config_path} variable.
Please do not share it publicly.
Then run the script:
python translator.py sample.txt Spanish output.txt
You should see output similar to this:
Translating 74 characters to Spanish...
Done. Output written to output.txt
cat output.txt
La clave de API se almacena en la variable {config_path}.
Por favor, no la comparta públicamente.
Next steps
Now that the core script works, you can extend it. Two concrete directions:
- Integrate it into a CI pipeline, such as GitHub Actions, to automatically translate documentation or README files on every release.
- Build a lightweight web UI with Gradio or Streamlit and host it internally, keeping Oxlo.ai as the backend for all inference.
Top comments (0)