Google Translate library with support for free and API key-based translation services
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Google Translate library with support for both free and API key-based translation services. pygtrans provides comprehensive translation capabilities including language detection, text translation, batch processing, and text-to-speech functionality, supporting 242 source languages and 243 target languages.
pip install pygtransfrom pygtrans import Translate, ApiKeyTranslateCommon imports for response handling:
from pygtrans import TranslateResponse, DetectResponse, LanguageResponse, NullAccess to language constants:
from pygtrans import SOURCE_LANGUAGES, TARGET_LANGUAGESfrom pygtrans import Translate
# Configure with proxy (required for non-Chinese users)
client = Translate(proxies={'https': 'http://localhost:10809'})
# Language detection
detect_result = client.detect('Hello, world!')
print(detect_result.language) # 'en'
# Single translation
result = client.translate('Hello, world!', target='zh-CN')
print(result.translatedText) # '你好世界!'
# Batch translation
results = client.translate([
'Good morning',
'How are you?'
], target='zh-CN')
for result in results:
print(result.translatedText)
# Text-to-speech
audio_data = client.tts('你好', target='zh-CN')
with open('hello.mp3', 'wb') as f:
f.write(audio_data)from pygtrans import ApiKeyTranslate
# Configure with Google Cloud API key
client = ApiKeyTranslate(api_key='your-api-key-here')
# Get supported languages
languages = client.languages()
print(languages[0].language, languages[0].name) # 'en' 'English'
# Translation with automatic source detection
result = client.translate('Hello, world!', target='zh-CN')
print(result.translatedText) # '你好世界!'
print(result.detectedSourceLanguage) # 'en'
# Batch translation
results = client.translate([
'Good morning',
'How are you?'
], target='zh-CN')pygtrans provides two main translation approaches:
Translate): Uses Google Translate web interface, requires proxy for non-Chinese users, supports basic translation and TTSApiKeyTranslate): Uses Google Cloud Translation API, requires valid API key, provides enterprise features and reliabilityBoth clients support:
Null response objectsProvides a command-line interface for quick translation tasks via the pygtrans command.
def main():
"""
Command-line interface entry point for pygtrans.
Usage: pygtrans (currently provides basic CLI functionality)
"""Complete free translation functionality using Google Translate web interface. Includes language detection, text translation, batch processing, and text-to-speech capabilities. Requires proxy configuration for users outside China.
class Translate:
def __init__(
self,
target: str = "zh-CN",
source: str = "auto",
fmt="html",
user_agent: str = None,
domain: str = "com",
proxies: dict = None,
timeout: int = None,
trust_env=False
): ...
def detect(self, q: str, timeout=...) -> Union[DetectResponse, Null]: ...
def translate(
self,
q: Union[str, List[str]],
target: str = None,
source: str = None,
fmt: str = None,
timeout=...
) -> Union[TranslateResponse, List[TranslateResponse], Null]: ...
def tts(self, q: str, target: str = None, timeout=...) -> Union[bytes, Null]: ...Enterprise translation functionality using Google Cloud Translation API. Provides reliable translation services, batch processing, language enumeration, and language detection with proper API authentication.
class ApiKeyTranslate:
def __init__(
self,
api_key: str,
target: str = "zh-CN",
source: str = None,
fmt: str = "html",
model: str = "nmt",
proxies: dict = None,
timeout=None,
trust_env=False
): ...
def languages(
self,
target: str = None,
model: str = None,
timeout=...
) -> Union[List[LanguageResponse], Null]: ...
def detect(
self,
q: Union[str, List[str]],
timeout=...
) -> Union[DetectResponse, List[DetectResponse], Null]: ...
def translate(
self,
q: Union[str, List[str]],
target: str = None,
source: str = None,
fmt: str = None,
model: str = None,
timeout=...
) -> Union[TranslateResponse, List[TranslateResponse], Null]: ...Data structures for handling translation results, language detection outcomes, and error responses. These objects provide structured access to translation data and metadata.
class TranslateResponse:
def __init__(
self,
translatedText: str,
detectedSourceLanguage: str = None,
model: str = None
): ...
class DetectResponse:
def __init__(
self,
language: str,
isReliable: bool = True,
confidence: float = 1.0
): ...
class LanguageResponse:
def __init__(self, language: str, name: str = None): ...
class Null:
def __init__(self, response: requests.Response): ...Comprehensive language support with 242 source languages and 243 target languages. Includes predefined dictionaries for language code mapping and validation.
SOURCE_LANGUAGES: dict[str, str] = {
"auto": "检测语言",
"en": "英语",
"zh-CN": "中文",
# ... 239 more languages
}
TARGET_LANGUAGES: dict[str, str] = {
"en": "英语",
"zh-CN": "中文(简体)",
"zh-TW": "中文(繁体)",
# ... 240 more languages
}from typing import Union, List, Dict
import requests
# Union types for method returns
TranslateResult = Union[TranslateResponse, List[TranslateResponse], Null]
DetectResult = Union[DetectResponse, List[DetectResponse], Null]
LanguageResult = Union[List[LanguageResponse], Null]
TTSResult = Union[bytes, Null]
# Input types
TextInput = Union[str, List[str]]
ProxyConfig = Dict[str, str] # e.g., {'https': 'http://localhost:10809'}