Build Language Translator With Python
Language Translator helps you to read and gain knowledge from the articles and the books written in different languages. Google Translator API helps us to create a language translator with Python. In this article you will learn to create a language translator with Python.
Build Language Translator With Python
With the help of Google Translate, you can easily translate text in any language. But as programmer, it creating our own language translator gives a satisfying experience. By using the Google API known as google_trans_new we can translate text in many languages. You can use this API to do all the tasks that you can do with Google Translate.
Firstly, install this library on your system by using the following command. Write and execute the below command in command prompt (cmd) or terminal of your IDE.
pip install google_trans_new
After installing the library on the system, now let's code. Create a python file in which you will write the code.
Also Read - Extract Phone Number Details with Python
1) First thing we will do is to import the module.
from google_trans_new import google_translator
2) Then create a translator object.
translator = google_translator()
3) Take text input from the user that he want to translate.
text = input("Enter the text ")
4) Translating text to other language
translate_text = translator.translate(text,lang_tgt='fr')
The above code translates the text in French language as you can see "fr" is written.
5) Print the translated text
print(translate_text)
That's all we have to write. Now, run the above code and enter the text and you will find the French text as output. Let's try to run.
Output:-
Enter the text Hello, I hope you all are fine.
Bonjour, j'espère que vous allez tous bien.
BOOM!! We finally created a Language Translator with Python. You can change the language to any other and it will translate in that.
Also Read - QR Code Generator With Python
Note - If you get error while running the code, you have to do some change in the google_trans_new.py file present in the following directory.
C:\Users\<SYSTEM_NAME>\AppData\Roaming\Python\Python38\site-packages\google_trans_new
In this file, change line 151 which is : response = (decoded_line + ']') to response = decoded_line.
As you will change the line, error will be resolved. This is how you can Build a Language Translator with Python. You can make this app with more languages and also you can create Graphical User Interface (GUI's) with Tkinter, PyQt5, Kivy or with any of your favourite GUI library to make the project more interactive.
So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.
Enjoy Coding!!!
Also Read :-
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.