or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

api-client.mdfree-client.mdindex.mdlanguages.mdresponses.md
tile.json

tessl/pypi-pygtrans

Google Translate library with support for free and API key-based translation services

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pygtrans@0.0.x

To install, run

npx @tessl/cli install tessl/pypi-pygtrans@0.0.0

index.mddocs/

pygtrans

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.

Package Information

  • Package Name: pygtrans
  • Language: Python
  • Installation: pip install pygtrans
  • Version: 0.0.1
  • License: GPL-3.0

Core Imports

from pygtrans import Translate, ApiKeyTranslate

Common imports for response handling:

from pygtrans import TranslateResponse, DetectResponse, LanguageResponse, Null

Access to language constants:

from pygtrans import SOURCE_LANGUAGES, TARGET_LANGUAGES

Basic Usage

Free Translation Client (requires proxy)

from 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)

API Key Client (paid service)

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')

Architecture

pygtrans provides two main translation approaches:

  • Free Translation (Translate): Uses Google Translate web interface, requires proxy for non-Chinese users, supports basic translation and TTS
  • API Key Translation (ApiKeyTranslate): Uses Google Cloud Translation API, requires valid API key, provides enterprise features and reliability

Both clients support:

  • Single string and batch list processing
  • Automatic language detection
  • Custom source/target language specification
  • Configurable timeouts and proxy settings
  • Error handling through Null response objects

Capabilities

Command Line Interface

Provides 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)
    """

Free Translation Client

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]: ...

Free Translation Client

API Key Translation Client

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]: ...

API Key Translation Client

Response Objects

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): ...

Response Objects

Language Support

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
}

Language Support

Types

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'}