i18n-copilot
v0.1.0
Published
A Node.js utility that watches translation files for key changes and automatically translates them using configurable LLM providers
Maintainers
Readme
i18n-copilot
AI-powered translation file watcher and translator that automatically translates new keys from your base language to target languages using configurable LLM providers.
🏗️ Architecture
The project has been refactored to separate concerns and improve maintainability:
Core Components
AutoTranslator- Main business logic orchestrator that manages the entire translation workflowTranslationWatcher- File system watcher that detects changes in translation filesTranslationOrchestrator- Handles the translation process and batchingTranslationParser- Parses translation files and extracts keysTranslationDiffDetector- Detects differences between base and target language files
Providers
OpenAIProvider- Uses OpenAI's GPT models for translationAnthropicProvider- Uses Anthropic's Claude models for translationLocalProvider- Uses local models (e.g., Ollama) for translation
CLI
TranslationCLI- Command-line interface that uses the AutoTranslator- Clean separation of concerns - CLI only handles user interaction and configuration
🚀 Features
- Automatic Translation: Watches for changes in base language files and automatically translates new keys
- Multiple Providers: Support for OpenAI, Anthropic, and local LLM providers
- Smart Batching: Efficiently processes translations in batches
- File Preservation: Maintains existing file structure and formatting
- Real-time Watching: Monitors file changes and triggers translations automatically
- Manual Translation: Command to manually translate specific files
- Comprehensive Testing: Full test coverage for all business logic
📦 Installation
# Install dependencies
pnpm install
# Build the project
pnpm run build
# Run tests
pnpm test🛠️ Build System
This project uses esbuild for fast builds and SWC for Jest testing:
Build Commands
# Build the project (library + CLI)
pnpm run build
# Watch mode for development
pnpm run dev
# Clean build artifacts
pnpm run cleanTesting
# Run tests once
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run test runner (requires build first)
pnpm run test:runnerCLI Usage
# Run CLI with help
pnpm run cli --help
# Start the translation watcher
pnpm start
# Or run directly
./dist/cli/cli.js --helpBuild Output
dist/index.js- Main library (ES modules)dist/cli/index.js- CLI implementation (ES modules)dist/cli/cli.js- CLI executable wrapper (ES modules with shebang)
⚙️ Configuration
Create a i18n-copilot.config.json file:
{
"watchPath": "./locales",
"baseLanguage": "en",
"targetLanguages": ["fr", "de", "es"],
"filePattern": ".*\\.json$",
"provider": {
"type": "openai",
"config": {
"apiKey": "your-openai-api-key",
"model": "gpt-3.5-turbo"
}
},
"preserveFormatting": true,
"contextInjection": true,
"batchSize": 10,
"retryAttempts": 3,
"logLevel": "info"
}🎯 Usage
Watch Mode (Automatic Translation)
# Start watching for changes
i18n-copilot watch
# With custom config
i18n-copilot watch -c ./my-config.json
# With CLI options
i18n-copilot watch -p ./locales -b en -t fr,de,es --provider openaiManual Translation
# Translate a specific file
i18n-copilot translate ./locales/en.json
# With custom config
i18n-copilot translate ./locales/en.json -c ./my-config.jsonOther Commands
# Validate configuration
i18n-copilot validate -c ./i18n-copilot.config.json
# Show status
i18n-copilot status
# Stop watcher
i18n-copilot stop
# Initialize new project
i18n-copilot init -p ./my-project --base en --targets fr,de,es🔧 Development
Project Structure
src/
├── core/ # Core business logic
│ ├── auto-translator.ts # Main orchestrator
│ ├── watcher.ts # File watching
│ ├── translator.ts # Translation orchestration
│ ├── parser.ts # File parsing
│ ├── diff-detector.ts # Difference detection
│ └── __tests__/ # Core tests
├── providers/ # LLM providers
│ ├── base-provider.ts # Base provider interface
│ ├── openai.ts # OpenAI provider
│ ├── anthropic.ts # Anthropic provider
│ ├── local.ts # Local provider
│ ├── mock-provider.ts # Mock provider for testing
│ └── __tests__/ # Provider tests
├── utils/ # Utilities
│ ├── config-validator.ts # Configuration validation
│ └── logger.ts # Logging utilities
├── cli/ # Command-line interface
│ └── index.ts # CLI implementation
└── index.ts # Main exportsRunning Tests
# Run all tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverage
# Run specific test file
pnpm test src/core/__tests__/auto-translator.test.tsTesting Architecture
- MockProvider: Simulates LLM responses for testing without API calls
- Comprehensive Coverage: Tests cover all business logic, error handling, and edge cases
- Isolated Testing: Each component can be tested independently
- Mock Dependencies: External dependencies are properly mocked
🔄 How It Works
- File Watching: The
TranslationWatchermonitors the configured directory for changes - Change Detection: When a base language file changes, it triggers the translation process
- Diff Analysis: The
TranslationDiffDetectoridentifies new or modified keys - Translation: The
TranslationOrchestratorsends keys to the configured LLM provider - File Updates: Translated content is written back to target language files
- Event Emission: Progress and completion events are emitted for monitoring
🧪 Testing
The project includes comprehensive tests for all business logic:
- AutoTranslator Tests: Core functionality, error handling, event emission
- MockProvider Tests: Provider behavior, error simulation, concurrent operations
- Integration Tests: End-to-end workflow testing
- Edge Case Tests: Error conditions, invalid inputs, boundary conditions
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
📝 License
MIT License - see LICENSE file for details.
🆘 Support
For issues and questions:
- Check the existing issues
- Create a new issue with detailed information
- Include configuration and error logs
- Provide reproduction steps
