Learn to Code via Tutorials on Repl.it!

← Back to all posts
Building a language translator with Python
userSM (253)

When I initially heard about building a language translator, I had visions of creating a massive dictionary with every key and value pair being perfect, with hours of hard work geting the words and their meanings. But luckily I discovered that you need to do none of this due to the Python googletrans module. Then I made a language translator and a txt file translator. So after having made 2 of these translation type repls, I decided to make a tutorial on how to make a translator make a translator with Python.
Enjoy :)

SETTING UP

To create a language translator, first we need to import the googletrans module, and from there get the the Translator method. So the following code would be used:
from googletrans import Translator
There, first step completed.

INITIALIZATION

Now we need to actually start making this translator. To do this, we need to create a variable, which to make this non confusing, I'm going to tell you to call trans (for the 'trans' bit in googletrans). Next we have to assign our new variable trans to the Translator function that comes with the googletrans module. This would look like this:
trans=Translator()

CONVERTING TEXT

Now for this we need to create a new variable called t (you can call it whatever you want). We need t to be assigned to the result of when trans (refer to the previous section) is subject to the .translate function. To make this easier to understand, I'm just going to write the code below:
t=trans.translate(source,dest=destination)

This line of code is taking t and assigning it to the result when trans (the module assigned to the Translator function which stores all the language data, to put it simply) has the .translate function be performed upon it. As parameters to the .translate function, you enter the source which is the source text (this can be in valid language) and the dest which equals any valid destination language, like say for example dest=french. This will translate the source text to french no matter what language the source text is in, sort of like auto detect in the actual google translate.

FINISHING UP

Take this for example:
t=trans.translate('Hello',dest=french)

This will translate "Hello" into french. But we actually need to see the translated text. To do this, we need to type:
print(t.text)
This will print the result text (for me it is t.text as I named my variable t but for you it should be whatever_u_named_it.text). To print the original text as well, type print(t.origin), and this will print the original text.

THE END

Hope you all liked it :)
Comments, upvotes and feedback are appreciated :)

Comments
hotnewtop
RishiMohanty (0)

I keep on getting an error that french is not defined...
Is there a line I missed?
Do you have download a package?

userSM (253)

@RishiMohanty no you don't have to download anything, the module is just outdated, so it won't work

RishiMohanty (0)

@userSM

oooOOH...
Is there another way to make the translator?

RishiMohanty (0)

Nvm.
Found a way:

import goslate
primary_text = 'my name is bob '
gs = goslate.Goslate()
text=gs.translate(primary_text, 'fr')

print(text)