Google Cloud Translation API: Setup Guide
Google Cloud Translation API: Setup Guide
Google Cloud Translation is one of the most widely used translation APIs, supporting 130+ languages with a 500K characters/month free tier. This guide walks you through the complete setup process.
Translation comparisons are based on automated metrics and editorial evaluation. Quality varies by language pair and content type.
Choose Your API Version
Google offers two versions:
| Feature | Basic (v2) | Advanced (v3) |
|---|---|---|
| Price | $20/1M chars | $80/1M chars |
| Free tier | 500K chars/month | No free tier |
| Languages | 130+ | 130+ |
| Glossary | No | Yes |
| Custom models (AutoML) | No | Yes |
| Batch translation | No | Yes |
| Adaptive Translation | No | Yes |
| Model selection | No | Yes |
Recommendation: Start with Basic (v2) for most use cases. Upgrade to Advanced (v3) when you need glossary support, batch translation, or custom models.
Step 1: Create a Google Cloud Project
- Go to Google Cloud Console
- Click “Select a project” in the top bar, then “New Project”
- Name your project and click “Create”
- Note your project ID (you will need it for API calls)
Step 2: Enable the Translation API
- In the Cloud Console, go to “APIs & Services” > “Library”
- Search for “Cloud Translation API”
- Click “Enable”
Step 3: Create API Credentials
Option A: API Key (simplest)
- Go to “APIs & Services” > “Credentials”
- Click “Create Credentials” > “API Key”
- Copy and securely store your API key
- Recommended: Restrict the key to the Translation API only
Option B: Service Account (recommended for production)
- Go to “APIs & Services” > “Credentials”
- Click “Create Credentials” > “Service Account”
- Name it and grant the “Cloud Translation API User” role
- Create a JSON key and download it
- Set the environment variable:
export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"
Step 4: Make Your First API Call
Using curl (Basic v2)
curl -X POST \
"https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"q": "Hello, world!", "target": "es"}'
Using Python (Basic v2)
import requests
API_KEY = "YOUR_API_KEY"
url = f"https://translation.googleapis.com/language/translate/v2?key={API_KEY}"
response = requests.post(url, json={
"q": "Hello, how are you?",
"target": "es",
"source": "en"
})
result = response.json()
print(result["data"]["translations"][0]["translatedText"])
# "Hola, ¿cómo estás?"
Using Python SDK (Advanced v3)
from google.cloud import translate_v3 as translate
client = translate.TranslationServiceClient()
project_id = "your-project-id"
parent = f"projects/{project_id}/locations/global"
response = client.translate_text(
request={
"parent": parent,
"contents": ["Hello, how are you?"],
"mime_type": "text/plain",
"source_language_code": "en",
"target_language_code": "es",
}
)
for translation in response.translations:
print(translation.translated_text)
Install the SDK: pip install google-cloud-translate
Step 5: Language Detection
Google can auto-detect the source language:
response = requests.post(url, json={
"q": "Bonjour le monde",
"target": "en"
# No "source" field — auto-detection
})
result = response.json()
detected = result["data"]["translations"][0]["detectedSourceLanguage"]
print(f"Detected: {detected}") # "fr"
Step 6: Set Up Glossary (v3 Only)
Glossaries enforce consistent terminology translation:
from google.cloud import translate_v3 as translate
client = translate.TranslationServiceClient()
parent = f"projects/{project_id}/locations/us-central1"
glossary_id = "my-glossary"
glossary = translate.Glossary(
name=f"{parent}/glossaries/{glossary_id}",
language_pair=translate.Glossary.LanguageCodePair(
source_language_code="en",
target_language_code="es"
),
input_config=translate.GlossaryInputConfig(
gcs_source=translate.GcsSource(
input_uri="gs://your-bucket/glossary.csv"
)
)
)
operation = client.create_glossary(parent=parent, glossary=glossary)
result = operation.result()
Glossary CSV format:
en,es
API key,clave de API
repository,repositorio
deploy,desplegar
Cost Management
Set Budget Alerts
- Go to “Billing” > “Budgets & alerts”
- Create a budget for your project
- Set threshold alerts (e.g., 50%, 75%, 100% of budget)
Monitor Usage
- Go to “APIs & Services” > “Dashboard”
- Select “Cloud Translation API”
- View request counts and character counts
Set Quotas
- Go to “APIs & Services” > “Quotas”
- Search for Translation API quotas
- Set per-minute and per-day limits to prevent runaway costs
Translation API Pricing Calculator
Common Patterns
Batch Translation (v3)
For translating large volumes, use batch translation to process files asynchronously:
operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": "en",
"target_language_codes": ["es", "fr", "de"],
"input_configs": [{"gcs_source": {"input_uri": "gs://bucket/input.txt"}}],
"output_config": {"gcs_destination": {"output_uri_prefix": "gs://bucket/output/"}}
}
)
HTML Translation
Preserve HTML tags while translating content:
response = requests.post(url, json={
"q": "<p>Hello <strong>world</strong></p>",
"target": "es",
"format": "html"
})
# Returns: "<p>Hola <strong>mundo</strong></p>"
Key Takeaways
- Google Cloud Translation is straightforward to set up and offers a generous 500K character/month free tier on the Basic API.
- Start with the Basic (v2) API and API key authentication for simplicity. Upgrade to Advanced (v3) when you need glossaries, batch translation, or custom models.
- Always set budget alerts and quotas to prevent unexpected charges.
- The glossary feature (v3 only) is essential for maintaining terminology consistency in professional translation.
Next Steps
- Compare with other APIs: Read Translation AI for Developers: API Comparison and Integration Guide.
- Set up DeepL: See DeepL API: Integration Tutorial.
- Compare with NLLB: Read NLLB-200 vs Google Translate: Accuracy by Language Pair.
- Self-host alternative: See How to Set Up NLLB-200 Locally: Tutorial.
- Calculate costs: Use Translation API Pricing Calculator.