@simon_he/translate
Version:
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.
136 lines (95 loc) β’ 3.86 kB
Markdown
π A fast, robust, and lightweight translation library supporting **6 translation services** with automatic fallback, batch processing, intelligent caching, and full TypeScript support.
- π **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
| 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)
```bash
npm install @simon_he/translate
pnpm add @simon_he/translate
yarn add @simon_he/translate
```
```typescript
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']
```
```typescript
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) // 'δ½ ε₯½'
```
```typescript
import translateLoader from '@simon_he/translate'
// Create translator with custom cache size
const translate = translateLoader(createLimitedCache(500))
```
Creates a translation function with automatic service fallback.
**Parameters:**
- `cacheMap` (optional): Custom cache implementation
**Returns:** `(texts: string | string[], to?: 'en' | 'zh') => Promise<string[]>`
Each service exports a `fanyi()` function:
```typescript
type TranslateFunction = (text: string, to?: string, from?: string) => Promise<{ text: string }>
```
```bash
npm test
npm run test:performance
npm run test:stress
npm run test:benchmark
npm run test:all
```
[](./LICENSE) License Β© 2022 [Simon He](https://github.com/Simon-He95)
<a href="https://github.com/Simon-He95/sponsor" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a>
<span><div align="center"></div></span>