Mastering Google Cloud Translation API with Python: A Developer's Guide

Published: (January 20, 2026 at 12:07 AM EST)
4 min read
Source: Dev.to

Source: Dev.to

📋 Prerequisites

  • A Google Account.
  • A credit card (required for Google Cloud billing account setup, even if you stay within the free tier).
  • Python 3.7+ installed on your machine.
  • pip for package installation.

1️⃣ Create a Google Cloud Project

  1. Open the Cloud Console

    • Navigate to in your web browser.
  2. Create a New Project

    • Click the project dropdown in the top‑left corner (it usually shows “My First Project” or your current project name).
    • In the pop‑up, click “New Project”.
    • Project name: Python-Translation-Project (or any clear name you prefer).
    • Location: optional – choose an organization/folder if you have one, otherwise leave as “No organization”.
    • Click “Create” and wait a few moments for provisioning.
  3. Select Your New Project

    • Once ready, select the project from the dropdown menu.

2️⃣ Enable the Cloud Translation API

  1. Open the API Library

    • Click the navigation menu (☰) in the top‑left corner.
    • Go to APIs & Services → Library.
  2. Search & Enable

    • In the search bar, type “Cloud Translation API”.
    • Click the “Cloud Translation API” result.
    • Press the large blue “ENABLE” button.

Troubleshooting: If you later see a 403 Forbidden error, revisit this step. It can take a few minutes for the enablement to propagate, or the API might not be enabled for the project your code is using.

3️⃣ Create a Service Account (for authentication)

  1. Open Credentials

    • Click the navigation menu (☰).
    • Go to APIs & Services → Credentials.
  2. Create the Service Account

    • Click + CREATE CREDENTIALSService account.
    • Service account name: translation-service-account (or similar).
    • Description (optional): “Service account for Python Translation API”.
    • Click “CREATE AND CONTINUE”.
  3. Grant Permissions

    • Under “Grant this service account access to project”, click Select a role.
    • Search for and select “Cloud Translation API User”.
    • Click “CONTINUE”.
  4. Finish

    • (Optional) Skip “Grant users access to this service account”.
    • Click “DONE”.
  5. Download the JSON Key

    • Back on the Credentials page, locate your new service account under Service Accounts.
    • Click its email address → Keys tab → ADD KEY → Create new key.
    • Choose JSON (default & recommended) and click CREATE.
    • A .json file downloads automatically. Keep this file secure – it’s the “password” for your service account.

4️⃣ Project Setup & Installation (Local Machine)

4.1 Create a Project Directory

mkdir my-translation-app
cd my-translation-app

4.2 Move Your Credentials

mkdir credentials
mv /path/to/your/downloaded-key.json credentials/google_key.json

(Rename the file to google_key.json for simplicity.)

4.3 Set Up a Virtual Environment

python3 -m venv venv
# On macOS/Linux
source venv/bin/activate
# On Windows
venv\Scripts\activate

4.4 Install the Client Library

pip install google-cloud-translate

5️⃣ Write the Translation Script

Create a file named translate_text.py in the project root and paste the following code:

import os
from google.cloud import translate_v2 as translate

# 👉 IMPORTANT: Point to your service‑account key file.
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials/google_key.json"

def translate_single_text(text: str, target_language_code: str) -> None:
    """
    Translates a single piece of text to the target language.
    """
    # Initialise the client
    client = translate.Client()

    # Perform translation
    result = client.translate(text, target_language=target_language_code)

    print(f"Original Text: {text}")
    print(f"Target Language: {target_language_code}")
    print(f"Translated Text: {result['translatedText']}")
    print(f"Detected Source Language: {result['detectedSourceLanguage']}")

if __name__ == "__main__":
    my_text = "Hello world, this is a test of the Google Cloud Translation API."

    # Example translations
    translate_single_text(my_text, "es")   # Spanish
    print("-" * 30)
    translate_single_text(my_text, "fr")   # French
    print("-" * 30)
    translate_single_text("Wie geht es dir?", "en")  # German → English

5.1 Run the Script

python translate_text.py

You should see output similar to:

Original Text: Hello world, this is a test of the Google Cloud Translation API.
Target Language: es
Translated Text: Hola mundo, esto es una prueba de la API de traducción de Google Cloud.
Detected Source Language: en
------------------------------
Original Text: Hello world, this is a test of the Google Cloud Translation API.
Target Language: fr
Translated Text: Bonjour le monde, ceci est un test de l'API de traduction Google Cloud.
Detected Source Language: en
------------------------------
Original Text: Wie geht es dir?
Target Language: en
Translated Text: How are you?
Detected Source Language: de

🎉 What’s Next?

  • Explore more languages – use any ISO‑639‑1 code (e.g., ja for Japanese).
  • Batch translations – translate multiple strings in one request.
  • Advanced features – glossary support, model selection, etc.

Happy coding! 🚀

Translation API Results

Target Language: es
Detected Source Language: en

Original Text

Hello world, this is a test of the Google Cloud Translation API.

Translated Text

Hola mundo, esta es una prueba de la API de traducción de Google Cloud.


Target Language: fr
Detected Source Language: en

Original Text

Hello world, this is a test of the Google Cloud Translation API.

Translated Text

Bonjour le monde, ceci est un test de l’API de traduction de Google Cloud.


Original Text

Wie geht es dir?

Target Language: en
Detected Source Language: de

Translated Text

How are you?


.gitignore

Create a .gitignore file in the root of your my-translation-app directory before pushing any code to GitHub.

# Credentials and Secrets
credentials/
.env

# Python Environment
venv/
__pycache__/
*.py[cod]

This prevents sensitive files such as google_key.json and your virtual environment from being accidentally committed to version control.

Next Steps

You have now:

  1. Set up a Google Cloud Project.
  2. Enabled the Translation API.
  3. Configured a secure Service Account.
  4. Written a Python script to translate text.

This foundational setup is essential for more advanced translation tasks, such as integrating the service into larger data pipelines or web applications.

Happy translating!

Back to Blog

Related posts

Read more »