Python Tutorial: How to Translate Languages in Python?

Posted in /  

Python Tutorial: How to Translate Languages in Python?

Vinay Khatri
Last updated on June 10, 2022

    Google Translator is one of the best text translator tools, and its API is available for many programming languages. Thus, you can write a program in any programming language of your choice to translate a text written in some language to many other languages.

    In this Python tutorial, we will explain how to translate languages in Python by using the Google Translate API. We will write a Python program that can translate a given text from one language to another.

    To start, we need to install the Google Translate API or the googletrans library for Python.

    Install the Python Google Translate API

    To install the Google Translate API, we can use the pip install command followed by the Google Translate API name, i.e., 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.

    So, for this tutorial, we will be installing the prerelease version of the Python googletrans library, i.e., 4.0.0rc1 . Run the following pip install command to install the Google Translate API v4.0.0rc1 for your Python environment:

    pip install googletrans==4.0.0rc1
    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'

    After successfully installing the googletrans==4.0.0rc1 library, open your best Python IDE or text editor , and get ready to write some Python code.

    List all the Languages Supported by Python Google Translator

    Before writing the actual Python program for translating text, let's list all the languages supported by the Python googletrans library. To list out 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'}

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

    How to Translate Languages in Python?

    You can translate any text from one language to another among the 107 languages supported by the Google Translate API, but we will be translating English to Hindi for this tutorial.

    Now, let's write a Python program that will translate an English phrase to its Hindi equivalent. We will start with importing the required module, Translator , from the googletrans library.

    from googletrans import Translator

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

    #initialize the Translator
    translator = Translator()

    Now, ask the user to enter the text to translate by the 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 have also specified the source_lan and translated_to variables to "en" and "hi" that are the language codes for the English and Hindi languages, respectively. 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 google translate() method automatically detects the source language of the text. The dest attribute specifies the destination 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.

    # 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

    In this Python tutorial, we learned how to translate languages in Python using the googletrans library. At present, Google Translate provides support for 107 different languages. The Python googletrans API version 3.0.0 has many bugs, so we will suggest you not use it. Instead, use an older stable or newer unstable or unreleased version of the googletrans library in your code.

    People are also reading:

    Tags:
    python

    Leave a Comment on this Post

    0 Comments