https://github.com/simon-he95/translate
api for translating powered by bing
https://github.com/simon-he95/translate
translate translate-api
Last synced: 10 months ago
JSON representation
api for translating powered by bing
- Host: GitHub
- URL: https://github.com/simon-he95/translate
- Owner: Simon-He95
- License: mit
- Created: 2023-04-24T09:19:17.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-03T15:13:47.000Z (over 1 year ago)
- Last Synced: 2024-11-24T20:49:33.976Z (over 1 year ago)
- Topics: translate, translate-api
- Language: TypeScript
- Homepage:
- Size: 137 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: license
Awesome Lists containing this project
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
```bash
npm install @simon_he/translate
# or
pnpm add @simon_he/translate
# or
yarn add @simon_he/translate
```
## π Quick Start
```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']
```
## π§ Advanced Usage
### Individual Service Usage
```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) // 'δ½ ε₯½'
```
### Custom Cache Configuration
```typescript
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`
### Individual Services
Each service exports a `fanyi()` function:
```typescript
type TranslateFunction = (text: string, to?: string, from?: string) => Promise<{ text: string }>
```
## π§ͺ Testing
```bash
# 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:all
```
## License
[MIT](./LICENSE) License Β© 2022 [Simon He](https://github.com/Simon-He95)

