How to use Google Cloud Translation API
Quick answer
Use the
google-cloud-translate Python client library to access the Google Cloud Translation API. Initialize the TranslationServiceClient with your Google Cloud credentials, then call translate_text with the target language and text to translate.PREREQUISITES
Python 3.8+Google Cloud project with Translation API enabledGoogle Cloud service account key JSON filepip install google-cloud-translate
Setup
Install the official Google Cloud Translation client library and set up authentication using a service account key JSON file. Export the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your key file.
pip install google-cloud-translate output
Collecting google-cloud-translate Downloading google_cloud_translate-3.11.1-py2.py3-none-any.whl (123 kB) Installing collected packages: google-cloud-translate Successfully installed google-cloud-translate-3.11.1
Step by step
This example demonstrates translating English text to Spanish using the Google Cloud Translation API with the Python client library.
import os
from google.cloud import translate
# Ensure your service account key JSON path is set
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your/service-account-key.json"
client = translate.TranslationServiceClient()
project_id = "your-google-cloud-project-id"
location = "global" # or specify region like "us-central1"
parent = f"projects/{project_id}/locations/{location}"
response = client.translate_text(
request={
"parent": parent,
"contents": ["Hello, world!"],
"mime_type": "text/plain",
"source_language_code": "en",
"target_language_code": "es",
}
)
for translation in response.translations:
print(f"Translated text: {translation.translated_text}") output
Translated text: Hola, mundo!
Common variations
- Change
target_language_codeto translate into other languages (e.g.,frfor French). - Use
mime_type="text/html"to translate HTML content preserving tags. - Use asynchronous calls with
asyncioandgoogle-cloud-translateasync client for high throughput.
import asyncio
from google.cloud import translate_v3 as translate
async def async_translate():
client = translate.TranslationServiceAsyncClient()
project_id = "your-google-cloud-project-id"
location = "global"
parent = f"projects/{project_id}/locations/{location}"
response = await client.translate_text(
request={
"parent": parent,
"contents": ["Good morning!"],
"mime_type": "text/plain",
"source_language_code": "en",
"target_language_code": "de",
}
)
for translation in response.translations:
print(f"Translated text: {translation.translated_text}")
asyncio.run(async_translate()) output
Translated text: Guten Morgen!
Troubleshooting
- If you see
DefaultCredentialsError, verify yourGOOGLE_APPLICATION_CREDENTIALSenvironment variable points to a valid service account JSON key. - Enable the Translation API in your Google Cloud Console for your project.
- Check IAM permissions: your service account needs
roles/cloudtranslate.user.
Key Takeaways
- Use the official
google-cloud-translatePython library for easy integration. - Set
GOOGLE_APPLICATION_CREDENTIALSenv var to authenticate with your service account key. - Call
translate_textwith proper project and location identifiers. - Support for async calls and HTML content translation is available.
- Ensure API is enabled and permissions are correctly set in Google Cloud Console.