mdbook-translator
A translation preprocessor plugin for mdBook that automatically translates Markdown documents using the DeepSeek API.
Features
- π Automatic translation of mdBook document content
- π Smart caching mechanism to avoid duplicate translations
- π― Preserves code blocks and technical terms
- π High-quality translation based on DeepSeek API
- βοΈ Configurable prompts
- π Multi-language translation support
Installation
Build from Source
# Clone the project
# Build the project
# Install to system path
Using cargo install
Configuration
1. Get DeepSeek API Key
Visit DeepSeek Official Website to get your API key and set the environment variable:
2. Configure book.toml
Add the following configuration to your mdBook project's book.toml file:
[]
= "Your Book Title"
= ["Author Name"]
[]
= ["mdbook-translator"]
= "book-zh" # Optional: specify output directory
[]
= "mdbook-translator"
= "Chinese" # Target translation language
= "Please keep technical terms like Send, Future, Futures in Rust untranslated" # Optional: custom translation prompt
Configuration Options
language: Target translation language (e.g., "Chinese", "Japanese", "Korean", etc.)prompt: Optional custom translation prompt to guide translation behaviorbuild-dir: Optional output directory, defaults to "book"
Usage
Basic Usage
# Run in your mdBook project directory
The plugin will automatically:
- Read source documents
- Call DeepSeek API for translation
- Cache translation results
- Generate translated documents
Clear Cache
If you need to retranslate, you can delete the cache file:
Debug Mode
The plugin outputs debug information to standard error output, including cache hit information.
How It Works
- Document Parsing: The plugin traverses all chapters and pages in mdBook
- Content Chunking: Splits long text into chunks suitable for API processing
- Smart Translation: Calls DeepSeek API for translation while preserving code blocks and formatting
- Caching Mechanism: Uses SHA256 hash to cache translation results, avoiding duplicate translations
- Document Reconstruction: Replaces original document content with translated content
Important Notes
- Ensure you have set the correct
DEEPSEEK_API_KEYenvironment variable - Translation process requires network connection (users in mainland China may need to configure HTTP proxy)
- First translation may take longer, subsequent builds will use cache for acceleration
- Code blocks and special formatting will be preserved and not translated
- It's recommended to backup original documents before translation
Dependencies
mdbook: mdBook core libraryreqwest: HTTP client for API callsserde_json: JSON serialization/deserializationsha2: Hash calculation for cache key generationanyhow: Error handlingclap: Command line argument parsingtoml: TOML configuration file parsing
Example Project
Refer to the async-book project configuration:
[]
= "Asynchronous Programming in Rust"
= ["Taylor Cramer", "Nicholas Cameron", "Open source contributors"]
[]
= ["mdbook-translator"]
= "book-zh"
[]
= "mdbook-translator"
= "Chinese"
= "Please keep technical terms like Send, Future, Futures in Rust untranslated"
Contributing
Welcome to submit Issues and Pull Requests!