Engineeringaigeminitranslation

How We Built an AI Translation API with Google Gemini

A deep dive into building our AI-powered translation endpoint using Google Gemini 2.5 Flash — supporting 22+ languages with dialect and formality control.

KhaleejiAPI TeamFebruary 22, 20266 min read

Why AI Translation?

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

  • Arabic dialects — Gulf Arabic vs Egyptian vs Levantine
  • Formality levels — Formal business Arabic vs casual chat
  • 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

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

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); // "السلام عليكم، كيف حالكم؟"

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"]) # "السلام عليكم، كيف حالكم؟"

Supported Languages

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

CodeLanguageDialect Support
arArabicgulf, egyptian, levantine, maghrebi
faPersian
urUrdu
trTurkish
enEnglish
frFrench
hiHindi

Performance

MetricValue
Cache hit latency<5ms
Cache miss latency300-500ms
Cache hit rate~75%
Supported languages22+
Max text length5,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"}'

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