Translation language in Python

Google Translator is one of the best text translation tools, and its API can be used in many programming languages. Therefore, you can write programs in any programming language of your choice and translate text written in one language into many other languages. In this Python tutorial, we will explain how to use the Google Translate API to translate the language in Python.

We will write a python program that can translate a given text from one language to another. First, we need to install the Google Translate API or the Google trans library for Python.

For English translation: Python language translator

Install Python Google translation API

To install the Google Translate API, we can use the pip install command followed by the Google Translate API name, that is, Google trans:

pip install googletrans

If you install the Google trans library directly without specifying the version, pip will install Google trans-3.0 for your Python environment 0, there are some bug s in this version. Therefore, for this tutorial, we will install a pre release version of the python Google trans library, 4.0.0 0rc1.

Run the following pip install command to install Google Translate API v4.0 for your Python environment 0.0rc1:

pip install googletrans==4.0.0rc1

Note: do not use PIP install Google RANS to install the Google RANS library, because it will throw the following error when executing the program.

code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

Successfully installed Google trans = = 4.0 After 0rc1 library, open your best Python IDE or text editor and prepare to write some Python code.

Lists all languages supported by Python Google translator

Before writing an actual Python program for translating text, let's list all the languages supported by the python Google trans library. To list all the languages supported by the Python GoogleTrans API, execute the following Python code on your Python IDE or text editor:

import googletrans

print("Number of Supported Languages:", len(googletrans.LANGUAGES)) 
print(googletrans.LANGUAGES)

output

Number of Supported Languages: 107
{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 
 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 
 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 
 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 
 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 
 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 
 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish',
 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian',
 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole',
 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 
 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic',
 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian',
 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh',
 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)',
 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian',
 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian',
 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese',
 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)',
 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian',
 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian',
 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona',
 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali',
 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik',
 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian',
 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh',
 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'}

How do I translate languages in Python?

Of the 107 languages supported by the Google Translate API, you can translate any text from one language to another, but we will translate English into Hindi in this tutorial. Now, let's write a Python program to translate English phrases into the corresponding Hindi. We will start by importing the required module Translator from the google trans library.

from googletrans import Translator

Next, let's initialize the Translator module and create its object converter.

#initialize the Translator
translator = Translator()

Now, the user is asked to enter the text to be translated by Google translator.

text = input("Enter your Text: ")

source_lan = "en"  #en is the code for Hindi Language
translated_to= "hi" #hi is the code for Hindi Language

Here we will also source_lan and translate_ The to variable is specified as "en" and "hi", which are the language codes of English and Hindi, respectively. Now, use the translate(text, src, dest) function to translate the text entered by the user.

#translate text
translated_text = translator.translate(text, src=source_lan, dest = translated_to)

We don't need to specify the src attribute because the google translate() method will automatically detect the source language of the text. The dest property specifies the target language code. After translating the text, print the translated text.

print(f"The Actual Text was {text}")
print(f"The Translated Text is: {translated_text.text}")
print(f"The Translated Text pronunciation is {translated_text.pronunciation}")

Now put all the code together and execute.

Output

Enter your Text: Welcome to techgeekbuzz.com!
The Actual Text was Welcome to techgeekbuzz.com!
The Translated Text is: Techgeekbuzz.com पर आपका स्वागत है!
The Translated Text pronunciation is taichhgaiaikbuzz.chom par aapaka svaagat hai!

conclusion

In this Python tutorial, we learned how to use the google trans library to translate languages in Python. Currently, Google Translate supports 107 different languages.

Keywords: Python list

Added by bluesoul on Fri, 10 Dec 2021 16:29:08 +0200