Translation APIs Beginner

Cloud translation APIs let you add multilingual capabilities to your applications without training or hosting your own models. This lesson covers the three most popular services: Google Cloud Translation, DeepL, and Azure Translator.

API Comparison

Feature Google Translate DeepL Azure Translator
Languages 130+ 30+ 100+
Quality Very good Excellent (European) Very good
Free tier 500K chars/month 500K chars/month 2M chars/month
Document translation Yes Yes Yes
Glossaries Yes Yes Yes

Google Cloud Translation

Bash
pip install google-cloud-translate
Python
from google.cloud import translate_v2 as translate

client = translate.Client()

# Translate text
result = client.translate(
    "Hello, how are you?",
    target_language="es"
)
print(result["translatedText"])
# "Hola, ¿cómo estás?"

# Detect language
detection = client.detect_language("Bonjour le monde")
print(detection["language"])  # "fr"

# Batch translation
texts = ["Good morning", "Thank you", "Goodbye"]
results = client.translate(texts, target_language="ja")
for r in results:
    print(f"{r['input']} -> {r['translatedText']}")

DeepL API

Python
import deepl

translator = deepl.Translator("YOUR_DEEPL_API_KEY")

# Translate text with formality control
result = translator.translate_text(
    "How are you doing today?",
    target_lang="DE",
    formality="more"  # Use formal register
)
print(result.text)
# "Wie geht es Ihnen heute?"

# Translate with glossary (enforce specific terms)
glossary = translator.create_glossary(
    "Tech Terms",
    source_lang="EN",
    target_lang="DE",
    entries={"machine learning": "maschinelles Lernen"}
)
result = translator.translate_text(
    "We use machine learning for predictions.",
    target_lang="DE",
    glossary=glossary
)

Azure Translator

Python
import requests

endpoint = "https://api.cognitive.microsofttranslator.com"
headers = {
    "Ocp-Apim-Subscription-Key": "YOUR_AZURE_KEY",
    "Ocp-Apim-Subscription-Region": "eastus",
    "Content-Type": "application/json",
}

# Translate to multiple languages at once
body = [{"text": "Hello, world!"}]
params = {"api-version": "3.0", "to": ["fr", "de", "ja"]}

response = requests.post(
    f"{endpoint}/translate",
    headers=headers, params=params, json=body
)

for translation in response.json()[0]["translations"]:
    print(f"{translation['to']}: {translation['text']}")

Using Open-Source Models Locally

If you prefer to run translation locally without an API, use MarianMT via Hugging Face:

Python
from transformers import MarianMTModel, MarianTokenizer

model_name = "Helsinki-NLP/opus-mt-en-de"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

texts = ["Hello, how are you?", "Machine learning is fascinating."]
inputs = tokenizer(texts, return_tensors="pt", padding=True)
translated = model.generate(**inputs)
results = tokenizer.batch_decode(translated, skip_special_tokens=True)

for src, tgt in zip(texts, results):
    print(f"{src} -> {tgt}")
Which API should you choose? Use DeepL for the highest quality European language translations. Use Google Translate for the broadest language coverage. Use Azure Translator for the most generous free tier and enterprise integrations. Use MarianMT for offline, privacy-preserving, or cost-free translation.

Try It Yourself

Choose one API and translate a paragraph between 3 different languages. Compare the quality with a local MarianMT model for the same language pair.

Next: Fine-Tuning →