DEV Community

shashank ms
shashank ms

Posted on

Building a Language Translator with LLM: A Tutorial

I built a small CLI translator for internal docs last month. It detects the source language, respects markdown formatting, and returns clean JSON so I can pipe it into other tools. In this tutorial I will walk through the exact code so you can adapt it for your own pipelines.

What you'll need

Step 1: Initialize the Oxlo.ai client

I start by pointing the OpenAI SDK at Oxlo.ai. Because Oxlo.ai is fully OpenAI API compatible, this is a single line change. I use qwen-3-32b here because its multilingual training handles nuance across European and Asian languages well.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.environ.get("OXLO_API_KEY")
)

# Quick smoke test
response = client.chat.completions.create(
    model="qwen-3-32b",
    messages=[{"role": "user", "content": "Translate 'hello world' to Spanish"}],
    temperature=0.1,
)
print(response.choices[0].message.content)

Step 2: Lock down the system prompt

Translation models wander if you do not constrain them. They add explanations, pronunciation guides, or quotation marks. I keep a strict system prompt in a module-level constant so I can version it.

SYSTEM_PROMPT = """You are a professional translation engine.
Rules:
1. Detect the source language if it is not provided.
2. Translate the user's text into the requested target language.
3. Preserve all markdown, code blocks, URLs, and formatting exactly.
4. Do not add explanations, preambles, or quotation marks around the output.
5. Respond in raw text only."""

Step 3: Build the core translate function

Now I wrap the call. I accept source language as optional and default to auto-detection. I also set temperature low to keep translations deterministic.

def translate(text: str, target_lang: str, source_lang: str | None = None) -> str:
    hint = f"The source language is {source_lang}." if source_lang else "Detect the source language."
    user_message = f"{hint}\nTranslate into {target_lang}:\n{text}"

    response = client.chat.completions.create(
        model="qwen-3-32b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
        temperature=0.1,
    )
    return response.choices[0].message.content.strip()

Step 4: Enforce structured output with JSON mode

For production use I do not want to parse free text. Oxlo.ai supports JSON mode, so I update the prompt to emit a JSON object and set response_format. This makes downstream validation trivial.

SYSTEM_PROMPT_JSON = """You are a professional translation engine.
Rules:
1. Detect the source language if it is not provided.
2. Translate the user's text into the requested target language.
3. Preserve all markdown, code blocks, URLs, and formatting exactly.
4. Do not add explanations, preambles, or quotation marks around the output.
Respond with a JSON object in this exact schema:
{
  "detected_source_language": "string",
  "translation": "string"
}"""

import json

def translate_json(text: str, target_lang: str, source_lang: str | None = None) -> dict:
    hint = f"The source language is {source_lang}." if source_lang else "Detect the source language."
    user_message = f"{hint}\nTranslate into {target_lang}:\n{text}"

    response = client.chat.completions.create(
        model="qwen-3-32b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT_JSON},
            {"role": "user", "content": user_message},
        ],
        response_format={"type": "json_object"},
        temperature=0.1,
    )
    return json.loads(response.choices[0].message.content)

Step 5: Add a CLI interface

I use argparse so teammates can run this from a Makefile or CI pipeline. The script reads from stdin or a positional string.

import argparse

def main():
    parser = argparse.ArgumentParser(description="Translate text via Oxlo.ai")
    parser.add_argument("--target", "-t", required=True, help="Target language, e.g. Japanese")
    parser.add_argument("--source", "-s", default=None, help="Optional source language, e.g. English")
    parser.add_argument("text", help="Text to translate, or use - for stdin")
    args = parser.parse_args()

    if args.text == "-":
        import sys
        text = sys.stdin.read()
    else:
        text = args.text

    result = translate_json(text, args.target, args.source)
    print(result["translation"])

if __name__ == "__main__":
    main()

Run it

Save the full script as translator.py, export your key, and run:

export OXLO_API_KEY="sk-oxlo.ai-..."
python translator.py -t Japanese "Deploy the container to the staging cluster."

Example output:

{
  "detected_source_language": "English",
  "translation": "コンテナをステージングクラスタにデプロイしてください。"
}

For longer markdown files, pipe stdin through the script:

python translator.py -t Spanish -s English - < README.md

The model preserves headers and code fences. Because Oxlo.ai uses request-based pricing, the cost stays flat even when I feed it an entire markdown file in one shot. For teams running long-context localization batches, that predictable per-request cost is a significant advantage over token-based providers. You can compare plans at https://oxlo.ai/pricing.

Wrap-up

Two concrete next steps. First, add a terminology glossary loaded from a YAML file and inject it into the system prompt so product names and technical terms stay consistent across translations. Second, wrap the translate_json function in a FastAPI endpoint and deploy it as an internal microservice so your frontend and mobile apps can share the same translation layer.

Top comments (0)