How do you translate text in python?
· 6 min read · Updated jul 2022 · Application Programming Interfaces · Natural Language Processing Show Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission. Google translate is a free service that translates words, phrases and entire web pages into more than 100 languages. You probably already know it and you have used it many times in your life. In this tutorial, you will learn how to perform language translation in Python using Googletrans library. Googletrans is a free and unlimited Python library that make unofficial Ajax calls to Google Translate API in order to detect languages and translate text. Here are the main features of this library:
Note that Googletrans makes API calls to the Google translate API, if you want a reliable use, then consider using an official API or making your own machine translation machine learning model. First, let's install it using pip:
Translating TextImporting necessary libraries:
Googletrans provides us with a convenient interface, let's initialize our translator instance:
Note that Translator class has several optional arguments:
Now we simply use
This will print the original text and language along with the translated text and language:
If the above code results in an error like this:
Then you have to uninstall the current googletrans version and install the new one using the following commands:
Going back to the code, it automatically detects the language and translate to english by default, let's translate to another language, arabic for instance:
Now let's set a source language and translate to English:
Output:
You can also check other translations and some other extra data:
See the output:
A lot of data to benefit from, you have all the possible translations, confidence, definitions and even examples. Translating List of PhrasesYou can also pass a list of text to translate each sentence individually:
Output:
Language DetectionGoogle Translate API offers us language detection call as well:
This will print the code of the detected language along with confidence rate (1.0 means 100% confident):
This will return the language code, to get the full language name, you can use the
Output:
Supported LanguagesAs you may know, Google Translate supports more than 100 languages, let's print all of them:
Here is a truncated output:
ConclusionThere you have it, this library is a great deal for everyone that wants a quick way to translate text in an application. However, this library is unofficial as mentioned earlier, the author noted that the maximum character length on a single text is 15K. It also doesn't guarantee that the library would work properly at all times, if you want to use a stable API you should use the official Google Translate API. If you get HTTP Also, I've written a quick Python script that will allow you to translate text into sentences as well as in documents in the command line, check it here. Finally, I encourage you to further explore the library, check out its official documentation. Finally, if you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python. You can also check our resources and courses page to see the Python resources I recommend! Learn also: How to Convert Text to Speech in Python. Happy Coding ♥ View Full Code Read AlsoComment panelHow do you translate words in python?Introduction. from googletrans import Translator.. translator = Translator(). translated_text = translator. translate('안녕하세요. '). print(translated_text. text). translated_text = translator. translate('안녕하세요.', dest='ja'). How do you translate text from one language to another in python?Steps to develop Python Language Translator with GUI:. Installing translate.. Importing translate and tkinter.. Initializing window.. Creating tuple for choosing languages.. Creating a function for translating the text.. Choice for input language and the language in which the text is to be translated.. Input and Output text.. Can we translate language in python?Practical Data Science using Python
The python package which helps us do this is called translate. This package can be installed by the following way. It provides translation for major languages. Below is an example of translating a simple sentence from English to German.
How do I translate German to English in python?You can also translate text documents via Google Translate API. All you have to do is to read the text file in Python using the open method, read the text and pass it to the translate() method.
|