Language Translator

How To Build Language Translator Using Google API in Python

How To Build Language Translator in python

Hello coder, welcome to the codewithrandom blog. In this article, we will Build Language Translator Using Google API in Python with Complete Source Code. Language translation is an essential aspect of communication between people who speak different languages. In this blog, we will learn how to build a language translator using the Google API in Python. There are 16 languages you can transate into this code.

Language Translator

To Build the Language Translator in Python , you can follow these steps:

Project Requirements

Open your terminal and Install the module we just talked about, the following commands would be of use.

$ pip install googletrans

Language options and their code 👇👇

| Code |Language |

| bn | Bangla |

| en | English |

| ko | Koren |

| fr | French |

| de | German |

| he | Hebrew |

| hi | Hindi |

| it | Italian |

| ja | Japanese |

| la | Latin |

| ms | Malay |

| ne | Nepali |

| ru | Russian |

| ar | Arabic |

| zh | Chinese |

| es | Spanish |

How To Run The Code :

step 1: open command prompt and install googletrans library.

step 2: open any python code Editor.

ADVERTISEMENT

step 3:  Make a python file main.py

ADVERTISEMENT

step 4:  import library

ADVERTISEMENT

step 5:  Copy the code & Past it

ADVERTISEMENT

step 6:  Run the file main.py and your program will run

ADVERTISEMENT

complete program code👇👇👇

from googletrans import Translator

translator = Translator()

language = {"bn": "Bangla",
            "en": "English",
            "ko": "Koren",
            "fr": "French",
            "de": "German",
            "he": "Hebrew",
            "hi": "Hindi",
            "it": "Italian",
            "ja": "Japanese",
            'la': "Latin",
            "ms": "Malay",
            "ne": "Nepali",
            "ru": "Russian",
            "ar": "Arabic",
            "zh": "Chinese",
            "es": "Spanish"
            }

allow = True  # variable to control correct language code input

while allow:  # checking if language code is valid

    user_code = input(
        f"Please input desired language code. To see the language code list enter 'options' \n")

    if user_code == "options":  # showing language options
        print("Code : Language")  # Heading of language option menu
        for i in language.items():
            print(f"{i[0]} => {i[1]}")
        print()  # adding an empty space

    else:  # validating user input
        for lan_code in language.keys():
            if lan_code == user_code:
                print(f"You have selected {language[lan_code]}")
                allow = False
        if allow:
            print("It's not a valid language code!")

while True:  # starting translation loop
    string = input(
        "\nWrite the text you want to translate: \nTo exit the program write 'close'\n")

    if string == "close":  # exit program command
        print(f"\nHave a nice Day!")
        break

    # translating method from googletrans
    translated = translator.translate(string, dest=user_code)

    # printing translation
    print(f"\n{language[user_code]} translation: {translated.text}")
    # printing pronunciation
    print(f"Pronunciation : {translated.pronunciation}")

    for i in language.items():  # checking if the source language is listed on language dict and printing it
        if translated.src == i[0]:
            print(f"Translated from : {i[1]}")

OutPut👇👇

Language Translator

Conclusion

Hurray! You have successfully the Build Language Translator project using the googletrans Python  modules. We learned to create simple python project, Language Translator . There are 16 languages you can translate into this code.  Hope you enjoyed building with us!

Converting numbers to words in Python

Build A Currency Converter in Python

 



Leave a Reply