How to Translate Languages in Python Using Googletrans?

Posted in /  

How to Translate Languages in Python Using Googletrans?
vinaykhatri

Vinay Khatri
Last updated on March 1, 2024

    You might have used Google Translate at some point in time. It is one of the best language translators that help you translate text written in one language into 100 different languages. But how does it do? It is all because of Google Translate API Google developed.

    Using this API, you can write a program in any programming language to translate a text written in some language to many other languages.

    In this blog post, we will use Google Translate API to write a Python program to translate languages. We will need Googletrans, a Python library that implements Google Translate API.

    Before writing our Python program, we must install the Googletrans library using pip. Let us see how to install it below.

    What is Googletrans and How to Install it?

    Googletrans is a free Python library that implements Google Translate API. It makes unofficial AJAX calls to Google Translate API to identify and translate text in one language into the desired one. It works well with Python 3.6+.

    Features of Googletrans

    • This Python translation library is extremely fast and reliable because it leverages the same servers as translate.google.com .
    • It automatically detects the language of the given text.
    • It is capable of translating text in bulk.
    • Googletrans offers HTP/2 support.

    You must know some important points before using this Python Language translator library:

    • The maximum number of characters the library supports for a single text is 15k.
    • There is no guarantee that the library will always work well due to the limitations of Google Translate’s web version.
    • There are chances to receive HTTP 5xx errors or errors like #6. This is because Google might have blocked your client IP address.

    How to Install Googletrans?

    We install this library using the pip install command followed by the library name – googletrans.

    pip install googletrans

    If you directly install the googletrans library without specifying its version, pip will install the googletrans-3.0.0 for your Python environment, and there are some bugs in this version.

    Note: Do not use pip install googletrans to install the googletrans library, as it will throw the following error while executing the program.

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

    So, for this tutorial, we will install the prerelease version of the Python googletrans library, i.e., 4.0.0rc1 . We will use the same command as we used above.

    pip install googletrans==4.0.0rc1

    After successfully installing the googletrans==4.0.0rc1 library, open your Python IDE or text editor , and write a program that converts text written in one language into another.

    Before writing the actual program for text translation, let's list all the languages supported by the Python googletrans library.

    List all Languages Supported by Python Google Translator

    To do so, execute the following Python code on 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'}

    From the above output, you can see that, currently, the googletrans library supports 107 different languages.

    How to Translate Languages in Python? A Step-by-Step Process

    You can translate any text from one language to another among the 107 languages supported by googletrans . But this tutorial shows an example of translating English text into Hindi.

    Now, let's write a Python program that will translate an English phrase into its Hindi equivalent. Here are the steps:

    1. We will start importing the required module, Translator, from the googletrans library.
    from googletrans import Translator
    1. Next, let's initialize the Translator module and create its object translator.
    #initialize the Translator
    translator = Translator()
    1. Now, ask the user to enter the text to translate by Google Translator.
    text = input("Enter your Text: ")
    source_lan = "en"#en is the code for the English Language
    translated_to= "hi"#hi is the code for the Hindi Language

    Here we have also specified the source_lan and translated_to variables to "en" and "hi" which are the language codes for the English and Hindi languages, respectively.

    1. Now, translate the user-entered text using the translate(text, src, dest) function.
    #translate text
    translated_text = translator.translate(text, src=source_lan, dest = translated_to)

    We do not need to specify the src attribute because the translate() method automatically detects the text’s source language. The dest attribute specifies the destination language code.

    1. 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.

    Python Program to Translate Text Using Googletrans

    from googletrans import Translator
    
    #initialize the Translator
    translator = Translator()
    
    text = input("Enter your Text: ")
    
    source_lan = "en"
    translated_to= "hi"#hi is the code for Hindi Language
    
    #translate text
    translated_text = translator.translate(text, src=source_lan, dest = translated_to)
    
    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}")

    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

    This was all about translating languages in Python using googletrans. We have used the googletrans library, a Python translation library that makes unofficial calls to the Google Translate API. This library supports 107 languages, as mentioned in the above post. Simply import this library into your program and use the translate() function to translate text written in one language to another.

    People are also reading:





    Leave a Comment on this Post

    0 Comments