UNPKG

@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
# @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<string[]>` ### 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) <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">![sponsors](https://www.hejian.club/images/sponsors.jpg)</div></span>