pygtrans
pygtrans: python google translate
Google Translate, support APIKEY
- github
- pypi
- The document
The basic function
- Gets a list of language support
- Automatic detection language, support batch
- Text /HTML translation, batch support
- Support the TTS
The installation
Environment requirement: >= Python 3.6
pip install pygtrans
Copy the code
or
pip install pygtrans -i https://pypi.org/simple
Copy the code
Quick start
from pygtrans import Translate
client = Translate()
text = client.translate('Google Translate')
print(text.translatedText) # Google Translate
Copy the code
Basic introduction
There are two modules to be concerned with in the PyGtrans package
TranslateThrough:Google translationAPI interface implementation, which can be used directly, but can be unstableApiKeyTranslateThrough:Google Cloud Translate APIsImplementation needs to provide a validAPIKEY.Google offers a free trial
Differences between the two
| disadvantages | advantages | |
|---|---|---|
| Translate | Stability cannot be guaranteed | Free and direct to use This product can be translated at one time100000A sentence |
| ApiKeyTranslate | Need to bemoneyTranslate content at most once102400 bytes One time maximum translation128A sentence |
Relatively stable |
- Translate does not have any restrictions, if you use the process of the problem, please leave a message
- ApiKeyTranslateThe official limitations of the code have been fault-tolerant, with the only caveat:No more than a single sentence 102400 bytes
aboutNullThe module
If you want to determine whether the translation is successful, check whether the return object is Null
from pygtrans import Translate, Null
client = Translate()
text = client.translate('Hello')
if isinstance(text, Null):
print("Translation failure")
else:
print("Successful translation")
Copy the code
It is recommended to judge on demand and use directly
The basic use
useTranslate
- Get supported languages: This feature is removed from the code and provided as a list of languages supported
- Language testingMethod:
detectBatch detection is not supported. If you need batch detection, use this commandtranslate_and_detectmethods
from pygtrans import Translate
client = Translate()
d = client.detect('hello')
assert d.language == 'zh-CN'
Copy the code
- Text translationUse:
translateMethod, the default isHTMLMode translation. For detailed parameter Settings, go toPygtrans document
from pygtrans import Translate
client = Translate()
text1 = client.translate('English')
assert text1.translatedText == 'English'
text2 = client.translate('like', target='en')
assert text2.translatedText == 'love'
# Batch translation
texts = client.translate(['Hello'.'World'])
for text in texts:
print(text.translatedText)
# how do you do
# the world
Copy the code
To change the default language, go to the PyGtrans document
- TTS: From text to language
from pygtrans import Translate
client = Translate()
tts = client.tts('hello')
open('hello, mp3'.'wb').write(tts)
tts = client.tts('や meit ', target='ja')
open('や meit.mp3 '.'wb').write(tts)
tts = client.tts('One, two, three, four, five, go up to the tiger.')
open('One two three four five. Mp3'.'wb').write(tts)
tts = client.tts(""" My little fish you wake up, still know the morning? Last night you said, may the night never fall. Your sweet cheeks gently slide, is your tears, or my tears? The first kiss kiss goodbye that season, not already cried? My fingertips still remember, your panic heartbeat. In the gentle body fragrance, that long hair fluttering. "" ")
open('My little fish you wake up. Mp3'.'wb').write(tts)
Copy the code
useApiKeyTranslate
Refer to the PyGtrans documentation