Building a language translator with Python
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.
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.
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:
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:
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.
Take this for example:
This will translate "Hello" into french. But we actually need to see the translated text. To do this, we need to type:
This will print the result text (for me it is
t.text as I named my variable
t but for you it should be
print(t.origin), and this will print the original text.
Hope you all liked it :)
Comments, upvotes and feedback are appreciated :)
I keep on getting an error that french is not defined...
Is there a line I missed?
Do you have download a package?