Skip to main content

Command Palette

Search for a command to run...

Building a language detection and auto-translation CLI program in python.

Published
4 min read
Building a language detection and auto-translation CLI program in python.

Building a cool side project can be very challenging but even more challenging than that is coming up with a great ( or maybe not so great ) idea of what you should build. We have all built "to-do lists" and i'm sure you do not want to build another "to-do" list or any variation of it. To help you out, i came up with an idea for a fun project that we can build using two libraries i've worked with - langdetect and translate.

Before i go further, i'll like to state early that this post is targeted at python developers ( beginners or intermediate ) who want to add a beautiful project to their portfolio. However, since no knowledge is ever lost, i'll advice that you read through even if you're not a pythonista.

What are we building?

Before i go over the whole idea, i think it'll be helpful if i gave brief explanations of the libraries i mentioned earlier and how they will be instrumental to what we will be building later in this article. As i mentioned in the first paragraph, i'll be making use of two python libraries - langdetect and translate.

The langdetect library basically reads through text provided by the user and detects what language is used in writing the text.

The translate library as you might have guessed, translates from one language to another. While, this library auto-detects the language in use, it'll just act as a vibe killer to our fun program. So, we'll be the detectors here :)

Now back to the idea. The task is to build a CLI ( command line interface ) program that:

  • Accepts user input ( words and/or sentences )

  • detects the language used by the user; and

  • translates the user's text from its original language to the English language which i believe we can all understand. Right?

Exciting right? The program is actually simpler than it sounds. I mean, it's python. But before we get to the main program, can i walk you through how these libraries work? Great!

How "langdetect" works:

The first step for getting your hands dirty with the langdetect library is to install it.

On windows, you can install it by entering the command below:

pip install langdetect

Ready to start detecting languages from text? First of all, you need to import the library as shown below:

from langdetect import detect
fing_language=detect("Hello, World!")
print(find_language)#prints "en"

Then, you call the detect method to start detecting the language(s) of text. Simple right? Yeah!

How "translate" works:

Just like the langdetect library, there is not much to learn about the translate library - it is very simple too. To get started with the translate library, you'll need to install it first:

pip install translate

After you have successfully installed the library, you need to import the Translator method from the the library. This method does all the work we need.

from translate import Translator

And to translate, we do this:

translator = Translate(from_lang="<PREFERRED LANGUAGE>",to_lang="en")
translator.translate("Bonjour")
#prints "Hello"

Putting them together

The logic for this project is very simple. It's not very big, but breaking every step step down will be very helpful in understanding what is going on. So, here goes nothing:

  1. Step 1: import the libraries ( langdetect and translate )

  2. Step 2: Accept input from the user

  3. Detect the language enetered by the user and choose which language to convert to

  4. Convert from the detected language to you preferred language ( in this case, English )

  5. Print the result and celebrate !

Here's the full code ( with comments ):

from langdetect import detect
from translate import Translator

#accepting user inputs
user_input=input("Enter Text from any languge:")

#detect the language of the user_input
find_language=detect(user_input)
#choose the detected language as the language to convert from, to English
translator=Translator(from_lang=find_language,to_lang="en")
#translate the user_input
translation=translator.translate(user_input)
print(translation)

#now play with the program

Conclusion

If you've made it to this part of the article, i commend you. As i have said earlier, this project is not difficult but is fun and something new. Through this blog post, you have worked with and learned two useful python library and have combined them to build a useful project which i strongly recommend that you share. Thank you so much for your time. See you in the next tutorial.