DEV Community

Cover image for How to do language translation in Python
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

How to do language translation in Python

Hi Tech-nerds,

Today I'm going to share with you guys how to automatically perform language translation in Python programming.

Language translation concerns translating textual data from one language to textual data into another language while maintaining the semantic meaning

This is usually done in such a way the same message can be conveyed to different people speaking different languages, We now have over** 6,500** Spoken languages in which more than 3,982 have developed a writing system

Therefore knowing how programmatically perform language translation can be your superpower of getting your articles, ebook, and etc to reach more people no matter what language they speak

Let's get started

We are going to cover only the very basics of doing language translation in python using pre-trained models

Python Libraries

There are several libraries in Python for performing Automatic language translation, below are some of those Libraries but almost all of them are using Google Translate API.

Just check them out in detail and choose which one you like the most to use your personal projects involving language translation, well on this blog post I will be reviewing only 3 of them

Goslate

Goslate provides you free python API to google translation service by querying the google translation website.

Installation

$~ pip install goslate 
Enter fullscreen mode Exit fullscreen mode

goslate will automatically detect your primary language of the text and then translate it to the secondary language you specify, during specifying language you use ISO 639-1 code of the language, you can find the whole list in Wikipedia

Let's try to translate some English text into French

You only specify the *second language *you're translating into and goslate will automatically detect your *primary language *as shown in example below

Sample Usage

>>>import goslate
>>>primary_text = 'Love you a lot '
>>>gs = goslate.Goslate()
>>>gs.translate(primary_text, 'fr')
"Je t'aime beaucoup"
Enter fullscreen mode Exit fullscreen mode

Now Let's move into the next library Google translate

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 detecting and translating.

Installation

$~ pip install googletrans 
Enter fullscreen mode Exit fullscreen mode

The simplicity of usage of googletrans and goslate is almost the same so it all comes to you choosing the one you like, same wise on googletrans we are using ISO 639-1 standard to represent languages.

Let's try converting some English text into Swahili

Sample Usage

>>> text = 'This site is awesome'
>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.translate(text , dest ='sw').text
'Tovuti hii ni ajabu
Enter fullscreen mode Exit fullscreen mode

Lastly let's do some translation using NLP library Textblob

TextBlob

TextBlob is a Python (2 and 3) library for processing textual data.
It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more

Installation

$~ pip install textblob
Enter fullscreen mode Exit fullscreen mode

one of the good things of working with textblob is that its general-purpose natural language processing library which you can use for its variety of NLP tasks apart from translation but simplicity and syntax all most the same as the two above.

Sample Usage

Let's try converting some French text to English

>>> from textblob import TextBlob
>>> blob = TextBlob('comment ca va ?')
>>> blob.translate(to='en')
TextBlob("How is it going ?")
Enter fullscreen mode Exit fullscreen mode

Well that's all, Hope you had fun learning language translation in Python

The Original Article can be found on kalebujordan.dev

If you find it useful don't forget to it a like and to share with your fellow developers

Translated version

  1. Spanish Translation of the article

Top comments (6)

Collapse
 
yuriizinets profile image
Yurii Zinets

Hi!
If someone needs something like this for Golang, check this! :)
github.com/yuriizinets/googletransx

Collapse
 
hectorpascual profile image
Héctor Pascual

Cool, thanks for sharing. Seems like the most up to date library is googletrans.

Collapse
 
kalebu profile image
Jordan Kalebu

Yeah sure

Collapse
 
ioanf2 profile image
ioanf2

see this full python code as to use googletrans:

neculaifantanaru.com/en/python-cod...

Collapse
 
putriika798 profile image
putriika798

Starting from November 30th, I always get errors when using Googletrans. Is googletrans no longer usable?

Collapse
 
alexstelmakh profile image
Alex Stelmakh

From pypi.org/project/googletrans
"You may wonder why this library works properly, whereas other approaches such like goslate won’t work since Google has updated its translation service recently with a ticket mechanism to prevent a lot of crawler programs.

I eventually figure out a way to generate a ticket by reverse engineering on the obfuscated and minified code used by Google to generate such token, and implemented on the top of Python. However, this could be blocked at any time."