@simon_he/translate
v0.0.13
Published
A fast, robust, and lightweight translation library supporting 5 translation services (Google, Bing, DeepL, MyMemory, Lingva) with automatic fallback, batch processing, caching, and full TypeScript support.
Maintainers
Keywords
Readme
@simon_he/translate
🚀 A fast, robust, and lightweight translation library supporting 6 translation services with automatic fallback, batch processing, intelligent caching, and full TypeScript support.
✨ Features
- 🌐 Multiple Translation Providers: Google, Bing, DeepL, MyMemory, Lingva
- 🔄 Automatic Fallback: If one service fails, automatically tries others
- ⚡ Batch Processing: Translate multiple texts concurrently
- 🧠 Smart Caching: LRU cache to avoid repeated API calls
- 🎯 Promise.any Racing: Uses the fastest available service
- 📦 Zero Dependencies: Lightweight and fast
- 🔒 TypeScript Support: Full type safety
- 🆓 Free Options: Includes free translation services
🌍 Supported Translation Services
| Service | Type | Status | Free Tier | API Key Required | |---------|------|--------|-----------|------------------| | MyMemory | Free | ✅ Working | 1000 requests/day | No | | Bing Translator | Commercial | ✅ Working | 2M chars/month | No* | | Google Translate | Commercial | ⚠️ Limited | 500K chars/month | No* | | DeepL | Commercial | ⚠️ Rate Limited | 500K chars/month | No* | | Lingva | Free Proxy | ⚠️ Blocked | Unlimited | No |
* These services use unofficial APIs and may have limitations or availability issues
✅ Recommended for Production: MyMemory, Bing ⚠️ Use with Caution: Google (timeouts), DeepL (rate limits), Lingva (blocked)
📦 Installation
npm install @simon_he/translate
# or
pnpm add @simon_he/translate
# or
yarn add @simon_he/translate🚀 Quick Start
import translateLoader from '@simon_he/translate'
const translate = translateLoader()
// Translate single text
const result = await translate('Hello world', 'zh')
console.log(result) // ['世界您好']
// Translate multiple texts (batch processing)
const results = await translate(['Hello', 'Good morning'], 'zh')
console.log(results) // ['你好', '早上好']
// English to Chinese (default)
const zhResult = await translate('Hello')
console.log(zhResult) // ['你好']
// Chinese to English
const enResult = await translate('你好', 'en')
console.log(enResult) // ['Hello']🔧 Advanced Usage
Individual Service Usage
import {
bingTranslate,
googleTranslate,
lingvaTranslate,
mymemoryTranslate
} from '@simon_he/translate'
// Use specific service
const google = googleTranslate()
const result = await google('Hello', 'zh')
console.log(result.text) // '你好'Custom Cache Configuration
import translateLoader from '@simon_he/translate'
// Create translator with custom cache size
const translate = translateLoader(createLimitedCache(500))🎯 API Reference
translateLoader(cacheMap?)
Creates a translation function with automatic service fallback.
Parameters:
cacheMap(optional): Custom cache implementation
Returns: (texts: string | string[], to?: 'en' | 'zh') => Promise<string[]>
Individual Services
Each service exports a fanyi() function:
type TranslateFunction = (text: string, to?: string, from?: string) => Promise<{ text: string }>🧪 Testing
# Run main tests
npm test
# Run performance tests
npm run test:performance
# Run stress tests
npm run test:stress
# Run benchmark tests
npm run test:benchmark
# Run all tests
npm run test:allLicense

