Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting now!
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 makes unofficial Ajax calls to Google Translate API to detect languages and translate text.
This library is not just for translation; we have a tutorial on detecting languages using this exact library, among others.
Here are the main features of this library:
Note that Googletrans makes API calls to the Google Translate API. If you want reliable use, then consider using an official API or making your own machine translation model.
First, let's install it using pip:
pip3 install googletrans
Learn also: How to Perform Text Summarization using Transformers in Python.
Importing necessary libraries:
from googletrans import Translator, constants
from pprint import pprint
Googletrans provides us with a convenient interface. Let's initialize our translator instance:
# init the Google API translator
translator = Translator()
Note that the Translator
class has several optional arguments:
service_urls
: This should be a list of strings that are the URLs of Google Translate API; an example is ["translate.google.com", "translate.google.co.uk"]
.user_agent
: A string that will be included in User-Agent header in the request.proxies
(dictionary): A Python dictionary that maps protocol or protocol and host to the URL of the proxy; an example is {'http': 'example.com:3128', 'http://domain.example': 'example.com:3555'}
, more on proxies in this tutorial.timeout
: The timeout of each request you make, expressed in seconds.Now, we simply use translate()
method to get the translated text:
# translate a spanish text to english text (by default)
translation = translator.translate("Hola Mundo")
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")
This will print the original text and language along with the translated text and language:
Hola Mundo (es) --> Hello World (en)
If the above code results in an error like this:
AttributeError: 'NoneType' object has no attribute 'group'
Then you have to uninstall the current googletrans version and install the new one using the following commands:
$ pip3 uninstall googletrans
$ pip3 install googletrans==3.1.0a0
Going back to the code, it automatically detects the language and translates to English by default, let's translate to another language, Arabic, for instance:
# translate a spanish text to arabic for instance
translation = translator.translate("Hola Mundo", dest="ar")
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")
"ar"
is the language code for Arabic. Here is the output:
Hola Mundo (es) --> مرحبا بالعالم (ar)
Now, let's set a source language and translate it to English:
# specify source language
translation = translator.translate("Wie gehts ?", src="de")
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")
Output:
Wie gehts ? (de) --> How are you ? (en)
You can also check other translations and some other extra data:
# print all translations and other data
pprint(translation.extra_data)
See the output:
{'all-translations': [['interjection',
['How are you doing?', "What's up?"],
[['How are you doing?', ["Wie geht's?"]],
["What's up?", ["Wie geht's?"]]],
"Wie geht's?",
9]],
'confidence': 1.0,
'definitions': None,
'examples': None,
'language': [['de'], None, [1.0], ['de']],
'original-language': 'de',
'possible-mistakes': None,
'possible-translations': [['Wie gehts ?',
None,
[['How are you ?', 1000, True, False],
["How's it going ?", 1000, True, False],
['How are you?', 0, True, False]],
[[0, 11]],
'Wie gehts ?',
0,
0]],
'see-also': None,
'synonyms': None,
'translation': [['How are you ?', 'Wie gehts ?', None, None, 1]]}
A lot of data to benefit from; you have all the possible translations, confidence, definitions, and even examples.
Related: How to Paraphrase Text using Transformers in Python.
You can also pass a list of text to translate each sentence individually:
# translate more than a phrase
sentences = [
"Hello everyone",
"How are you ?",
"Do you speak english ?",
"Good bye!"
]
translations = translator.translate(sentences, dest="tr")
for translation in translations:
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")
Output:
Hello everyone (en) --> herkese merhaba (tr)
How are you ? (en) --> Nasılsın ? (tr)
Do you speak english ? (en) --> İngilizce biliyor musunuz ? (tr)
Good bye! (en) --> Güle güle! (tr)
Google Translate API offers us language detection calls as well:
# detect a language
detection = translator.detect("नमस्ते दुनिया")
print("Language code:", detection.lang)
print("Confidence:", detection.confidence)
This will print the code of the detected language along with the confidence rate (1.0 means 100% confidence):
Language code: hi
Confidence: 1.0
This will return the language code. To get the full language name, you can use the LANGUAGES
dictionary provided by Googletrans:
print("Language:", constants.LANGUAGES[detection.lang])
Output:
Language: hindi
Read also: Conversational AI Chatbot with Transformers in Python.
As you may know, Google Translate supports more than 100 languages. Let's print all of them:
# print all available languages
print("Total supported languages:", len(constants.LANGUAGES))
print("Languages:")
pprint(constants.LANGUAGES)
Here is a truncated output:
Total supported languages: 107
{'af': 'afrikaans',
'sq': 'albanian',
'am': 'amharic',
'ar': 'arabic',
'hy': 'armenian',
...
<SNIPPED>
...
'vi': 'vietnamese',
'cy': 'welsh',
'xh': 'xhosa',
'yi': 'yiddish',
'yo': 'yoruba',
'zu': 'zulu'}
There you have it. This library is great for anyone who wants to quickly translate text into 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 will 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 5xx
errors with this library, then Google has banned your IP address; it's because using this library a lot, Google Translate may block your IP address; you'll need to consider using proxies by passing a proxy dictionary to proxies
parameter in Translator()
class, or use the official API as discussed.
Also, I've written a quick Python script that will allow you to translate text into sentences and documents on the command line. Check it out here.
Finally, I encourage you to explore the library further; check out its official documentation.
Learn also: How to Convert Text to Speech in Python.
Happy Coding ♥
Finished reading? Keep the learning going with our AI-powered Code Explainer. Try it now!
View Full Code Improve My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!