Translate text and detect text language using googletrans and python

In this article, we shall see how to translate text from one language to another using python with the help of a python library called googletrans. This library has multiple methods and features but we are going to focus only on the following two things.

  1. Text translation
  2. Language detection

Let us get started by installing this awesome python package.

What is googletrans?

Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.

Compatible with Python 3.6+. The key features of this library are,

  • Fast and reliable – it uses the same servers that translate.google.com uses
  • Auto language detection
  • Bulk translations
  • Customizable service URL
  • HTTP/2 support

Installation

We can install this package from pypi.org by running the following command on the terminal.

pip install googletrans

Once this package is installed we can get started.

Supported languages

This python library supports the following languages. We can also see the supported languages by printing the LANGUAGES constant from the googletrans module.

The LANGUAGES constant is a dictionary with the language code as the key and the language itself as a value to the key.

Run the following code snippet to get the dictionary of languages.

List of languages

This will print all the supported languages in the format of a dictionary.

{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani',
 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan',
 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)',
 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto',
 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician',
 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa',
 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic',
 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada',
 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin',
 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay',
 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)',
 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese',
 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian',
 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali',
 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu',
 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese',
 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'}

These languages are supported by this library.

Translating text

Let us see how to translate the text using this library. The translate() method from the Translator class does the job for us. This method takes the string, source language, and destination language as its arguments.

syntax

translator = Translator()
translator.translate(string, src='English', dest='Tamil')

This method will return an object. The source language is optional. The destination language is English by default. The returned object will have the following data, that can be accessed using the dot operator(It is a class).

  1. source
  2. destination
  3. The translated text
  4. pronunciation
  5. extra arguments

Let us look at a complete code example.

import googletrans
from googletrans import Translator

translator = Translator()
translated_text = translator.translate('Hi, how are you?', dest='spanish')
print(translated_text.text)
translated_text = translator.translate('Hi, how are you?', dest='tamil')
print(translated_text.text)

The output of the above code is,

¿Hola! Cómo estás?
ஹாய், நீங்கள் எப்படி இருக்கிறீர்கள்?

The first output is in Spanish and the next one is in Tamil.

Language detection

The next important feature of this library is the language detection. The detect method takes our input string and returns us a Detected class object which consists of two params.

  1. The language of the string
  2. Confidence value – value from 0.00 to 1.00

where 1.00 is the highest confidence and 0.00 is the lowest confidence.

syntax

translator = Translator()
translator.detect('string')

Let us look at a complete code example for the detect method.

import googletrans
from googletrans import Translator

translator = Translator()

dt1 = translator.detect('பைதான் கோடர்கள்')
print(dt1.lang, dt1.confidence)
dt2 = translator.detect('Python is a popular programming language')
print(dt2.lang, dt2.confidence)

The output of the above code snippet is,

ta 1.0
en 1.0

The language code and confidence are printed. If we want the language name instead of the code, we can use the code as the key to getting the value from the LANGUAGES dictionary.

The modified code is,

import googletrans
from googletrans import Translator

translator = Translator()
languages = googletrans.LANGUAGES
dt1 = translator.detect('பைதான் கோடர்கள்')
print(languages[dt1.lang], dt1.confidence)
dt2 = translator.detect('Python is a popular programming language')
print(languages[dt2.lang], dt2.confidence)

Now the output is,

tamil 1.0
english 1.0

The language name is printed now, instead of the language code. You can also use this method if you want to get the language name instead of the code.

Conclusion

This library provides several other methods also. If you want to know more about this library, take a look at their official documentation here. Hope this article is helpful.

Happy coding!