Language Translator in python | Python Google Translator | 4 line source code

Step by Step Procedure to create Language Translator

Hello friends how are you, Today in this post "Language Translator in python" i am going to teach you how you can create your own Language translator using 4 lines of code only. If you are a computer science students or teacher and want to learn something interesting in programming then just go through this post. This can also be a simple and interesting project for you.

Language translator is one of the most used software now a days because in most of the cases we have to translate one language into another as per our requirements. Suppose that you want to make a blogger website and you have good knowledge of Hindi but you are unable to translate your Hindi words into English then in this case you need a translator and we mostly prefer google translator, but if you wan to use google translator then you must have a net connection. Here the most important point about this translator is that you don't need an internet connection to translate one language into another and you can create it using only 4 line of code.

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you will get a Download button and after clicking on this button you will get exe of latest python version just install it into your system. 

Step 2: Install Pycharm | Create Project | Install LibraryClick here to watch a single video on How to install Pycharm | Create Project | Install Library  or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 3: Create Project : Open Pycharm  click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "LanguageTranslator" after directory name, and at last click the create button to create this project. 

Step 4: Create Python file: To create a python file for coding in your project just right click on project name "LanguageTranslator" and select new and click on Python File , you will get popup screen in which you have to type a name like "Program" for python file and press enter, it will create a python file with name Program.py inside the project LanguageTranslator. 

Step 5: Install library (translate) : Before doing code for this we have to first, install the library which is used to translate one language to another. To install this library just click on Terminal which will be in Left bottom of screen , you will get a console screen and in this console screen you have to type pip install translate and then press enter. After some seconds you will get a message like library is installed successfully. Still facing problem then watch the above video, i have explained everything.


Step 6: Code ExplanationNow i am going to explain the code line by line

Line 1: import library: Here in this we will import the needed library that convert one Language into another. 

from translator import Translator

This is the first line which used to import the library translator. This library is used to translate one language into another.

Line 2: Set Language :Here in this line we will set a language in which we want  to translate.
translator=Translator(to_lang="Chinese")

Here translator is a Variable, Translator() is a predefined function and Chinese is the language in which we want to translate the given message or sentence.  You can type any language like German Hindi Japanese etc in place of Chinese in which you want to translate.


Line 3: Pass Message or Sentence: Here in this step we will pass message or sentence that we want to translate into and Languages.
translation = translator.translate("Hello friend How are you?")

Here translation is a variable that will store the translated message ,


Line 4: Printing Translated Message: Here we just display the translated message using print function.
print(translation)
Here print function will print "嗨,朋友你好吗?" and it is the Chinese translation of "Hello friend How are you?"

Step 7: Complete Python code:  Here is the complete code for this project/program you just need to type this code into your python file or you can copy this code for your personal use.

#import library
from translate import Translator
#set language in which you want to translate
translator= Translator(to_lang="Chinese")
#set message that you want to translate
translation = translator.translate("Hello friend How are you?")
#print translated message
print(translation)

Just copy this code from here and paste it into program.py and to run this code right click on code area and click on Run Program you will get output like below
Python Google Translator | 4 line source code

GUI Based Language Translator: Now i am going to give you something more interesting using this code . It will give you a GUI interface and where you can write your sentences into  Textbox and you can select your desired language and then press a button Translate and finally you will get  your sentences into your selected language.

Complete Code :Here is the complete code for GUI based Language Translator

#import library
from tkinter import*
from translate import Translator
from tkinter import ttk
#translation
def translate():
 #get input from user
 ip=engVariable.get()
 #get selected langauge
 langauge=lang.get()
 #creating variable of Translator
 translator = Translator(to_lang=langauge)
 #Translation
 translation = translator.translate(ip)
 #set translated message into Label
 transVariable.set(translation)

#creating GUI for Transator
root =Tk()
#setting window height and width
root.geometry("600x200")
#setting window background color
root["bg"]="#20325B"
#setting title of window
root.title("Krazy:Language Translator")
#creating variable for messages
global engVariable
global transVariable
global lang;
engVariable=StringVar()
transVariable=StringVar()
lang=StringVar()
#Entry/Textbox for message/Sentence
Entry(root,text="Hello",textvariable=engVariable,font="Arial 12",fg="black",width=42).place(x=80,y=30)
#combox for language
fontExample = ("Courier", 12, "bold")
comboExample = ttk.Combobox(root,textvariable=lang,
                            values=[
                                    "Hindi",
                                    "German",
                                    "Chinese",
                                    "Japanese"],
                            #here you can add many more languages
                            font = fontExample)

root.option_add('*TCombobox*Listbox.font', fontExample)
comboExample.place(x=80,y=70)

#button for translate
Button(root,text="Translate",font="Arial 12 bold",width="10",command=translate,bg="orange").place(x=350,y=70)
#label for generated/success message
Label(root,text="Hello",textvariable=transVariable,font="Arial 12",bg="#20325B",fg="white").place(x=80,y=120)
root.mainloop()

Here you just need to type this your code into program.py file or you can copy this code for your personal use only.

Step 8: Run Code: To run this code just right click on the coding area and click on Run Program and you will get a message output screen like below.

Language Translator in python

Here you have to type a sentence into given text box , after that select a language from dropdown in which you want to translate, for example see the below screenshot

Language Translator in python

here the sentence is Hello Friend How are you? and we have to translate it into Japanese.

Now here the last step is to click on Translate button to translate the given sentence, you will get a screen like below.

Language Translator in python

I hope now you can "Language Translator in python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

0 Comments