Build a Translator using Python

Build a Translator using Python

In this Blog article, we will learn how to Create a Translator . We will see the implementation in python.

Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video tutorial for better Understanding

What will be covered in this Blog

1. Translator Introduction
2. What is Googletrans?
3. Create a Translator with Python

Translator Introduction:

The Dictionary definition:

The definition of a translator is someone who helps people who speak different languages to communicate or who takes something (such as a speech or a book) in one language and who puts it into a different language for people to understand.

If you wish to know more about it, you can refer to Translation Wikipedia Page. Use this link to navigate to the GIF's Wikipedia Page.

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.

  • Fast and reliable - it uses the same servers that translate.google.com uses
  • Auto language detection
  • Bulk translations
  • Customizable service URL
  • Connection pooling (the advantage of using requests.Session)
  • HTTP/2 support

If you wish to know more about it, you can refer to Googletrans Documentation. Use this link to navigate to the documentation.

Now that you are aware of Translator and Googletrans basics, we can move forward to the coding section.

Time to code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

carbon (6).png

Installing Googletrans

Open your terminal and run the following command

pip install googletrans

Now that we have the package, we are ready to import it in our python script.

from googletrans import Translator

Let's store some text that we will use for translation. You can use any text segment of your choice. I am using of one of my favourite poem Daffodils by WILLIAM WORDSWORTH. Let's store it in text. It's the last stanza of the poem.

The language I am using is French. You can choose any language of your choice.

You can even take the text as a user input if you like, so you wont have to make changes in your program every time you wish to use the translator.

text = '''  Pour souvent, quand sur mon canapé je mens
D'humeur vacante ou songeuse,
Ils clignotent sur cet œil intérieur
Quel est le bonheur de la solitude;
Et puis mon cœur se remplit de plaisir,
Et danse avec les jonquilles. '''

Let's create an instance of Translator to use.

translator = Translator()

This uses the Google Translate Ajax API to make calls to such methods as detect and translate. Now, let's call the detect method on translator and pass in our data stored in text and store it in lang.

lang = translator.detect(text)
print(lang)

Now let's print it out.

  • lang=fr means the detected language is French.
  • confidence=0.9365149 is how much percentage the translator is confident about the language detection.
Detected(lang=fr, confidence=0.9365149)

Once done, let's move forward and actually translate it. I am calling the translate method here on translator and passing two parametrs.

  • text: our data in original language
  • dest: our destination language

Now, to refer the other language codes you can refer to the google language documentation

NOTE: If you do not specify any destination language, it will automatically switch to the default one i.e. English.

res = translator.translate(text, dest = 'en')

At last, let's verify it by printing our res.

Often when on my couch I lie
In a vacant or pensive mood,
They blink on that inner eye
What is the happiness of solitude;
And then my heart fills with pleasure,
And dance with the daffodils.

This is how the conversion is done. This is all about the creating a Translator with Python. That's it! simple, isn't it? Hope this tutorial has helped.

You can play around with the library and explore more features and even make use of Python GUI using Tkinter.

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Thank you for reading, I would love to connect with you at Twitter | LinkedIn. I would recommend you to Check out the YouTube video of the same and don't forget to subscribe my Channel.

Do share your valuable feedback and suggestions!

You should definitely check out my other Blogs:

Resources:

  • See you in my next Blog article, Take care

Did you find this article valuable?

Support Ayushi Rawat by becoming a sponsor. Any amount is appreciated!