Googletrans

4.0.2 · active · verified Thu Apr 09

Googletrans is a free and unlimited Python library that implements the Google Translate API. It leverages the Google Translate Ajax API for language detection and text translation. As of version 4.0.2, the library features a modern async-only API, support for bulk translations, automatic language detection, and proxy configurations. It is compatible with Python 3.8+ and is actively maintained.

Warnings

Install

Imports

Quickstart

Initializes a Translator instance to translate a single string, multiple strings in a batch, and detect the language of a given text. Note that the API is now async-only.

import asyncio
from googletrans import Translator

async def translate_text():
    translator = Translator()
    text_to_translate = "Hello, how are you?"
    
    # Translate a single text
    translation = await translator.translate(text_to_translate, dest='es')
    print(f"Original: {translation.origin}, Translated (ES): {translation.text}")

    # Translate multiple texts
    texts = ["The quick brown fox", "jumps over", "the lazy dog"]
    translations = await translator.translate(texts, dest='fr')
    for t in translations:
        print(f"Original: {t.origin} -> Translated (FR): {t.text}")

    # Detect language
    detection = await translator.detect("Bonjour")
    print(f"Detected language: {detection.lang} with confidence {detection.confidence}")

if __name__ == "__main__":
    asyncio.run(translate_text())

view raw JSON →