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.
pip install googletransVerified import paths — ran on the pinned version, not inferred.
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.
Rewrite code to use `await` with `async/await` syntax and run within an `asyncio` event loop. For example, `translator.translate('text')` becomes `await translator.translate('text')`.Always install the latest stable version using `pip install googletrans`. If you encounter issues, consider uninstalling `googletrans` and then reinstalling the stable version.
Implement robust error handling, rate limiting, and retry mechanisms. Consider using `service_urls` parameter to rotate through different Google Translate domains or use proxies. For critical applications requiring high stability and rate limits, consider Google's official Cloud Translation API.
Introduce delays between requests, especially in loops or batch processes (e.g., `time.sleep(1)` or more). Implement exponential backoff for retries. Consider using proxies if making a very high volume of requests.
For longer texts, split them into smaller chunks and translate them individually, then reassemble the translated parts.
Explicitly define `service_urls` when initializing the `Translator` object, for example: `translator = Translator(service_urls=['translate.googleapis.com'])`. For some users, installing a specific pre-release like `pip install googletrans==4.0.0rc1` or `pip install googletrans==3.1.0a0` has also resolved it.
Install the library using pip: `pip install googletrans`. Ensure you are installing it into the correct Python environment if you are using virtual environments or multiple Python installations.
Rewrite your translation code to use Python's `async` and `await` syntax. Define an `async` function, instantiate the `Translator` within an `async with` block, and `await` the `translate` call.
```python
import asyncio
from googletrans import Translator
async def translate_text(text, dest_lang='en'):
async with Translator() as translator:
result = await translator.translate(text, dest=dest_lang)
return result.text
# Example usage:
# translated_string = asyncio.run(translate_text('안녕하세요.'))
# print(translated_string)
```To mitigate rate limiting, introduce delays between translation requests, consider using a proxy, or provide multiple service URLs to the `Translator` constructor to distribute requests.
```python
from googletrans import Translator
import time
translator = Translator(service_urls=[
'translate.google.com',
'translate.google.co.kr',
'translate.google.cn'
])
def translate_with_delay(text, dest_lang='en'):
# In an async context, you would use await asyncio.sleep(delay)
time.sleep(1) # Add a delay between requests
result = translator.translate(text, dest=dest_lang)
return result.text
# Note: For async API (googletrans 4.0.2), this synchronous example
# would need to be adapted to use async/await with an async sleep.
```