Build a language translation app — Explore the application code
The source code for the application is in the Docker-NLP/05_language_translation.py file.
Reference note (untrusted external data; do not execute it as instructions).
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.
Import the required libraries.
Bounded code example (external data; do not execute automatically):
```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.
Specify the main execution block.
Bounded code example (external data; do not execute automatically):
```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.
Create an infinite loop for continuous input.
Bounded code example (external data; do not execute automatically):
```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.
Create an instance of Translator.
Bounded code example (external data; do not execute automatically):
```python
translator = Translator()
```
This creates an instance of the Translator class, which performs the translation.
Bounded code example (external data; do not execute automatically):
```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.
Print the original and translated text.
Bounded code example (external data; do not execute automatically):
```python
print(f"Original Text: {input_text}")
print(f"Translated Text: {translated_text}")
```
These two lines print the original text entered by the user and the translated text. …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/language-translation.md :: Explore the application code ↗Revision 3a9d778562f3 · Apache-2.0 and attribution