In this Python article we are going to learn about Google Translate API with Python, so for this purpose we are going to use a library that is called googletrans. so Googletrans is a free and unlimited python library that implemented Google Translate API with Python. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.
Python Firebase SDK Tutorials
1: Python Firebase SDK Integration with Real Time Database
2: Python Firebase SDK working with Cloud Firestore
3: Python Firebase SDK working with Authentication
What is Google Translate?
According to Wikipedia Google Translate is a free multilingual statistical and neural machine translation service developed by Google, to translate text and websites from one language into another. and there are 109 language that are available for google Translate.
Installation of Google Translate API for Python
As we have already said that we are going to use googletrans library, so you can easily use pip for Googletrans installation.
1 |
pip install googletrans |
These are the available feature for Googletrans.
- Fast and reliable – it uses the same servers that translate.google.com uses
- Auto language detection
- Bulk translations
- Customizable service URL
- Connection pooling (the advantage of using requests.Session)
- HTTP/2 support
Note on library usage
- The maximum character limit on a single text is 15k.
- Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don’t care about stability.)
- If you want to use a stable API, I highly recommend you to use Google’s official translate API.
- If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
OK now let’s first check that which languages are available in this google translate library. you can use this code for finding the number of languages.
1 2 3 |
import googletrans print(googletrans.LANGUAGES) |
Run the code and this is the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
{'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'} |
So now let’s detect languages using Google Translate API with Python, you can use this code for detecting the languages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from googletrans import Translator text1 = "subscribe my channel" text2 = "suscríbete a mi canal" text3 = "kanalıma abone ol" translator = Translator() print(translator.detect(text1)) print(translator.detect(text2)) print(translator.detect(text3)) |
In the above code first we have imported Translator from googletrans, after that we have created three text in different languages, the first one is English, second one Spanish and third one Turkish. you can simply use detect() method for finding the language of the text.
Now if you run the code this will be the result. three languages are detected successfully.
1 2 3 |
Detected(lang=en, confidence=1.0) Detected(lang=es, confidence=1.0) Detected(lang=tr, confidence=1.0) |
Note: to run this code the Python version should be less then 3.8
Also you can translate from one language to another language using Googletrans, this is the code for translating. you can use the translate() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from googletrans import Translator text1 = "subscribe my channel" text2 = "suscríbete a mi canal" text3 = "kanalıma abone ol" translator = Translator() print("Translated From Spanish : ", translator.translate(text2)) print("Translated From Turkish : ", translator.translate(text3)) |
Run the code this is the result.
1 2 |
Translated From Spanish : Translated(src=es, dest=en, text=subscribe to my channel, pronunciation=subscribe to my channel, extra_data="{'translat...") Translated From Turkish : Translated(src=tr, dest=en, text=subscribe to my channel, pronunciation=subscribe to my channel, extra_data="{'translat...") |
You can see that we have not specified the source and destination for the language, if you don’t specify the source, by default google translate specify English the source or destination.
Now let’s just specify the source. in this example we have translated our text from English to Spanish and Turkish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from googletrans import Translator text1 = "subscribe my channel" text2 = "suscríbete a mi canal" text3 = "kanalıma abone ol" translator = Translator() print("Translate English to ESP : ", translator.translate(text1, src='en', dest='es')) print("Translate En to Tur : ", translator.translate(text1, src='en', dest='tr')) |
So Run the code and this will be the result.
1 2 |
Translate English to ESP : Translated(src=en, dest=es, text=suscríbete a mi canal, pronunciation=subscribe my channel, extra_data="{'translat...") Translate En to Tur : Translated(src=en, dest=tr, text=kanalıma abone ol, pronunciation=subscribe my channel, extra_data="{'translat...") |
FAQs:
Q: Does Google Translate have a Python API?
A: Yes, Google Translate does not have an official Python API provided by Google. However, there are third-party Python libraries such as googletrans that offer an unofficial API for accessing the Google Translate service.
Q: Is googletrans Python free?
A: Yes, googletrans is Python library for accessing the Google Translate service, it is free and open-source. You can use it to translate text and detect languages without any cost.
Q: Is Google Translate API free?
A: Google Translate API offers both free and paid usage tiers. The free tier provides a limited number of requests per day, while the paid tier offers higher usage limits and additional features. You need to sign up for a Google Cloud Platform (GCP) account and enable the Google Translate API to access the service. Refer to the Google Cloud Platform pricing documentation for more information on usage limits and pricing.
Subscribe and Get Free Video Courses & Articles in your Email