How to use Python to call Google Translate unlimited free API interface?
2023-05-19 · 234 · 6 min
# Preface
When I was building a new version of the backend of my website, I started out using the Google Translate Paid API, but after I translated some of my articles, Google told me they were going to charge me over $200. I immediately replaced the translation service of Microsoft Azure next door. They can use 2 million characters for free every month, which is completely inexhaustible for my usage. However, the good times did not last long, and today I received an email:
We detected suspicious activity or a violation of the terms of use and therefore disabled the subscription to protect the owner of the payment method and Microsoft. If you think this is a bug, please go ahead and file a support ticket with the security team to see a decision.
Later, I thought it should be when I switched accounts, and the misoperation caused Microsoft to judge me to be a slut, so I was banned. After searching for a while, I found that there is a ready-made wheel on GitHub that can be used out of the box.
It can call Google's free API, and automatically truncate the part larger than 4500 characters, and then translate it in segments, which bypasses the limitation that the free version API only allows translation of 4500 characters at a time. The following part of this article is how to use this wheel, as well as its optimization and application.
# Install
Please use the corresponding pip installation according to your python version:
pip install googletrans
# OR
pip3 install googletrans
# use
Because I want to integrate it in my website translation module, I deleted it, and the simplified code is as follows:
import re
import sys
from deep_translator import Google Translator
def split_text(text, max_len=4500):
lines = text. split("\n")
chunks = []
chunk = ""
for line in lines:
if len(chunk + line) > max_len:
chunks.append(chunk)
chunk = ""
chunk += line + "\n"
if chunk:
chunks.append(chunk)
return chunks
def translate(text, target):
chunks = split_text(text)
translated_text = ''
for chunk in chunks:
translated = GoogleTranslator(source='auto', target=target).translate(text=chunk)
translated_text += translated + "\n"
translated_text = re.sub(r'[\n]{3,}', '\n\n', translated_text.strip())
translated_text = translated_text. strip()
return translated_text
translated = translate('Hi, I'm saltyleo, from China.','en')
print(translated)
Paste the above code directly on the server, and you can get the return after running:
Hello, I'm saltyleo, from China.
So far, you have got a python script that can translate unlimited text. If you need to translate the entire file, please move to the original author's github. The original code is translated according to the file.
If you don't know what the abbreviation of the language you want to translate to is, you can find the abbreviation corresponding to the name of the language you want to translate to in this link: googletrans.LANGUAGES
This article is written by SaltyLeo. If the content is incorrect, please leave a message for feedback. When reprinting or quoting this article, please comply with the CC BY-NC-SA agreement, must be attributed, used non-commercially and share alike!