AI Translation and Localization: Building Multilingual Applications
Use AI to build truly global applications
AI Translation and Localization
Beyond Google Translate
Modern AI translation is dramatically better, but building production-grade multilingual apps requires more than just API calls.Translation with Modern LLMs
python
import openaidef translate_with_context(text: str, source_lang: str, target_lang: str, context: str = "") -> str:
client = openai.OpenAI()
system_prompt = f"""You are a professional translator specializing in {target_lang}.
Translate accurately while preserving:
- Tone and voice
- Cultural nuances
- Domain-specific terminology
- Formatting (markdown, HTML tags)
Context: {context}"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Translate from {source_lang} to {target_lang}:\n\n{text}"}
]
)
return response.choices[0].message.content
Building a Translation Pipeline
python
import deepl # DeepL API for high-quality EU language translation
from typing import Dictclass TranslationPipeline:
def __init__(self):
self.deepl = deepl.Translator(DEEPL_API_KEY)
self.openai = openai.OpenAI()
self.cache = {}
def translate(self, text: str, target_lang: str, source_lang: str = "EN") -> str:
cache_key = f"{source_lang}:{target_lang}:{hash(text)}"
if cache_key in self.cache:
return self.cache[cache_key]
# Use DeepL for European languages (better quality)
eu_langs = ["DE", "FR", "ES", "IT", "PT", "NL", "PL"]
if target_lang in eu_langs:
result = self.deepl.translate_text(text, target_lang=target_lang)
translation = result.text
else:
# Use GPT-4o for Asian, Middle Eastern languages
translation = translate_with_context(text, source_lang, target_lang)
self.cache[cache_key] = translation
return translation
i18n Framework Integration
typescript
// next-intl with AI-generated translations
import { getTranslations } from 'next-intl/server';async function generateMissingTranslations(baseMessages: object, targetLocale: string) {
const missing = findMissingKeys(baseMessages, loadLocale(targetLocale));
for (const [key, value] of Object.entries(missing)) {
const translation = await translateWithContext(
value as string,
'en',
targetLocale,
getContextForKey(key)
);
saveTranslation(targetLocale, key, translation);
}
}
Cultural Adaptation
Translation is not just language - it's culture:Quality Assurance
Also available in 中文.