We can use a language translator to translate text from one language to another.
There are various APIs and modules for this, we’ll use the Google Translate API.

We will use the Goslate module to translate. Apart from translation, it supports
language detection, batch translation, dictionary lookup and more.

Related course: Complete Machine Learning Course with Python

Installation

The goslate module connects with the Google Translate API.
The first step is to install the goslate module.
Install goslate using pyenv, pipenv or virtualenv.

Example

With just a few lines of code we can translate English to French.

 
import goslate

text = "Hello World"

gs = goslate.Goslate()
translatedText = gs.translate(text,'fr')

print(translatedText)

Change the second parameter in gs.translate to alter the language.

Other Translate APIs

There are a few other translation APIS including:

Do you know of any other modules? leave a comment.

Alternatives

If there is no module available for the translation API,
you could make your own module using beautifulsoup.

Offline Translation

This is a simple dictionary translation (offline). If you want a simple word translation, you can use this.Use your package manager to install dictd,

 
sudo pacman -S dictd
sudo apt-get install dictd


Then to list databases
 
dict -D


To translate a single word:
 
dict -d fd-eng-spa "earth"


We create this code to list all translations:
 
import os

word = 'earth'
translation = os.popen('dict -d fd-eng-spa ' + word).read()
print(translation)

Download Machine Learning examples