How to translate languages in Python with Google Translate and DeepL (plus more)

In this tutorial, you will learn how to perform Python translation of nearly any type of text. I’ll show you how to work with the Google Translate and DeepL engines using Python, how to detect the language of your texts, and how to automate language translation using a dedicated TMS. We are going to discuss three Python translate libraries: Translators, googletrans, and deep-translator, and see them in action.

Check out how our translation management system can help you translate your Python apps faster.

    What is machine translation and which engine is the best one?

    Machine translation (MT) is a process involving some kind of algorithm to perform translations automatically rather than hiring a human specialist. The MT market is growing rapidly: computer-generated translations have come a long way, from absolutely terrible to pretty usable, thanks to the emergence of machine learning and neural networks. Some even say that we don’t really need human translators anymore, but I’d say that’s a little far-fetched. MT has pros, but of course it has cons, too.

    If you are interested in learning more about MT in general and the available engines, I would recommend checking out this very detailed blog post on this topic.

    Python translation libraries

    There are a few available Python packages that introduce the ability to translate texts by taking advantage of neural networks. Let’s discuss some of them.

    Translators

    The translators package is probably one of the best choices out there.

    Pros:

    • It is actively supported.
    • It does not have many open issues and appears stable.
    • Its package supports around a dozen different translation APIs.
    • It can be used to translate HTML.
    • It supports batch translations.

    Cons:

    • It does not provide many customization options.
    • Translators does not allow performance of language detection only.
    • The response contains the translated text without any additional information.

    Deep-translator

    Deep-translator also seems like a very solid option.

    Pros:

    • It has support for multiple translation APIs, including Google, Microsoft, Yandex, Libre, and others.
    • Allows performance of translations directly from a text file (unfortunately, it has to be in .txt format).
    • Can be used directly from a terminal.
    • Supports batch translations.

    Cons:

    • Language detection is only possible with a private API key.
    • The package’s community seems to have become less active.

    Googletrans

    Googletrans is the library that, as the name suggests, enables you to easily translate any text using Google Translate. While it’s still a feasible solution, it does have certain issues.

    Pros:

    • No configuration or authentication needed; you can start using the library right away.
    • It has a text language detection feature.
    • The returned data contains extra information like translation confidence score or potential mistakes.

    Cons:

    • The biggest problem is that this package no longer appears to be actively maintained.
    • The latest stable release has unresolved issues and you’ll have to install release candidate instead.
    • Certain additional data no longer seems to be available.

    Translate

    Translate is a simple command-line tool and a Python module that enables you to perform translations using Google MT and other engines. It’s quite similar to googletrans.

    Pros:

    • It is available both as a Python module and as a command-line tool.
    • Supports multiple engines, including Google, Microsoft, and DeepL.

    Cons:

    • This package is a bit too simplistic.
    • Does not allow text language detection.
    • Has unresolved issues and does not seem to be actively maintained.

    Setting up a new Python project

    Now I would like to see these Python translation libraries in action, but first things first: we need to prepare a sample project. I’ll be using Pipenv to manage dependencies and create a virtual environment, but of course you can take advantage of another solution.

    Let’s install Pipenv:

    pip install --user pipenv

    Next, create a new folder for your project and add a Pipenv file in it:

    [[source]]
    url = "https://pypi.org/simple"
    verify_ssl = true
    name = "pypi"
    
    [packages]
    requests = "~=2.27"
    python-lokalise-api = "~=1.6"
    python-dotenv = "~=0.20"
    googletrans = "==4.0.0rc1"
    translators = "~= 5.4"
    deep-translator = "~=1.9"
    [requires]
    python_version = "3.10"

    We will use these packages throughout the tutorial, therefore install it by running:

    pipenv install

    Also, create an empty .env file and an src directory. Inside the src directory, let’s add an i18n folder with an en.json file. The full path to this file is YOUR_PROJECT/src/i18n/en.json. Here are some sample contents for this JSON file:

    {
      "welcome": "Welcome to the tutorial, {username}!",
      "description": "This tutorial explains how to translate texts with Python."
    }

    Okay, so we’ve laid the ground work and can proceed to the main part of this tutorial.

    Using DeepL and the Translators library

    The first library that I wanted to show you has a very concise name: Translators. It supports numerous engines, including Google, DeepL, Baidu, and others.

    Translate texts with DeepL

    To get started, let’s create a new src/translate-translators.py file:

    import translators as ts

    Then you will have to decide which translation engine to use. To use an engine, you simply need to call a method with the corresponding name, for example:

    ts.google()
    ts.deepl()
    ts.baidu()

    These methods accept at least one argument: the text you would like to translate. Next, you can specify additional options, for example the source and the target language. By default the engine will try to “guess” the source language, and use English as the target.

    Let’s translate a simple sentence into French:

    import translators as ts
    
    res = ts.deepl("Welcome to our tutorial!", to_language='fr')
    print(res)

    Here’s the result:

    Bienvenue à notre tutoriel !

    What if we wanted to translate multiple items from a JSON file? That’s not a problem either:

    import json
    import os
    import translators as ts
    
    filepath = os.path.join(os.path.dirname(__file__), "i18n/en.json")
    
    with open(filepath) as i18n_file:
        parsed_json = json.loads(i18n_file.read())
    
    result = [ts.deepl(phrase, to_language='fr', sleep_seconds=5) for phrase in parsed_json.values()]
    print(result)

    In this case I’ve specified the sleep_seconds option to avoid sending too many requests.

    Now you can run the script

    pipenv run python src\translate-translators.py

    …and observe the results.

    That was fast, eh?

    Using Google Translate and the deep-translator package

    Next, I’ll show you how to get started with the deep-translator library and easily perform text language detection.

    Translate your texts

    So, to see this library in action, let’s create a new src/deep-translate.py file:

    import os
    import json
    from deep_translator import GoogleTranslator

    In this example we will be using GoogleTranslator, but the package has support for other translation APIs as well.

    Next, let’s pick the source and the target languages. For the source, I want Google MT to “guess” it for me, and the target should be French:

    translator = GoogleTranslator(source='auto', target='fr')

    The last step is to perform the actual translation:

    print(translator.translate("Welcome to our tutorial!"))

    Simple, right?

    Let’s read our JSON file and take advantage of bulk translation:

    import os
    import json
    from deep_translator import GoogleTranslator
    
    translator = GoogleTranslator(source='auto', target='fr')
    
    filepath = os.path.join(os.path.dirname(__file__), "i18n/en.json")
    
    with open(filepath) as i18n_file:
        parsed_json = json.loads(i18n_file.read())

    Now we need to take all the values from the parsed_json and pass it to the translate_batch method:

    results = translator.translate_batch(parsed_json.values())
    print(results)

    The results variable will contain an array with the translated texts:

    ['Bienvenue dans le didacticiel, {username}!', 'Ce tutoriel explique comment traduire des textes avec Python.']

    Great!

    Detect text language with Python

    Deep-translator allows you to perform language detection, but unfortunately this does not work out of the box. To start using this feature, you have to register on the https://detectlanguage.com website and generate an API key. The good news is that registration takes less than a minute and you won’t have to pay anything: there’s a free plan that allows you to send up to 1000 requests per day. So, after you’ve registered, proceed to your personal profile, and copy the API key.

    Next, paste it in the .env file:

    DETECTION_KEY=123abc

    Create a new src/deep-detection.py file:

    import os
    import json
    from deep_translator import single_detection
    
    from dotenv import load_dotenv
    load_dotenv()
    

    And now simply use the single_detection method:

    lang = single_detection("Ce tutoriel explique comment traduire des textes avec Python.", api_key=os.getenv('DETECTION_KEY'))
    print(lang)

    After running the script, you should see fr printed to the screen. Nice!

    Using Google Translate and the googletrans library

    In this section, I’m going to show you how to utilize the googletrans library and use Google Machine Translation (MT) with ease.

    Performing text translations

    To see the googletrans library in action, let’s create a new src/google-translate.py file and instantiate the Translator class:

    from googletrans import Translator
    
    translator = Translator()

    To perform text translation, you should use a method called — guess what? — translate. Easy, huh? This method accepts at least one argument: that’s your source text. By default the target language is English, but you can override it simply by passing a second argument:

    result = translator.translate("Welcome to our tutorial!", dest="fr")

    The result variable will contain a special object responding to the following methods:

    • text — target translation value (in our case, it should return French text).
    • dest — returns a target (destination) language code. In our case, it’ll be fr.
    • origin — the source text.
    • src — the source text language, which Google MT will detect automatically.

    So, why don’t we try to display this info? Modify your script by adding the following lines:

    print(f"Translating the following text:\n{result.origin}\nDetected language code is {result.src}")
    print(f"Here's the result:\n{result.text}\nTarget language code is {result.dest}")

    Now run the script:

    pipenv run python src\translate.py

    And here’s your result:

    Translating the following text:
    Welcome to our tutorial!
    Detected language code is en
    Here's the result:
    Bienvenue dans notre tutoriel!
    Target language code is fr

    Great!

    Of course, you can read the contents of our sample JSON file and translate those with ease:

    import json
    import os
    from googletrans import Translator
    
    filepath = os.path.join(os.path.dirname(__file__), "i18n/en.json")
    
    with open(filepath) as i18n_file:
        parsed_json = json.loads(i18n_file.read())
    
    translator = Translator()
    
    for source in parsed_json.values():
        result = translator.translate(source, dest="fr")
        print(f"Translating the following text:\n{result.origin}\nDetected language code is {result.src}")
        print(f"Here's the result:\n{result.text}\nTarget language code is {result.dest}")

    Now rerun the script and make sure that the translations display properly. You might note, however, that the {username} placeholder is translated as well. Unfortunately, there’s no simple way to prevent that: sometimes MT engines fail to recognize such elements. One solution is to split your text and translate it in parts, but in this case the result could be very far from ideal. Your translated parts might have no meaning when connected together. Nevertheless, later I’ll show you another solution that can detect such placeholders for you.

    Python language detection

    Okay, so what if you need to perform language detection only? This can come in handy when you are not sure what the source language of your texts is. To achieve this, let’s create another src/google-detection.py file:

    from googletrans import Translator
    
    translator = Translator()

    To perform language detection, we should use another method which is unsurprisingly called detect:

    result = translator.detect("Welcome to our tutorial!")

    The result object has a lang attribute which returns the detected code:

    print(f"Language code: {result.lang}")

    Of course, you can provide more phrases in different languages:

    result = translator.detect("Welcome to our tutorial!")
    print(f"Language code: {result.lang}")
    
    result = translator.detect("Добро пожаловать в нашу статью!")
    print(f"Language code: {result.lang}")

    Nice!

    How to automate translations in Python

    So, as you can see, using a machine translation engine is not a complex task and you can start working with it in a matter of minutes. However, while the approach explained above is fine for translating smaller sentences, it does not scale well and thus is not suitable for large projects.

    Suppose you have a large website with hundreds or even thousands of texts stored in multiple translation files. How hard would it be to translate this website into three languages? I would say quite hard, as you might face various unexpected issues. Also, as explained above, using an MT engine alone to translate complex texts is probably not the best idea, and you’ll most likely need someone to perform post-editing. Moreover, you’ll probably need a tool to better organize your translations, and provide screenshots explaining where and how these texts display (context matters!). Or maybe you’ll need to automatically export and import translations to and from your GitHub repository. There are numerous potential requirements, and the simplest solution would be to take advantage of a dedicated translation management system.

    In this section, you’ll meet Lokalise — a TMS for agile teams that will cover all your needs. I’ll show you how to get started with Lokalise, how to automatically apply machine translation to uploaded text, and how to use the API to easily upload and download your translation files in any format.

    Prepare a new Lokalise project

    To begin, grab your free trial at app.lokalise.com/signup (no credit card required!). After signing up, you can join an existing team, if your company is already using Lokalise, or create a new one. For the purposes of this demo, let’s stick to the latter option and create a brand-new team. A team is basically a collection of users, groups, settings, and translation projects. Follow the wizard’s instructions; for example, choose:

    • I’m a software developer.
    • I want to start localization from scratch.
    • My content is in translation files.

    Then press Create a project:

    Next, give your project a name and choose base and target languages. The base language is the original language of your content, whereas the target is the language that you want to translate into (please note that it’s possible to choose multiple targets):

     

    That’s pretty much it: you can now close the wizard without uploading any translation files. Instead, we’ll set up an automation and then upload our data via the API.

    Setting up an automation rule

    The next step is creating a special automation rule that will apply machine translation to the uploaded text.

    Choose More > Automations from the top menu:

     

    Then click Create automation rules and adjust the following options:

    • Monitored language: English (or your own base language)
    • Minimal change required: 0%
    • Automated languages: add any target languages of your choice
    • Actions:
      • Use machine translation — by Google
      • Clear statuses
      • Mark as unverified (actually, enabling the last two options is not mandatory but most likely you’ll want to double-check machine translations as they might not be perfect)

    To learn more about automations, please refer to our documentation.

    Basically, that’s it! Once you are ready, hit Save changes and return to the project’s main page (called the “project editor”). The next thing to do is generate a Lokalise API key.

    Generating an API key

    We are going to use the Lokalise API in order to upload and download our translations; therefore, we must generate an API token. To achieve this, find your avatar in the bottom left corner, click on it, and choose Profile settings:

    Then, choose API tokens in the left menu, and click Generate new token. The following dialog will appear:

    Choose Read and write access, and then click Generate. Copy the newly created token and paste it to your .env file:

    API_KEY=123abc

    Great job! If would like to learn more about the Lokalise API and see usage examples, please check out my “APIv2 in practice” tutorial and a tutorial explaining how to build a Flask app with the Lokalise API and implement an OAuth 2 flow.

    Uploading a translation file and applying an automation

    At this point we are ready to upload our translation file and use the automation rule to apply machine translation.

    First of all, let’s create a new src/upload.py file, load all the necessary dependencies, and read environment variables from the .env file:

    import lokalise
    import os
    import base64
    import time
    
    from dotenv import load_dotenv
    load_dotenv()

    Next, we are going to initialize an API client and define some variables:

    client = lokalise.Client(os.getenv('API_KEY'))
    
    project_id = "YOUR_LOKALISE_PROJECT_ID"
    filename = "en.json"
    filepath = os.path.join(os.path.dirname(__file__), f"i18n/{filename}")

    We will be using an official Python SDK for Lokalise API.

    Please note that you’ll need to specify a Lokalise project ID. You can find this ID by returning to your Lokalise project and clicking More > Settings in the top menu. You’ll see your project ID under the corresponding section:

     

    Now here’s the main part of our uploading script:

    with open(filepath) as f:
        content = f.read()
        file_data = base64.b64encode(content.encode()).decode() # 1
    
        bg_process = client.upload_file(project_id, {
            "data": file_data, # 2
            "filename": filename,
            "lang_iso": 'en', # 3
            "use_automations": True # 4
        })
    
        result = is_uploaded(client, project_id, bg_process) # 5

    Here:

    1. The imported content must be encoded with Base64.
    2. We are providing our content under the data field.
    3. Be sure to specify a language ISO code for your translation data. My base language is English, therefore I’m providing en. Please note that this language code has to be present in the chosen Lokalise project otherwise you’ll get an error.
    4. Be sure to set enable_automations to True so that our rule kicks in when the translations are uploaded.
    5. While the uploading process happens in the background on Lokalise, we’ll have to wait a few moments and make sure our data is properly imported. The scheduled background process is represented by the bg_process object.

    Now let’s code the is_uploaded method:

    def is_uploaded(api_client, project, process):
        for _i in range(5):
            process = api_client.queued_process(project, process.process_id)
            if process.status == 'finished':
                return True
            time.sleep(1)
        return False

    We are requesting the status of the background process five times with a 1 second delay. If the status has changed to finished, it means that everything is good and our data was properly processed. If all five checks failed, it probably means that something has gone wrong. Please note that you might need to adjust these numbers depending on how large your translation file is.

    And so this is it: your uploading script is ready!

    Checking the file uploading feature

    So, let’s execute our script by running:

    pipenv run python src\upload.py

    Once the operation is completed, return to your Lokalise project and open the editor:

    Wow, take a look at that! The French and German translations were provided for us automatically; we didn’t have to lift a finger. Moreover, the {username} part was properly recognized as a placeholder and now has a special formatting as placeholders should not be translated or modified.

    These orange circles next to the translations mean that these values are unverified. You can filter only the unverified texts (by using the Filter dropdown) and double-check the result delivered by the MT.

    Awesome!

    Downloading translated content from Lokalise to your project

    Now that our translations are done, we can download them back to the project. To do this, let’s create a new download.py file:

    import lokalise
    import requests
    import io
    import zipfile
    import os
    
    from dotenv import load_dotenv
    load_dotenv()

    Next, instantiate a client and add a list of the language ISO codes that you’d like to download:

    client = lokalise.Client(os.getenv('API_KEY'))
    project_id = "23921143635688189927f8.87373707"
    target_isos = ["fr", "de"]

    You can find a language code by opening your Lokalise project, clicking on the languages dropdown, and then pressing More > Settings next to the language name:

    You’ll see a dialog with the following information:

    That’s your language code. If needed, you can actually toggle the switch to On and enter any other code.

    Great, now let’s start the download process:

    response = client.download_files(project_id, {
        "format": "json",
        "filter_langs": target_isos,
        "original_filenames": True,
        "directory_prefix": "",
        "indentation": "2sp",
        "placeholder_format": "icu"
    })
    • format — the desired format for your translation files. Lokalise supports more than a dozen file formats.
    • filter_langs — only the chosen languages should be included in the download bundle.
    • original_filenames — we would like to preserve the original names for our files.
    • directory_prefix — translation files should not be further grouped into nested folders.
    • indentation — two spaces should be used for indentation.
    • placeholder_format — our format is ICU, but you can choose other options depending on the file format (Symfony, iOS, .NET, raw, and so on).

    Finally, we should extract the archive to the src/i18n folder:

    data = io.BytesIO(requests.get(response['bundle_url']).content)
    
    with zipfile.ZipFile(data) as archive:
        for iso in target_isos:
            archive.extract(f"{iso}.json", path="src/i18n/")

    Please note that any duplicate files in that directory will be overwritten so be careful!

    And, that’s pretty much it. Now you can run the download.py script and make sure that two new files, namely de.json and fr.json, were created in the i18n directory. Great job!

    What about human-generated translations?

    As you already know, while MT is a great tool it does have its limitations. Is there an easy way to hire a professional translators without needing to find a candidate all by yourself? But of course!

    Lokalise allows you to take advantage of professional translation services with just a few clicks. First, choose Order from the left menu and then click New order:

    Then you’ll just need to select a translation provider (Lokalise or Gengo), choose the project, and adjust the other options:

    Once you are ready, simply enter your credit card details and wait until the order is completed. If you choose the Gengo provider, you’ll also be able to attach a plain text file instead of choosing a translation project.

    To learn more about professional translation services, please refer to our documentation.

    Conclusion

    And this concludes our tutorial. Actually, we’ve only scratched the surface, as you can build much more complex flows with Lokalise. Here are some suggestions for further research:

    I thank you for staying with me, and until next time.

    Talk to one of our localization specialists

    Book a call with one of our localization specialists and get a tailored consultation that can guide you on your localization path.

    Get a demo

    Related posts

    Learn something new every two weeks

    Get the latest in localization delivered straight to your inbox.

    Related articles
    Localization made easy. Why wait?