Custom Translators
BallonTranslator makes it easy to add custom translation services by implementing a simple interface. This guide shows you how to create your own translator module.Overview
All translators in BallonTranslator inherit from theBaseTranslator class and implement two key methods:
_setup_translator()- Initialize the translator and define language mappings_translate()- Perform the actual translation
BaseTranslator Interface
Here’s the core interface frommodules/translators/base.py:
Key Attributes
| Attribute | Type | Description |
|---|---|---|
lang_map | Dict | Maps display language names to translator-specific codes |
lang_source | str | Current source language |
lang_target | str | Current target language |
concate_text | bool | Whether to concatenate text list into single string |
cht_require_convert | bool | Enable Traditional Chinese via conversion from Simplified |
textblk_break | str | Separator for concatenated text blocks (default: \n##\n) |
params | Dict | Custom parameters (displayed in settings panel) |
name | str | Translator name (auto-populated from registry) |
Creating a Custom Translator
Step 1: Create the Translator File
Create a new file inmodules/translators/, for example trans_mycustom.py:
Step 2: Register the Translator
The@register_translator('MyCustom') decorator automatically registers your translator. The name you provide will appear in the UI.
Step 3: Import in __init__.py
Add your translator to modules/translators/__init__.py:
Step 4: Test Your Translator
Launch BallonTranslator and your translator should appear in the settings panel:- Open Settings → Module
- Select “MyCustom” from the Translator dropdown
- Configure parameters (API key, etc.)
- Test translation
Real-World Example: Google Translator
Here’s a simplified version of the Google Translator from the source code:Advanced Features
Text Concatenation
Some APIs work better with batch requests. Enable concatenation:self.textblk_break.
Custom Text Separator
If the default separator (\n##\n) doesn’t work for your API:
Pre/Post Processing Hooks
Add custom processing before or after translation:Traditional Chinese Support
If your API doesn’t natively support Traditional Chinese but supports Simplified:Custom Parameters
Define parameters that appear in the settings UI:Rate Limiting
Implement delays between API calls:Error Handling
Provide meaningful error messages:Language Mapping
Thelang_map dictionary maps BallonTranslator’s standard language names to your API’s language codes.
Standard Language Names
Testing Your Translator
Unit Test
Createtest_mycustom.py:
Manual Testing
- Launch BallonTranslator
- Open Settings → Module
- Select your translator
- Configure parameters
- Open a test project
- Run translation
- Check results
Best Practices
- Handle empty strings: Always check for empty input and return empty strings
- Error resilience: Return empty string on API errors, don’t crash
- Logging: Use
LOGGER.error()for errors,LOGGER.info()for status - Rate limiting: Respect API rate limits using the
delayparameter - Language validation: Only populate
lang_mapfor supported languages - Parameter validation: Check required parameters in
_setup_translator() - Timeout handling: Use reasonable timeouts for API calls
- Retry logic: Implement retries for transient failures
- Encoding: Handle Unicode properly, especially for CJK languages
- Documentation: Add a description in
params['description']
Examples from BallonTranslator
DeepL Translator
Sakura LLM Translator
OpenAI-Compatible Translator
Troubleshooting
Translator Not Appearing in UI
- Ensure you used
@register_translator()decorator - Check that the file is imported in
__init__.py - Look for Python syntax errors in your file
Language Not Available
- Verify the language is in
lang_mapwith a non-empty value - Check that the language name matches exactly (including Unicode characters)
Translation Returns Empty
- Check API response format
- Verify API credentials
- Look for errors in logs (
--debugmode) - Test API directly with curl/Postman
Next Steps
- Review Configuration for translator parameters
- Use Headless Mode to test batch translation
- Check GPU Acceleration for LLM-based translators
