language-translator-python

Language Translator | Python

In this article, I will take you through creating a Language Translator with Python programming language.

Introduction:

Let’s create a Language Translator that accepts a language code and the text to be translated from the user, and produces the translated text and its pronunciation as the result.

Language Translation is very useful in our daily lives. Did anyone think about creating our own program to translate from one language to another by ourselves?
and what if I tell you!! You can do the same things with the help of a simple python script.

Outline:

In this project, we will take language code & text to be translated from the user as input and translate it into other languages and also provide the translated text. We just write our python script in any text editor or IDE and run it. After running the script we can enter the code of the language from the provided options and get it translated.

Project Prerequisites:

You have to install only one package in your system to run this script. Because we muse google translator API here.

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 detecting and translating.

Compatible with Python 3.6+.

For details refer to the API Documentation.

Features
  • Fast and reliable – it uses the same servers that translate.google.com uses
  • Auto language detection
  • Bulk translations
  • Customizable service URL
  • HTTP/2 support
See also  Weight Converter GUI | Python

Package Installation: 

> pip install googletrans==4.0.0-rc1

for more information about this library refer to this – googletrans · PyPI

 That’s it…! We just don’t need to install any other package for this project.

Code Implementation:

The first thing we need to do is to import the required packages and modules. In this project, we import the googletrans package to make use of its functions. We will be importing the Translator class from the googletrans package.

from googletrans import Translator

from googletrans import Translator

# Creating an object for the Translator class

translator = Translator()

# Creating an object for the Translator class
translator = Translator()

Next, we will be defining a dictionary called language. In this dictionary, we fix all the languages code for 10+ languages like the following.

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"
            }

In the next section, we ask the user for the language code and check if it is valid or not.

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!")

In the last section, we will start translating the given text to the selected language with the help of the googletrans package. We will loop until the user enters close for the exit.

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]}")

That’s it. We have completed the code implementation.

See also  Make Super Mario Game using Python.

Source Code:

Here is the complete source code of the project.

#pip install googletrans==4.0.0-rc1

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:

As we have completed the project let’s check the output.

Run the source code like the following terminal command:

After running the source code, there will be a prompt asking:

Please input desired language code. To see the language code list enter ‘options’

enter options to look for the available language codes:

See also  Bank Management Project with Python

Now, enter the appropriate language code from the above list.

Now, start entering the text and it will be translated.

You can also see the pronunciation of the text like the below example screenshot.

You can play with it any number of times and can exit by entering “close”.

Alright, Congratulations! We have successfully learned how to create our Language Translator.

Important Note:

Note that you can still improve this Language Translator and make it even better by changing its properties.
Go to the documentation of the googletrans, learn about it, and start playing with it.
Happy Coding.

Thank You!
By UdaySk

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh