Googletrans
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
- breaking Version 4.0.0 introduced an async-only API. All synchronous translation and detection methods have been removed. Existing code using synchronous calls will break.
- gotcha Many older tutorials and Stack Overflow answers still recommend installing `googletrans==4.0.0rc1`. This is an outdated pre-release version and may lead to unexpected behavior, bugs, or missing features compared to the latest stable release.
- gotcha Googletrans is an unofficial library that relies on the public Google Translate web API. Google frequently updates its web services, which can occasionally cause the library to stop working or return HTTP 5xx errors (e.g., due to IP bans or API changes).
- gotcha Making too many requests in a short period can lead to temporary IP bans or rate limiting from Google, resulting in connection errors or HTTP 5xx status codes.
- gotcha The Google Translate web API (and thus googletrans) has a maximum character limit of approximately 15,000 characters per single translation request.
Install
-
pip install googletrans
Imports
- Translator
from googletrans import Translator
Quickstart
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())