DEV Community

xidioda xidioda
xidioda xidioda

Posted on • Originally published at khaleejiapi.dev

How We Built an AI Translation API with Google Gemini

Why AI Translation?

Traditional translation APIs (Google Translate, DeepL) work well for general text, but they struggle with:

  1. Arabic dialects — Gulf Arabic vs Egyptian vs Levantine
  2. Formality levels — Formal business Arabic vs casual chat
  3. Context-aware translation — Technical terms, cultural nuances

We decided to use Google Gemini 2.5 Flash as our translation engine. It's fast (< 500ms), understands context, and supports dialect control.

Architecture

Client → /api/v1/translate → Cache Check (Valkey)
                                  ↓ miss
                             Gemini 2.5 Flash
                                  ↓
                             Cache Store (24h TTL)
                                  ↓
                             Response
Enter fullscreen mode Exit fullscreen mode

The Prompt

The key to good AI translation is the prompt. Here's our approach:

Translate the following text from {source} to {target}.
Formality: {formality}
Dialect: {dialect}

Rules:
- Maintain the original meaning and tone
- Use the specified dialect if provided
- Return ONLY the translated text, no explanations
Enter fullscreen mode Exit fullscreen mode

Caching Strategy

Translation is expensive (API credits + latency). We cache aggressively:

  • Cache key: Hash of text + source + target + formality + dialect
  • TTL: 24 hours in Valkey
  • Fallback: In-memory Map if Valkey is unavailable

This gives us 0ms latency on cache hits and reduces our Gemini API costs by ~80%.

SDK Usage

TypeScript

import { KhaleejiAPI } from '@khaleejiapi/sdk';

const client = new KhaleejiAPI({ apiKey: 'YOUR_KEY' });

const result = await client.communication.translateAI({
  text: 'Hello, how are you?',
  target: 'ar',
  formality: 'formal',
  dialect: 'gulf'
});

console.log(result.translated);
// "السلام عليكم، كيف حالكم؟"
Enter fullscreen mode Exit fullscreen mode

Python

from khaleejiapi import KhaleejiAPI

client = KhaleejiAPI(api_key="YOUR_KEY")

result = client.translate(
    text="Hello, how are you?",
    target="ar",
    formality="formal",
    dialect="gulf"
)

print(result["translated"])
# "السلام عليكم، كيف حالكم؟"
Enter fullscreen mode Exit fullscreen mode

Supported Languages

We support 22+ languages, including all major MENA languages:

Code Language Dialect Support
ar Arabic gulf, egyptian, levantine, maghrebi
fa Persian
ur Urdu
tr Turkish
en English
fr French
hi Hindi

Performance

Metric Value
Cache hit latency <5ms
Cache miss latency 300-500ms
Cache hit rate ~75%
Supported languages 22+
Max text length 5,000 chars

Try It

curl -X POST https://khaleejiapi.dev/api/v1/translate \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text": "Good morning", "target": "ar", "dialect": "gulf"}'
Enter fullscreen mode Exit fullscreen mode

Get your free API key at khaleejiapi.dev/signup.


Originally published on KhaleejiAPI Blog. Sign up for a free API key at khaleejiapi.dev/signup — 1,000 requests/month, no credit card required.

Top comments (0)