Async Translation API
Note
This documentation may contain AI-generated content. While we strive for accuracy, there might be inaccuracies. Please report any issues via:
- GitHub Issues
- Community contribution (PRs welcome!)
Overview
The yadt.high_level.async_translate function provides an asynchronous interface for translating PDF files with real-time progress reporting. This function yields progress events that can be used to update progress bars or other UI elements.
Usage
Event Types
The function yields different types of events during the translation process:
1. Progress Start Event
Emitted when a translation stage begins:
{
"type": "progress_start",
"stage": str, # Name of the current stage
"stage_progress": float, # Always 0.0
"stage_current": int, # Current progress count (0)
"stage_total": int # Total items to process in this stage
}
2. Progress Update Event
Emitted periodically during translation (controlled by report_interval, default 0.1s):
{
"type": "progress_update",
"stage": str, # Name of the current stage
"stage_progress": float, # Progress percentage of current stage (0-100)
"stage_current": int, # Current items processed in this stage
"stage_total": int, # Total items to process in this stage
"overall_progress": float # Overall translation progress (0-100)
}
3. Progress End Event
Emitted when a stage completes:
{
"type": "progress_end",
"stage": str, # Name of the completed stage
"stage_progress": float, # Always 100.0
"stage_current": int, # Equal to stage_total
"stage_total": int, # Total items processed in this stage
"overall_progress": float # Overall translation progress (0-100)
}
4. Finish Event
Emitted when translation completes successfully:
{
"type": "finish",
"translate_result": TranslateResult # Contains paths to translated files and timing info
}
5. Error Event
Emitted if an error occurs during translation:
Translation Stages
The translation process goes through the following stages in order:
- ILCreater
- LayoutParser
- ParagraphFinder
- StylesAndFormulas
- ILTranslator
- Typesetting
- FontMapper
- PDFCreater
Each stage will emit its own set of progress events.
Cancellation
The translation process can be cancelled in several ways:
- By raising a
CancelledError(e.g., when usingasyncio.Task.cancel()) - Through
KeyboardInterrupt(e.g., when user presses Ctrl+C) - By calling
translation_config.cancel_translation()method
Example of programmatic cancellation:
When cancelled: - The function will log the cancellation reason - All resources will be cleaned up properly - Any ongoing translation tasks will be stopped - A final error event with CancelledError will be emitted - The function will exit gracefully
Error Handling
Any errors during translation will be: 1. Logged with full traceback (if debug mode is enabled) 2. Reported through an error event 3. Cause the event stream to stop after the error event 4. Clean up resources properly before exiting
It's recommended to handle these events appropriately in your application to provide feedback to users. The example in the Usage section shows a basic error handling pattern.