Pygtrans: A Simple Way to Use Google Translate with API Key
Pygtrans is a lightweight library for Python that connects easily to Google Translate, supporting translations with an API key. This library offers users a streamlined way to translate text between various languages, integrating smoothly with Python projects.
How to Install
Installing Pygtrans is straightforward and can be done through the Python Package Index (PyPI). Users are encouraged to run the preferred installation command as follows:
# Recommended installation
pip install -U pygtrans
Additionally, users can opt to install directly from the GitHub repository if they prefer:
# Optional installation methods
pip install git+ssh://[email protected]/foyoux/pygtrans.git
pip install git+https://github.com/foyoux/pygtrans.git
Quick Start Guide
To begin using Pygtrans, one needs to import the Translate
class from the library. Example use cases include detecting the language of a given text, translating sentences into various languages, and converting text to speech. Here are some simple examples:
-
Language Detection:
from pygtrans import Translate client = Translate(proxies={'https': 'http://localhost:10809'}) text = client.detect('Answer the question.') assert text.language == 'en'
-
Sentence Translation:
text = client.translate('Look at these pictures and answer the questions.') assert text.translatedText == '看这些图片,回答问题。'
-
Batch Translation:
texts = client.translate([ 'Good morning. What can I do for you?', 'Read aloud and underline the sentences about booking a flight.', 'May I have your name and telephone number?' ]) assert [text.translatedText for text in texts] == [ '早上好。我能为你做什么?', '大声朗读并在有关预订航班的句子下划线。', '可以给我你的名字和电话号码吗?' ]
-
Translation to Specific Languages:
-
To Japanese:
text = client.translate('请多多指教', target='ja') assert text.translatedText == 'お知らせ下さい'
-
To Korean:
text = client.translate('请多多指教', target='ko') assert text.translatedText == '조언 부탁드립니다'
-
-
Text-to-Speech Conversion:
tts = client.tts('やめて', target='ja') open('やめて.mp3', 'wb').write(tts)
Supported Languages
Pygtrans supports a wide array of languages for both source and target translations. There are 242 source languages and 243 target languages available. A user-friendly dropdown list is available in the documentation for an easy lookup of language codes and names.
This extensive range of supported languages ensures that Pygtrans can accommodate diverse translation needs, making it a versatile tool for developers working in multilingual contexts.
In summary, Pygtrans presents a simplified interface for Google Translate, allowing developers to integrate language translation capabilities into their Python applications efficiently. Its lightweight design and extensive language support make it an attractive option for projects requiring multilingual support.