Python is a versatile programming language that can be used for a wide range of applications including language translation. In this article we will explore how to create Language Translator in Python that asks for user input language and output language and then converts accordingly.
Video Procedure:
Write a Python Program to Detect Abusive Comment
Topic Identification System in Python
Concept Identification System in Python
To begin we need to install necessary libraries for our Python Language Translator. We will use googletrans
library to perform language translation. To install the googletrans
library we can use the following command in our Colab notebook:
!pip install googletrans==4.0.0-rc1
Once we have installed googletrans
library we can begin writing our Python Language Translator program.
The first step is to import necessary libraries. We will import the googletrans
library as well as Text
module from the googletrans
library.
from googletrans import Translator, LANGUAGES
from googletrans.models import Text
Next we will create a function called translate_text()
that will handle language translation. This function will take two parameters: the input language and output language.
def translate_text(input_lang, output_lang):
translator = Translator()
text = input("Enter the text you want to translate: ")
translation = translator.translate(text, src=input_lang, dest=output_lang)
print(f"Translated text: {translation.text}")
Inside the translate_text()
function we first create a translator
object using Translator()
method from googletrans
library. We then prompt the user to enter text they want to translate. We store this input in the text
variable.
Next we use the translate()
method of translator
object to perform translation. We pass text
variable as the first parameter, input_lang
as src
parameter, and output_lang
as the dest
parameter. The translate()
method returns a Translated
object, which we store in the translation
variable.
Finally we print the translated text using translation.text
attribute.
Now that we have defined our translate_text()
function we can create main program that will ask user for input language and output language and then call the translate_text()
function to perform the translation.
print("Welcome to the Python Language Translator!")
print("Here is the list of supported languages:")
for code, lang in LANGUAGES.items():
print(f"{code}: {lang}")
input_lang = input("Enter the input language code: ")
output_lang = input("Enter the output language code: ")
translate_text(input_lang, output_lang)
In the main program we first print a welcome message to user. We then loop through LANGUAGES
dictionary of the googletrans
library to print a list of supported languages and their corresponding language codes.
Next we prompt the user to enter input language code and output language code. We store these inputs in the input_lang
and output_lang
variables.
Finally we call translate_text()
function, passing in input_lang
and output_lang
variables as parameters.
Here is complete code implementation of our Python Language Translator:
from googletrans import Translator, LANGUAGES
def translate_text(input_lang, output_lang):
translator = Translator()
text = input("Enter the text you want to translate: ")
translation = translator.translate(text, src=input_lang, dest=output_lang)
print(f"Translated text: {translation.text}")
print("Welcome to the Python Language Translator!")
print("Here is the list of supported languages:")
for code, lang in LANGUAGES.items():
print(f"{code}: {lang}")
input_lang = input("Enter the input language code: ")
output_lang = input("Enter the output language code: ")
translate_text(input_lang, output_lang)
Output:
![](https://programmerschallenge.tech/wp-content/uploads/2023/03/image-19-1024x502.png)
Here are some useful links related to Python language translation:
- Googletrans library documentation: The official documentation of googletrans library provides detailed information on how to use library to perform language translations in Python. The documentation includes code examples a list of supported languages and instructions on how to install and use library. Here is link: https://py-googletrans.readthedocs.io/en/latest/
- Language codes: If you are looking for list of language codes supported by googletrans library, you can refer to the LANGUAGES dictionary in the googletrans module. LANGUAGES dictionary contains a mapping of language codes to their corresponding names. Here is the link to the googletrans module on GitHub: https://github.com/ssut/py-googletrans/blob/master/googletrans/constants.py
- PyPI page for Googletrans: PyPI is official repository for Python packages. You can find more information about the googletrans library on its PyPI page. Here is the link: https://pypi.org/project/googletrans/
- Google Translate API: If you need to perform large number of translations or require advanced translation features, you may want to consider using Google Translate API. The Google Translate API is paid service that provides access to Google’s machine translation technology. Here is the link to the API documentation: https://developers.google.com/admin-sdk/directory/v1/languages
I hope these links are helpful for your project!