Skip to main content

High-performance Chinese to English translation tool with multi-threading and batch processing

Project description

Enhanced Chinese Translator

PyPI version Python versions License

🚀 A high-performance Chinese to English translation tool with multi-threading, batch processing, and advanced optimizations for code files, documentation, and text content.

Features

Multi-threaded Processing: Concurrent translation with configurable worker threads
🚀 Batch Translation: Process multiple texts simultaneously for improved efficiency
💾 Smart Caching: Thread-safe translation cache to avoid duplicate API calls
🎯 Context-Aware: Handles strings, comments, and code contexts intelligently
Rate Limiting: Built-in API rate limiting to prevent service throttling
📁 Directory Processing: Batch process entire directories with file filtering
🔄 Multiple Services: Support for Google Translate, Baidu, and other services
📊 Performance Stats: Detailed statistics and progress tracking
🛡️ Backup Support: Automatic backup creation before translation
🎨 Smart Text Processing: Preserves programming terms and handles mixed content

Installation

From PyPI (Recommended)

pip install enhanced-chinese-translator

From Source

git clone https://github.com/enhanced-translator/enhanced-chinese-translator.git
cd enhanced-chinese-translator
pip install -e .

Quick Start

Command Line Usage

# Translate a single file
enhanced-chinese-translator my_file.py

# Use short command alias
ect my_file.py

# Translate entire directory
ect /path/to/project --patterns "*.py" "*.dart" "*.js"

# Advanced usage with custom settings
ect /path/to/project \
    --workers 10 \
    --batch-size 20 \
    --rate-limit 0.02 \
    --output /path/to/translated \
    --no-backup

Python API Usage

from enhanced_chinese_translator import EnhancedChineseTranslator

# Initialize translator
translator = EnhancedChineseTranslator(
    translation_service='google',
    max_workers=5,
    batch_size=10,
    rate_limit=0.05
)

# Translate a single file
success = translator.translate_file('my_file.py')

# Translate multiple files in directory
count = translator.translate_directory(
    '/path/to/project',
    file_patterns=['*.py', '*.dart'],
    backup=True
)

# Batch translate texts
texts = ["你好世界", "这是一个测试", "中文翻译"]
translations = translator.translate_texts_batch(texts)

# Print performance statistics
translator.print_performance_stats()

Command Line Options

Option Description Default
path File or directory path to translate .
-o, --output Output file/directory path In-place
-s, --service Translation service (google, baidu) google
--patterns File patterns to match All text files
--no-backup Skip creating backup files False
--workers Maximum number of worker threads 5
--batch-size Batch size for translations 10
--rate-limit Rate limit between API calls (seconds) 0.05

Examples

Basic File Translation

# Translate a Python file
ect my_script.py

# Translate with output to different file
ect input.py -o translated.py

Directory Translation

# Translate all Python files in current directory
ect . --patterns "*.py"

# Translate Flutter project files
ect ./lib --patterns "*.dart" --workers 8

# Translate with custom output directory
ect ./src --patterns "*.js" "*.ts" -o ./translated_src

Advanced Usage

# High-performance batch translation
ect ./project \
    --patterns "*.py" "*.dart" "*.js" \
    --workers 15 \
    --batch-size 25 \
    --rate-limit 0.01 \
    --output ./translated_project

# Translate without creating backups
ect ./docs --patterns "*.md" --no-backup

Python API Examples

Basic Translation

from enhanced_chinese_translator import EnhancedChineseTranslator

translator = EnhancedChineseTranslator()

# Translate single text
result = translator.translate_texts_batch(["你好,世界!"])
print(result[0])  # "Hello, world!"

Batch Processing

# Process multiple texts efficiently
chinese_texts = [
    "这是第一个测试文本",
    "这是第二个测试文本", 
    "这是第三个测试文本"
]

translations = translator.translate_texts_batch(chinese_texts)
for original, translated in zip(chinese_texts, translations):
    print(f"{original} -> {translated}")

Directory Processing

# Translate entire project
translator = EnhancedChineseTranslator(
    max_workers=10,
    batch_size=20
)

success_count = translator.translate_directory(
    './my_project',
    file_patterns=['*.py', '*.dart', '*.js'],
    output_dir='./translated_project',
    backup=True
)

print(f"Successfully translated {success_count} files")

Supported File Types

The translator automatically detects and processes various file types:

  • Programming Languages: .py, .dart, .js, .ts, .java, .cpp, .c, .h
  • Web Technologies: .html, .css, .scss, .vue, .jsx, .tsx
  • Documentation: .md, .txt, .rst, .xml, .json, .yaml
  • Configuration: .conf, .ini, .cfg, .properties

Translation Context Handling

The tool intelligently handles different contexts:

String Literals

# Before
message = "用户登录失败"

# After  
message = "User login failed"

Comments

# Before
# 这是一个重要的函数
def important_function():
    pass

# After
# This is an important function  
def important_function():
    pass

Mixed Content

// Before
/// 获取用户信息的API接口
Future<UserInfo> getUserInfo(String userId) {
  // 发送HTTP请求
  return http.get('/api/user/$userId');
}

// After
/// API interface for getting user information
Future<UserInfo> getUserInfo(String userId) {
  // Send HTTP request
  return http.get('/api/user/$userId');
}

Performance Features

Multi-threading

  • Concurrent translation processing
  • Configurable worker thread count
  • Thread-safe caching and rate limiting

Batch Processing

  • Group multiple texts for efficient API usage
  • Reduces API call overhead
  • Optimized for large-scale translations

Smart Caching

  • Persistent translation cache
  • Avoids duplicate API calls
  • Thread-safe cache operations

Rate Limiting

  • Prevents API service throttling
  • Configurable delay between requests
  • Per-thread rate limiting

Configuration

Environment Variables

# Set default translation service
export ECT_SERVICE=google

# Set default worker count
export ECT_WORKERS=8

# Set cache directory
export ECT_CACHE_DIR=/path/to/cache

Configuration File

Create ~/.ect_config.json:

{
  "translation_service": "google",
  "max_workers": 8,
  "batch_size": 15,
  "rate_limit": 0.03,
  "backup": true,
  "default_patterns": ["*.py", "*.dart", "*.js"]
}

Performance Statistics

The tool provides detailed performance metrics:

📊 Performance Statistics:
  ├─ Total translations: 1,247
  ├─ Cache hit rate: 23.4%
  ├─ API calls made: 89
  ├─ Failed translations: 0 (0.0%)
  ├─ Total processing time: 45.67s
  └─ Average time per translation: 36.6ms

Troubleshooting

Common Issues

API Rate Limiting

# Increase rate limit delay
ect myfile.py --rate-limit 0.1

Large File Processing

# Reduce batch size and workers
ect large_project/ --workers 3 --batch-size 5

Memory Usage

# Process files one at a time
ect project/ --workers 1 --batch-size 1

Error Handling

The translator includes robust error handling:

  • Network timeout recovery
  • API service fallbacks
  • Partial translation recovery
  • Progress preservation

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

git clone https://github.com/enhanced-translator/enhanced-chinese-translator.git
cd enhanced-chinese-translator
pip install -e ".[dev]"

Running Tests

pytest tests/

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

Version 1.0.0

  • Initial release
  • Multi-threaded translation processing
  • Batch translation support
  • Smart caching system
  • Rate limiting
  • Directory processing
  • Performance statistics
  • Multiple translation services

Support

Acknowledgments

  • Thanks to all contributors who helped improve this tool
  • Inspired by the need for efficient code translation workflows
  • Special thanks to the open-source translation service providers

Made with ❤️ by the Enhanced Chinese Translator Team

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

enhanced_chinese_translator-1.0.1.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enhanced_chinese_translator-1.0.1-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file enhanced_chinese_translator-1.0.1.tar.gz.

File metadata

File hashes

Hashes for enhanced_chinese_translator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f668f0da7f4a646aeaf60f228aab66466cc47f8369589c30ecd25267e88932cf
MD5 4879a9c26e58ded1b157380c15a48db3
BLAKE2b-256 4db2ad24a9e03fd91ef48130d7e855e0cede7e828bb7f27b163c8b17b4c6976d

See more details on using hashes here.

File details

Details for the file enhanced_chinese_translator-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for enhanced_chinese_translator-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b04a14117ab110eec500be568d4db899044616d43b78d8f9f2b8f08a84eeecbf
MD5 b55ffebb6016be954bbc70289fd027bc
BLAKE2b-256 c51f29569ac079b581f726fefe9e2ce041769175cabbfb4af430648a8bf3d13c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page