Package: tranzlate
tranzlateA wrapper around the translators package by UlionTse that enables multilingual translation of text, files, markup and BeautifulSoup objects.InstallationPerform a quick install with pip:pipinstalltranzlateUsageCreate a TranslatorTheTranslatorclass is the main interface for the package. It can be used to translate text, files, markup and BeautifulSoup objects. To create an instance of theTranslatorclass, simply import thetranzlatepackage and instantiate theTranslatorclass:importtranzlatetranslator=tranzlate.Translator()Use a Custom Translation EngineBy default, theTranslatorclass uses Bing Translator as its translation engine. Based on test results, Bing is the most reliable translation engine. However, you can use any translation engine that is supported by the translators package by passing the name of the engine to theTranslatorclass on instantiation:In this example, we will use Google Translate as our translation engine:importtranzlatetranslator=tranzlate.Translator('google')To get a list of all supported translation engines:importtranzlateprint(tranzlate.Translator.engines())# Output:# ['bing', 'google', 'yandex', 'baidu', 'sogou', 'tencent', 'deepl', 'alibaba', ...]Detect Languageimporttranzlatetranslator=tranzlate.Translator()text='Good Morning!'detected_lang=translator.detect_language(text)print(detected_lang)# Output: enTranslate Text, Markup and BeautifulSoup ObjectsTo translate text, markup or a soup, you can use thetranslatemethod of theTranslatorclass. Thetranslatemethod is a general purpose method that can be used to translate text, markup and BeautifulSoup objects.Translate Textimporttranzlatetranslator=tranzlate.Translator()text='Good Morning!'translation=translator.translate(text,target_lang='yo')print(translation)# Output: Eku ojumo!Translate Markupimporttranzlatetranslator=tranzlate.Translator()markup='<p>Good Morning!</p>'translated_markup=translator.translate(markup,target_lang='yo',is_markup=True)print(translated_markup)# Output: <p>Eku ojumo!</p>Translate BeautifulSoup Objectsimporttranzlatefrombs4importBeautifulSouptranslator=tranzlate.Translator()markup='<p>Good Morning!</p>'soup=BeautifulSoup(markup,'html.parser')translated_soup=translator.translate(soup,target_lang='yo')However, there are specialized methods for translating text, markup and BeautifulSoup objects. These methods aretranslate_text,translate_markupandtranslate_souprespectively.Translate FilesTo translate files, we use thetranslate_filemethod of theTranslatorclass.importtranzlatetranslator=tranzlate.Translator()translated_file=translator.translate_file('path/to/file.txt',src_lang="en",target_lang='yo')It is advisable to specify the source language when performing translations as it helps the translation engine to provide more accurate translations.Use a ProxyTo use a proxy, simply pass the proxy to theTranslatorclass on instantiation:importtranzlatetranslator=tranzlate.Translator()text='Good Morning!'translation=translator.translate(text,target_lang='yo',proxies={'https':'https://<proxy>:<port>'})print(translation)Other MethodsGet Supported LanguagesTo get a list of all supported (source) languages by the translator's engine:importtranzlatetranslator=tranzlate.Translator()print(translator.supported_languages)Check if a Language is SupportedTo check if a (source) language is supported by the translator's engine:importtranzlatetranslator=tranzlate.Translator()is_supported=translator.is_supported_language('yo')print(is_supported)# Output: TrueCheck if a Language Pair is SupportedTo check if a language pair is supported by the translator's engine:importtranzlatetranslator=tranzlate.Translator()is_supported=translator.is_supported_pair(src_lang='en',target_lang='yo')print(is_supported)# Output: TrueGet a List of Supported Target Languages for a Source LanguageTo get a list of supported target languages for a source language:importtranzlatetranslator=tranzlate.Translator()supported_target_languages=translator.get_supported_target_languages('en')print(supported_target_languages)TestingTo run the tests, simply run the following command in the root directory of your cloned repository:python-munittestdiscovertests"test_*.py"ContributingContributions are welcome. Please open an issue or submit a pull request.