translator¶
translator
¶
Core translation logic for the AI Translate application.
TranslationWorker
¶
Bases: QThread
Worker thread for processing translation tasks.
Thin QThread wrapper that delegates to run_translation_pipeline().
Only one instance can run at a time.
Initializes the TranslationWorker.
| PARAMETER | DESCRIPTION |
|---|---|
tasks
|
List of (h_id, storage_path, src_lang, target_lang).
TYPE:
|
config
|
Optional config snapshot; when None, from_settings() is called at the start of run().
TYPE:
|
Source code in src/core/translator.py
is_busy
classmethod
¶
stop
¶
_is_cancelled
¶
Checks if a task was paused or deleted while in progress.
Reads the current DB status; returns True if it is no longer 'Translating' (e.g. user clicked Pause).
Source code in src/core/translator.py
run
¶
Processes queued translation tasks for already cloned files.
Source code in src/core/translator.py
_resolve_output_dir
¶
Resolves the output directory for translated files.
Priority
- User-configured storage path (from config or settings).
- Original source file's parent directory (if it still exists).
- Desktop folder as a last-resort fallback.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Optional config snapshot; falls back to load_setting().
TYPE:
|
source_path
|
Original source file path before cloning.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
The resolved output directory.
TYPE:
|
Source code in src/core/translator.py
_fetch_all_glossary_entries
¶
Collects glossary entries from all active glossary sets.
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[int, str, str]]
|
list[tuple[int, str, str]]: Flat list of (id, source, target) |
list[tuple[int, str, str]]
|
tuples from every active glossary set. |
Source code in src/core/translator.py
_update_storage_path
¶
Updates the storage_path column for a history entry.
Source code in src/core/translator.py
_map_error_to_code
¶
Maps an error message string to a standardized error code.
| PARAMETER | DESCRIPTION |
|---|---|
msg
|
The exception message string.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The corresponding error code constant.
TYPE:
|
Source code in src/core/translator.py
_build_output_name
¶
Builds the translated output filename.
Format: {stem}_translated_{src_locale}_{target_locale}{suffix}
e.g. report_translated_en-US_vi.docx.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Original source file path.
TYPE:
|
src_lang
|
Source language label (e.g. "English (US)").
TYPE:
|
target_lang
|
Target language label (e.g. "Vietnamese").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The formatted output filename string. |
Source code in src/core/translator.py
_get_unique_path
¶
Returns a unique path by appending a numeric suffix if the file exists.
Source code in src/core/translator.py
_pipeline_run_ocr
¶
Runs OCR and returns results, or None on failure.
| PARAMETER | DESCRIPTION |
|---|---|
h_id
|
The history entry ID.
TYPE:
|
file_path
|
Path to the image file.
TYPE:
|
src_lang
|
Source language label for OCR language selection.
TYPE:
|
config
|
Optional config snapshot; falls back to load_setting().
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[list[OCRResult], list[OCRResult], str] | None
|
Tuple of (ocr_results, raw_ocr_results, ocr_method) or None. |
Source code in src/core/translator.py
_pipeline_run_llm
¶
_pipeline_run_llm(
h_id,
file_path,
ocr_data,
src_lang,
target_lang,
*,
provider=None,
model=None,
)
Runs LLM translation + paragraph merge, or None on failure.
| PARAMETER | DESCRIPTION |
|---|---|
h_id
|
The history entry ID.
TYPE:
|
file_path
|
Path to the image file.
TYPE:
|
ocr_data
|
(ocr_results, raw_ocr_results, ocr_method) from OCR step. |
src_lang
|
Source language.
TYPE:
|
target_lang
|
Target language.
TYPE:
|
provider
|
Optional LLM provider override.
TYPE:
|
model
|
Optional LLM model override.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[list[OCRResult], list[str], list[OCRResult]] | None
|
Tuple of (ocr_results, translations, raw_fragments) or None. |
Source code in src/core/translator.py
_pipeline_process_image
¶
_pipeline_process_image(
h_id,
file_path,
src_lang,
target_lang,
config=None,
cancel_check=None,
*,
source_path=None,
)
Runs the full image translation pipeline: OCR → LLM → merge → render.
Checks for checkpoints from a previous run so expensive stages (OCR, LLM) can be skipped on resume. Checks for cancellation (e.g. user pause) between each stage.
| PARAMETER | DESCRIPTION |
|---|---|
h_id
|
The history entry ID.
TYPE:
|
file_path
|
Path to the image file.
TYPE:
|
src_lang
|
Source language.
TYPE:
|
target_lang
|
Target language.
TYPE:
|
config
|
Optional config snapshot; falls back to load_setting().
TYPE:
|
cancel_check
|
Callable taking h_id, returns True if cancelled.
TYPE:
|
source_path
|
Original source file path for output directory fallback.
TYPE:
|
Source code in src/core/translator.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
_pipeline_process_text
¶
_pipeline_process_text(
h_id,
file_path,
src_lang,
target_lang,
config=None,
cancel_check=None,
*,
source_path=None,
)
Runs the full text file translation pipeline: read → LLM → write.
Computes the output path, fetches glossary entries, and delegates to the text_processor module for format-specific handling. Passes the storage directory for per-chunk/batch checkpointing.
| PARAMETER | DESCRIPTION |
|---|---|
h_id
|
The history entry ID.
TYPE:
|
file_path
|
Path to the cloned text file.
TYPE:
|
src_lang
|
Source language.
TYPE:
|
target_lang
|
Target language.
TYPE:
|
config
|
Optional config snapshot; falls back to load_setting().
TYPE:
|
cancel_check
|
Callable taking h_id, returns True if cancelled.
TYPE:
|
source_path
|
Original source file path for output directory fallback.
TYPE:
|
Source code in src/core/translator.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
_pipeline_finalize
¶
Finalizes a translation task based on user settings.
| PARAMETER | DESCRIPTION |
|---|---|
h_id
|
The history entry ID.
TYPE:
|
config
|
Optional config snapshot; falls back to load_setting().
TYPE:
|
Source code in src/core/translator.py
run_translation_pipeline
¶
run_translation_pipeline(
config,
is_cancelled=None,
task_cancelled=None,
task_ids=None,
model_setting_key="",
)
Pure-Python translation loop. No PySide6 dependency.
Fetches pending tasks from DB, processes them sequentially. Suitable for CLI, MCP server, REST API, or any headless caller.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Frozen snapshot of translation settings.
TYPE:
|
is_cancelled
|
Returns True when the overall pipeline should stop.
TYPE:
|
task_cancelled
|
Returns True when a specific task (by h_id) is cancelled (e.g. user paused it).
TYPE:
|
task_ids
|
Optional set of history IDs this invocation owns. When provided, the loop ignores unrelated pending work.
TYPE:
|
model_setting_key
|
Optional per-feature LLM model setting key. When provided, the loop re-reads it before each task so a mid-run model change (e.g. user picks a new model in the Re-translate dialog) takes effect on the next queued task without waiting for the current worker to exit.
TYPE:
|
Source code in src/core/translator.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | |
setup_translation_tasks
¶
Creates DB entries and clones files to storage for a set of translation tasks.
| PARAMETER | DESCRIPTION |
|---|---|
file_paths
|
List of absolute paths to files to translate.
TYPE:
|
src_lang
|
Source language name.
TYPE:
|
target_lang
|
Target language name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
List of (history_id, storage_path, src_lang, target_lang) tuples.
TYPE:
|
Source code in src/core/translator.py
resume_unfinished_translations
¶
Resumes unfinished translation tasks from the database.
| PARAMETER | DESCRIPTION |
|---|---|
statuses
|
Tuple of status strings to filter unfinished tasks.
TYPE:
|
config
|
Optional config snapshot; forwarded to TranslationWorker.
TYPE:
|
Source code in src/core/translator.py
get_available_languages
¶
Returns a list of supported languages for translation.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List[str]: A list of language names. |