Home Explore Blog CI



docker

1st chunk of `content/guides/language-translation.md`
9ec3bf629fd5b3848984f78912654ccb0b06e02dbe02aa3a0000000100000fb0
---
title: Build a language translation app
linkTitle: Language translation
keywords: nlp, natural language processing, text summarization, python, language translation, googletrans
description: Learn how to build and run a language translation application using Python, Googletrans, and Docker.
summary: |
  This guide demonstrates how to use Docker to deploy language translation
  models for NLP tasks.
tags: [ai]
languages: [python]
aliases:
  - /guides/use-case/nlp/language-translation/
params:
  time: 20 minutes
---

## Overview

This guide walks you through building and running a language translation
application. You'll build the application using Python with Googletrans, and
then set up the environment and run the application using Docker.

The application demonstrates a simple but practical use of the Googletrans
library for language translation, showcasing basic Python and Docker concepts.
Googletrans is a free and unlimited Python library that implements the Google
Translate API. It uses the Google Translate Ajax API to make calls to such
methods as detect and translate.

## Prerequisites

- You have installed the latest version of [Docker Desktop](/get-started/get-docker.md). Docker adds new features regularly and some parts of this guide may work only with the latest version of Docker Desktop.
- You have a [Git client](https://git-scm.com/downloads). The examples in this section use a command-line based Git client, but you can use any client.

## Get the sample application

1. Open a terminal, and clone the sample application's repository using the
   following command.

   ```console
   $ git clone https://github.com/harsh4870/Docker-NLP.git
   ```

2. Verify that you cloned the repository.

   You should see the following files in your `Docker-NLP` directory.

   ```text
   01_sentiment_analysis.py
   02_name_entity_recognition.py
   03_text_classification.py
   04_text_summarization.py
   05_language_translation.py
   entrypoint.sh
   requirements.txt
   Dockerfile
   README.md
   ```

## Explore the application code

The source code for the application is in the
`Docker-NLP/05_language_translation.py` file. Open `05_language_translation.py`
in a text or code editor to explore its contents in the following steps.

1. Import the required libraries.

   ```python
   from googletrans import Translator
   ```

   This line imports the `Translator` class from `googletrans`.
   Googletrans is a Python library that provides an interface to Google
   Translate's AJAX API.

2. Specify the main execution block.

   ```python
   if __name__ == "__main__":
   ```

   This Python idiom ensures that the following code block runs only if this
   script is the main program. It provides flexibility, allowing the script to
   function both as a standalone program and as an imported module.

3. Create an infinite loop for continuous input.

   ```python
      while True:
         input_text = input("Enter the text for translation (type 'exit' to end): ")

         if input_text.lower() == 'exit':
            print("Exiting...")
            break
   ```

   An infinite loop is established here to continuously prompt you for text
   input, ensuring interactivity. The loop breaks when you type `exit`, allowing
   you to control the application flow effectively.

4. Create an instance of Translator.

   ```python
         translator = Translator()
   ```

   This creates an instance of the Translator class, which
   performs the translation.

5. Translate text.

   ```python
         translated_text = translator.translate(input_text, dest='fr').text
   ```

   Here, the `translator.translate` method is called with the user input. The
   `dest='fr'` argument specifies that the destination language for translation
   is French. The `.text` attribute gets the translated string. For more details
   about the available language codes, see the
   [Googletrans docs](https://py-googletrans.readthedocs.io/en/latest/).

6. Print the original and translated text.

Title: Building a Language Translation Application with Python and Docker: Overview and Setup
Summary
This section guides you through creating a language translation application using Python's Googletrans library and deploying it with Docker. It covers the necessary prerequisites like Docker Desktop and Git, cloning the sample application's repository, and exploring the Python code responsible for language translation using Googletrans, including setting up an infinite loop for continuous text input and using the Translator class.