We are building a lightweight CLI translation tool that auto-detects source language and respects domain-specific terminology. It is useful for developers who need to embed translation into backend pipelines without managing token costs on every long input.
What you'll need
- Python 3.10 or newer
- The OpenAI SDK:
pip install openai - An Oxlo.ai API key from https://portal.oxlo.ai
Step 1: Initialize the Oxlo.ai client
I always start by verifying the connection. Create a file named translator.py and set up the client pointing at Oxlo.ai.
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "user", "content": "Hello, world!"},
],
)
print(response.choices[0].message.content)
Step 2: Define the translator system prompt
The system prompt controls behavior. I keep it strict: detect source language, preserve formatting, and do not add explanations.
SYSTEM_PROMPT = """You are a precise translation engine. Follow these rules:
1. Detect the source language automatically.
2. Translate the user's text into the target language specified by the user.
3. Preserve all formatting, line breaks, markdown, and code blocks exactly.
4. Use domain-appropriate terminology when a domain is provided.
5. Match the requested tone: formal, casual, or technical.
6. Output only the translated text. Do not add preambles, explanations, or quotation marks around the output."""
Step 3: Build the core translate function
This function wraps the Oxlo.ai call. I use Qwen 3 32B because it handles multilingual reasoning well, and Oxlo.ai's request-based pricing means I do not worry about token count when users paste long paragraphs.
from openai import OpenAI
client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")
SYSTEM_PROMPT = """You are a precise translation engine. Follow these rules:
1. Detect the source language automatically.
2. Translate the user's text into the target language specified by the user.
3. Preserve all formatting, line breaks, markdown, and code blocks exactly.
4. Use domain-appropriate terminology when a domain is provided.
5. Match the requested tone: formal, casual, or technical.
6. Output only the translated text. Do not add preambles, explanations, or quotation marks around the output."""
def translate(text: str, target_language: str, tone: str = "neutral", domain: str = "general") -> str:
user_message = f"""Target language: {target_language}
Tone: {tone}
Domain: {domain}
Text to translate:
{text}"""
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
)
return response.choices[0].message.content.strip()
Step 4: Add streaming for long texts
For longer documents, waiting for the full response is slow. Oxlo.ai supports streaming with no cold starts, so I add a generator version.
def translate_stream(text: str, target_language: str, tone: str = "neutral", domain: str = "general"):
user_message = f"""Target language: {target_language}
Tone: {tone}
Domain: {domain}
Text to translate:
{text}"""
stream = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_message},
],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
yield content
Step 5: Build the CLI wrapper
I wrap the functions in a small argparse CLI so I can pipe files into it. This is the version I actually use in scripts.
import argparse
import sys
def main():
parser = argparse.ArgumentParser(description="CLI translator via Oxlo.ai")
parser.add_argument("--to", required=True, help="Target language")
parser.add_argument("--tone", default="neutral", help="formal, casual, technical, or neutral")
parser.add_argument("--domain", default="general", help="e.g., medical, legal, engineering")
parser.add_argument("--stream", action="store_true", help="Stream the output")
args = parser.parse_args()
text = sys.stdin.read()
if not text.strip():
print("Error: provide text via stdin.", file=sys.stderr)
sys.exit(1)
if args.stream:
for chunk in translate_stream(text, args.to, args.tone, args.domain):
print(chunk, end="", flush=True)
print()
else:
print(translate(text, args.to, args.tone, args.domain))
if __name__ == "__main__":
main()
Run it
Test the tool with a mixed technical sentence. Here is the command and the output I received:
$ echo "The quick brown fox jumps over the lazy dog. 函数在内存中分配了缓冲区。" | python translator.py --to Spanish --tone technical --stream
El rápido zorro marrón salta sobre el perro perezoso. La función asigna un búfer en memoria.
The model correctly detected English and Chinese source segments, preserved the technical register, and returned only the translated text.
Wrap-up
This tool is now usable in shell pipelines and backend services. Two concrete next steps: integrate it into a FastAPI endpoint so other services can call it internally, or add a post-processing step that uses Oxlo.ai embeddings to verify translation consistency against a domain-specific glossary.
Top comments (0)