deep-translator library

1.11.4 · active · verified Sat Apr 11

deep-translator is a versatile Python library designed for translating text between various languages using a multitude of free and commercial online translation services. It is currently at version 1.11.4 and maintains an active development pace with frequent updates, bug fixes, and additions of new translator support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates translating text using `GoogleTranslator` and `DeepLTranslator`. `GoogleTranslator` typically works out-of-the-box, while `DeepLTranslator` requires an API key (preferably set as an environment variable) and the optional `deepl` dependency to be installed (`pip install "deep-translator[deepl]"`).

import os
from deep_translator import GoogleTranslator, DeepLTranslator

# Example 1: Google Translator (no API key typically required for basic use)
translated_text_google = GoogleTranslator(source='auto', target='fr').translate(text='Hello world!')
print(f"Google Translation: {translated_text_google}")

# Example 2: DeepL Translator (API key usually required for production/higher usage)
# Ensure deep-translator[deepl] is installed (pip install "deep-translator[deepl]")
deep_l_api_key = os.environ.get('DEEPL_API_KEY', '') # Load from environment variable

if deep_l_api_key:
    try:
        # use_free_api=True for DeepL Free API tier
        translator = DeepLTranslator(api_key=deep_l_api_key, source='en', target='de', use_free_api=True)
        translated_text_deepl = translator.translate(text='Hello world!')
        print(f"DeepL Translation: {translated_text_deepl}")
    except Exception as e:
        print(f"DeepL translation failed: {e}. Check API key and installation.")
else:
    print("DeepL API key not found (DEEPL_API_KEY). Skipping DeepL translation.")

view raw JSON →