We are going to build a command line translation tool that preserves tone, named entities, and formatting across more than twenty languages. It runs on Oxlo.ai's flat per-request pricing, so sending a paragraph costs the same as a single word, which makes it practical for localizing large apps or processing long-form content. If you manage product copy, documentation, or agent pipelines that need reliable multilingual output, this is for you.
What you'll need
Python 3.10 or newer, the OpenAI SDK, and an Oxlo.ai API key from https://portal.oxlo.ai. Install the SDK with pip.
pip install openai
Step 1: Set up the Oxlo.ai client
I start by instantiating the OpenAI-compatible client pointing at Oxlo.ai. This single client will handle every request we send to the translation endpoint.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY",
)
Step 2: Write the translation system prompt
Next, I define the system prompt. Good translations need explicit constraints, so I lock in rules for tone, placeholders, and output format.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY",
)
SYSTEM_PROMPT = """You are a professional translation engine. Follow these rules exactly:
1. Translate the user's text from the source language to the target language.
2. Preserve all markdown, HTML tags, placeholders like {{name}}, and formatting.
3. Do not add explanations, preambles, or quotation marks around the output.
4. Preserve the original tone: formal stays formal, casual stays casual.
5. If the input is a JSON array, return a JSON array of translated strings in the same order.
6. Detect the source language automatically if the user specifies "auto".
Source language: {source_lang}
Target language: {target_lang}
"""
Step 3: Build the core translate function
Now I add the core translate function. It formats the prompt with the source and target languages, calls Oxlo.ai, and returns the cleaned output. I use Qwen 3 32B here because it handles multilingual reasoning well, but you can swap in Llama 3.3 70B or Kimi K2.6 without changing any other logic.
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY",
)
SYSTEM_PROMPT = """You are a professional translation engine. Follow these rules exactly:
1. Translate the user's text from the source language to the target language.
2. Preserve all markdown, HTML tags, placeholders like {{name}}, and formatting.
3. Do not add explanations, preambles, or quotation marks around the output.
4. Preserve the original tone: formal stays formal, casual stays casual.
5. If the input is a JSON array, return a JSON array of translated strings in the same order.
6. Detect the source language automatically if the user specifies "auto".
Source language: {source_lang}
Target language: {target_lang}
"""
def translate(text: str, target_lang: str, source_lang: str = "auto") -> str:
prompt = SYSTEM_PROMPT.format(source_lang=source_lang, target_lang=target_lang)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": text},
],
temperature=0.3,
)
return response.choices[0].message.content.strip()
Step 4: Add batch translation
Because Oxlo.ai charges per request rather than per token, I can feed an entire page of UI strings into one call without worrying about input length costs. The batch function serializes the list to JSON, asks the model to return a JSON array, and parses the result.
import json
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY",
)
SYSTEM_PROMPT = """You are a professional translation engine. Follow these rules exactly:
1. Translate the user's text from the source language to the target language.
2. Preserve all markdown, HTML tags, placeholders like {{name}}, and formatting.
3. Do not add explanations, preambles, or quotation marks around the output.
4. Preserve the original tone: formal stays formal, casual stays casual.
5. If the input is a JSON array, return a JSON array of translated strings in the same order.
6. Detect the source language automatically if the user specifies "auto".
Source language: {source_lang}
Target language: {target_lang}
"""
def translate(text: str, target_lang: str, source_lang: str = "auto") -> str:
prompt = SYSTEM_PROMPT.format(source_lang=source_lang, target_lang=target_lang)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": text},
],
temperature=0.3,
)
return response.choices[0].message.content.strip()
def translate_batch(strings: list[str], target_lang: str, source_lang: str = "auto") -> list[str]:
prompt = SYSTEM_PROMPT.format(source_lang=source_lang, target_lang=target_lang)
payload = json.dumps(strings, ensure_ascii=False)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": payload},
],
temperature=0.3,
response_format={"type": "json_object"},
)
raw = response.choices[0].message.content.strip()
result = json.loads(raw)
if isinstance(result, list):
return result
return list(result.values())[0]
Step 5: Wire up the CLI
Finally, I wrap everything in a small CLI so we can point the script at a JSON file and write translated output to disk.
import argparse
import json
from openai import OpenAI
client = OpenAI(
base_url="https://api.oxlo.ai/v1",
api_key="YOUR_OXLO_API_KEY",
)
SYSTEM_PROMPT = """You are a professional translation engine. Follow these rules exactly:
1. Translate the user's text from the source language to the target language.
2. Preserve all markdown, HTML tags, placeholders like {{name}}, and formatting.
3. Do not add explanations, preambles, or quotation marks around the output.
4. Preserve the original tone: formal stays formal, casual stays casual.
5. If the input is a JSON array, return a JSON array of translated strings in the same order.
6. Detect the source language automatically if the user specifies "auto".
Source language: {source_lang}
Target language: {target_lang}
"""
def translate(text: str, target_lang: str, source_lang: str = "auto") -> str:
prompt = SYSTEM_PROMPT.format(source_lang=source_lang, target_lang=target_lang)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": text},
],
temperature=0.3,
)
return response.choices[0].message.content.strip()
def translate_batch(strings: list[str], target_lang: str, source_lang: str = "auto") -> list[str]:
prompt = SYSTEM_PROMPT.format(source_lang=source_lang, target_lang=target_lang)
payload = json.dumps(strings, ensure_ascii=False)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": payload},
],
temperature=0.3,
response_format={"type": "json_object"},
)
raw = response.choices[0].message.content.strip()
result = json.loads(raw)
if isinstance(result, list):
return result
return list(result.values())[0]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Translate JSON string arrays with Oxlo.ai")
parser.add_argument("--input", required=True, help="Input JSON file containing a string array")
parser.add_argument("--output", required=True, help="Output JSON file")
parser.add_argument("--target", default="Spanish", help="Target language")
parser.add_argument("--source", default="auto", help="Source language")
args = parser.parse_args()
with open(args.input, "r", encoding="utf-8") as f:
source_strings = json.load(f)
translations = translate_batch(source_strings, args.target, args.source)
with open(args.output, "w", encoding="utf-8") as f:
json.dump(translations, f, ensure_ascii=False, indent=2)
print(f"Translated {len(translations)} strings to {args.target}.")
Run it
Create an input file named input.json with a few UI strings that include placeholders.
[
"Welcome back, {{username}}",
"Your subscription expires on {{date}}.",
"Click here to upgrade."
]
Then run the script.
python translator.py --input input.json --output es.json --target "Spanish"
You should see this output, and es.json should contain the translated array.
Translated 3 strings to Spanish.
Contents of es.json:
[
"Bienvenido de nuevo, {{username}}",
"Tu suscripción expira el {{date}}.",
"Haz clic aquí para actualizar."
]
Next steps
Wire this script into your CI pipeline to localize UI strings on every deploy, or wrap the translate function in a FastAPI endpoint so multiple services can share one Oxlo.ai integration. If you need deeper reasoning for legal or medical translations, swap qwen-3-32b for deepseek-v3.2 or kimi-k2.6 in the model string and rerun the same code.
Top comments (0)