pythonpython-3.xtranslatebing-translator-api

How to Convert Text from One Language to another using Microsoft's Translate API in Python?


I was trying to develop a Multi language Chat application which can convert text from one language to another using Python.

I have tried a couple of solutions and I found the Microsoft Bing Translate API to be a perfect solution for my requirement.

How to use that API in Python?


Solution

  • from translate import Translator
    
    class clsTranslate():
    
        def translateText(self, strString, strTolang):
            self.strString = strString
            self.strTolang = strTolang
            translator = Translator(to_lang=self.strTolang)
            translation = translator.translate(self.strString)
            return (str(translation))
    
    # Create a Class object and call the Translate function
    
    objTrans=clsTranslate()
    strTranslatedText= objTrans.translateText('How are you', 'de')
    
    print(strTranslatedText)