The Python Programmer’s Toolkit: Essential Libraries for Translation

Translation Libraries in Python

  • googletrans
  • deep_translator
  • translate
  • translators

googletrans

Googletrans is a free and unlimited Python library that implements Google Translate API. It utilizes the Google Translate Ajax API for executing calls to functions like detect and translate.

pip install googletrans==3.1.0-alpha
from googletrans import Translator

translator = Translator()

text = "Yurtta sulh, cihanda sulh!"

translated = translator.translate(text, src="tr", dest="en").text
print(translated)

#Peace at home peace in the world!

deep_translator

A flexible FREE and UNLIMITED tool to translate between different languages in a simple way using multiple translators.

pip install deep-translator

There are many different integrated translators in the library.

from deep_translator import (GoogleTranslator,
                             ChatGptTranslator,
                             MicrosoftTranslator,
                             PonsTranslator,
                             LingueeTranslator,
                             MyMemoryTranslator,
                             YandexTranslator,
                             PapagoTranslator,
                             DeeplTranslator,
                             QcriTranslator,
                             single_detection,
                             batch_detection)

For all translators that require an ApiKey, you can either specify it as an argument to the translator class or you can export it as an environment variable, this way you won’t have to provide it to the class.

Example: export OPENAI_API_KEY=”your_key”

langs_list = GoogleTranslator().get_supported_languages()
langs_list
"""
['afrikaans', 'albanian', 'amharic', 'arabic', 'armenian', 'assamese', 'aymara', 'azerbaijani', 'bambara'...
"""

translated = GoogleTranslator(source='tr', target='en').translate(text)
print(translated)
# Peace at home peace in the world!

translate

Translate is a simple but powerful translation tool written in Python with support for multiple translation providers. By now it offers integration with Microsoft Translation API, Translated MyMemory API, LibreTranslate, and DeepL’s free and pro APIs.

pip install translate
from translate import Translator
translator = Translator(from_lang="tr", to_lang="en")
translation = translator.translate(text)
print(translation)
# Peace at home peace in the world.

translators

Translators is a library that aims to bring free, multiple, enjoyable translations to individuals and students in Python. It supports a great variety of providers.

pip install translators
import translators as ts

print(ts.translate_text(text, translator="bing", from_language="tr",to_language="en"))
print(ts.translate_text(text, translator="google", from_language="tr",to_language="en"))
print(ts.translate_text(text, translator="alibaba", from_language="tr",to_language="en"))

"""
Peace at home, peace in the world!
Peace at home peace in the world!
Peace in the dormitory, peace in the world!
"""

Read More

6 Python Packages for Working with PDF Files

Drawing with Code: Using PyAutoCAD to Automate AutoCAD in Python

Building a Neural Network from Scratch in Python: A Step-by-Step Guide

Spearman’s Rank Correlation

Python Trading Guide: MACD

Sources

https://pypi.org/project/googletrans/

https://pypi.org/project/deep-translator/

https://pypi.org/project/translate/

https://pypi.org/project/translators/

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics