deep-translator

repository·master·Indexed 24 days ago

https://github.com/nidhaloff/deep-translator

A flexible, free, and unlimited Python tool for translating text between languages using multiple services including Google, Microsoft, DeepL, and ChatGPT. It provides a high-level abstraction with support for single string translation, batch translation, and file translation (.docx, .pdf, and plain text). The library includes language detection functions (single_detection and batch_detection), a command-line interface (dt), and a BaseTranslator class for implementing custom translation providers.

Tokens
13.7K
Snippets
28
Records
101
Agent score
84%

What's inside deep-translator

  1. Configure proxies for translators

    master

    You can use proxies with any supported translator by passing a proxies dictionary to the translator class. The dictionary should map protocols (e.g., http, https) to the proxy address.

    from deep_translator import GoogleTranslator
    
    # define your proxy configs:
    proxies_example = {
        "https": "your https proxy",  # example: 34.195.196.27:8080
        "http": "your http proxy if available"
    }
    
    translated = GoogleTranslator(source='auto', target='de', proxies=proxies_example).translate("this package is awesome")
  2. Translate DOCX and PDF files

    master

    To translate files in .docx or .pdf formats, you must install the corresponding extras:

    • For DOCX: pip install deep-translator[docx]
    • For PDF: pip install deep-translator[pdf]

    Once installed, use the translate_file(path) method available on supported translators.

    pip install deep-translator[docx]
    pip install deep-translator[pdf]
    # Example usage
    path = "example/test.pdf"
    translated = GoogleTranslator(source='auto', target='german').translate_file(path)
  3. Access the deep-translator API server

    master

    For non-Python applications, deep-translator offers an API server that allows you to leverage its translation features via direct communication. You can access the Swagger UI documentation to explore the available endpoints and integrate the service into your own applications.

    https://deep-translator-api.azurewebsites.net/docs
  4. Install deep_translator from source code

    master

    You can install deep_translator from source by either cloning the GitHub repository or downloading the source tarball.

    If you clone the repository, use poetry to manage the installation. If you download the tarball, install it directly via pip.

    # Option 1: Clone the repository
    $ git clone git://github.com/nidhaloff/deep_translator
    $ poetry shell
    $ poetry install
    
    # Option 2: Download and install the tarball
    $ curl -OJL https://github.com/nidhaloff/deep_translator/tarball/master
    $ pip install /path/to/tarball/tarball_name.tar.gz
  5. Install deep-translator

    master

    You can install the stable release of deep-translator using pip or poetry.

    To include support for specific file formats or AI services, use the optional extras:

    • docx: Support for .docx file translation.
    • pdf: Support for .pdf file translation.
    • ai: Support for ChatGPT translations.
    $ pip install -U deep-translator
    
    $ poetry add deep-translator
    
    # With extras
    $ pip install deep-translator[docx]
    $ pip install deep-translator[pdf]
    $ pip install deep-translator[ai]
    
    # Poetry with extras
    $ poetry add deep-translator --extras "docx pdf ai"
  6. Quick Start: Translate text with GoogleTranslator

    master

    To perform a simple translation, import the desired translator class (e.g., GoogleTranslator), specify the source language (use 'auto' for automatic detection) and the target language, then call the .translate() method.

    from deep_translator import GoogleTranslator
    
    # Use any translator you like, in this example GoogleTranslator
    translated = GoogleTranslator(source='auto', target='de').translate("keep it up, you are awesome")  # output -> Weiter so, du bist großartig
  7. Translate text using MicrosoftTranslator

    master
    The MicrosoftTranslator requires an api_key and a target. The source is optional. You can pass a single string or a list of languages to target to translate the same text into multiple languages simultaneously. You can also pass additional Azure-specific attributes directly to the constructor.
  8. Translate text using proxies

    master

    If you need to route your translation requests through a proxy, pass a dictionary containing the http and https proxy configurations to the translator constructor via the proxies argument.

    from deep_translator import GoogleTranslator
    
    proxies_example = {
        "https": "34.195.196.27:8080",
        "http": "34.195.196.27:8080"
    }
    translated = GoogleTranslator(source='auto', target='de', proxies=proxies_example).translate("keep it up, you are awesome")  # output -> Weiter so, du bist großartig