How to use googletrans in python
Unless you have been hiding under a rock, you have probably used Google Translate on many occasions in your life. Whenever you try to translate a word or a sentence from a certain language to another, it is the Google Translate API which brings you the desired results in the background. Though you can translate anything by simply going to the Google Translate web page, you can also integrate Google Translate API into your web applications or desktop programs. The best thing about the API is that it is extremely easy to set up and use. Show
You can actually do a lot of things with the help of the Google Translate API ranging from detecting languages to simple text translation, setting source and destination languages, and translating entire lists of text phrases. In this article, you will see how to work with the Google Translate API in the Python programming language. Google Translate API InstallationBefore you can work with the Google Translate API in Python, you will have to install it. There are two different methods of installing the API. The first method is straight forward. Simply go to terminal and use the
Press Enter and the Python module for Google Translate API will be installed on your system. If you have installed an Anaconda distribution of Python, you can install the API using the Anaconda Prompt. In this particular method, you will replace
Now that you have installed the API, we will see it in action with the help of some examples. Listing Supported LanguagesThe Google Translate API supports a variety of languages. To list all the supported languages, run the following script:
In the above example, you use the keyword When executed, the above piece of code will list all the supported languages names along with their shorthand notation. Here is how the output will look:
Basic UseThe most basic use of the Google Translate API is, of course, translating words or sentences from one language into another. To do so, we have to import the
Next, you have to create an object of the
Once the Translator class object is created, you will pass the text in source language as a parameter to the
In the script above, we pass Finnish text to the The The object
returned by the
Let's print all the above attributes and see what output we get:
Output:
The output shows that the source language is Finnish (fi) and the destination language is English (en). The translated sentence can be printed via the In the above example, we did not specify the source language. Therefore, Google Translate API tries to detect source language itself. Similarly, we did not specify any destination language as well and thus, the API translated the source language into the default language that is English. But, what if you want to specify both the source and destination languages? Specifying Source and Destination LanguagesIt is in fact, very easy to specify both the destination and source languages in the Google Translate API. Here is the code you'll use to pass only the source language:
For adding destination language only, you have to add
You can also pass the source and destination languages at the same time: Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Let's now translate a Finnish sentence into French and then print the source and destination languages, as well as the translated text. This time we will specify the source and destination languages.
The above piece of code will produce the following result.
Translating List of PhrasesIt is also possible to translate a list of textual phrases with the help of the Google Translate API. The basic process is the same as discussed above. You just have to pass the list containing the phrases as a parameter to the Let's create a list of strings containing some phrases from the French language.
Now it is time to call the
In the above script, the source language is French and the destination language is Swahili. The In the script below, we iterate over the list of objects returned by the
The following will be the result displayed on the screen.
Translating Text DocumentsYou can also translate text documents via Google Translate API. All you have to do is to read the text file in Python using the The first step is to open the file in the "read" mode:
You can also check whether or not the file is in "read" mode using the
Next, you can use the We will also print the
Here is the output of the file contents:
Make sure you have the above content in your text file if you want to follow along with our example. We have ascertained that Python is accessing and reading the text file.
Now, we will translate the result by importing the same old
The next step is to pass the
The output should look like the following:
To write the translated text to the same file, or a different text file, you will simply open the file in
the write mode ("w"). Next, you need to call the
In the above example, we have used the context manager ConclusionGoogle Translate is a tool with an API that helps you perform a lot of different translation-related functions. We have only scratched the surface with the above examples. You are encouraged to practice the API, as well as learn and understand how to use it in real-life applications. How do I install Googletrans in python?Installation. To install, either use things like pip with the package “googletrans” or download the package and put the “googletrans” directory into your python path.
How do I use Google translate in python?Using the Translation API with Python. Overview.. Setup and requirements.. Enable the API.. Authenticate API requests.. Install the client library.. Start Interactive Python.. List available languages.. Translate text.. Can we use Google translate 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.
How do you translate text in python?How do you translate text using Python?. from googletrans import Translator.. translator = Translator(). translated_text = translator. translate('안녕하세요. '). print(translated_text. text). translated_text = translator. translate('안녕하세요.', dest='ja'). |