Learn how to create a text translator in Python that can translate between multiple languages using just a few lines of code.
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
To receive the file(s) used in the class by email, fill in :
Your best email
To send
Python Icon
Python
Impressive
You will learn the fastest growing programming language in the student data world to create incredible automations, develop websites, perform data analysis, work with Data Science, Artificial Intelligence, even if you have never had any contact with Programming in your life.
Start now
Right arrow
Python icon used as backgroundThree images of Python course classes
Python Text Translator – With Few Lines of Code
In today's class, I'll show you how to develop a text translator in Python capable of translating between different languages, with just a few lines of code.
We will use the Deep-translator library, which allows us to integrate Google Translate directly into Python to perform our translations. This way, we will be able to easily translate languages, just as we can do on the website itself.
This code will make the task of translating any text with Python simple, quick and easy. So, download the material available in the class and let's learn how to build this text translator in Python.
Ways to Translate Texts with Python
There are several ways to translate text from one language to another using Python. You can train an AI model to perform this process, however, there are already pre-built models for this task.
Therefore, we will move on to another approach, which involves consulting a translation service. There are free options for these services that are really effective, such as Google Translate .
Installing and Importing the Library
To build our text translator with Python and use Google Translate within it, we will need the library called Deep-translator .
This library incorporates several translation tools. Some require API keys to access, but in the case of Google Translate, this is not necessary.
So, the first step will be to install this library. In your code editor terminal, run the command:
pip install deep-translator
Once this is done, we can import it into our code. Since we will only be using Google Translate, we will import it specifically from that library.
from deep_translator import GoogleTranslator
Creating Text Translator in Python
With the library installed and imported into the code, we can now start developing our text translator in Python. This process will be straight to the point, with very few lines.
First, let's create a variable to start the translator, assigning it the GoogleTranslator() class . This class requires two essential parameters: source , which is the original language, and target , which is the desired language.
You can enter the language in full, such as “portuguese” and “english” , or in abbreviated form. However, please note that full names must be written in English .
Once this is done, we can create a variable called text to pass the text we want to translate.
texto = "Fala pessoal, inscrevam-se no canal do YouTube para aprenderem mais sobre programação em Python e outras linguagens de programação. "
Then, we can translate this text by calling the translate method of our translator and passing the text we want to translate.
Python Text Translator – With Few Lines of Code
-
- Posts: 52
- Joined: Sun Dec 15, 2024 5:27 am