Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

15.4 KB
Raw Blame
#!/usr/bin/env python3
"""
Bulk Auto-Translate All Languages
Automatically translates all languages in parallel using OpenAI API.
Supports concurrent translation with configurable thread pool.

Modes:
  missing  keys present in en-US but absent from the language (default)
  changed  keys whose en-US text changed after the language last translated them (git history)
  all      both of the above
"""

import argparse
import json
import os
import shutil
import subprocess
import sys
import tempfile
import threading
import time
import tomllib
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path

from stale_translations import find_stale, git

# Thread-safe print lock
print_lock = threading.Lock()


def safe_print(*args, **kwargs):
    """Thread-safe print function."""
    with print_lock:
        print(*args, **kwargs)


def get_all_languages(locales_dir: Path) -> list[str]:
    """Get all language codes from locales directory."""
    languages = []

    if not locales_dir.exists():
        print(f"Error: Locales directory not found: {locales_dir}")
        return []

    for lang_dir in sorted(locales_dir.iterdir()):
        # en-GB is derived from en-US by scripts/sync_en_us_spelling.py, not translated
        if lang_dir.is_dir() and lang_dir.name not in ("en-US", "en-GB"):
            toml_file = lang_dir / "translation.toml"
            if toml_file.exists():
                languages.append(lang_dir.name)

    return languages


def get_language_completion(locales_dir: Path, language: str) -> float | None:
    """Get completion percentage for a language."""
    lang_dir = locales_dir / language
    toml_file = lang_dir / "translation.toml"

    if not toml_file.exists():
        return None

    try:
        with open(toml_file, "rb") as f:
            target_data = tomllib.load(f)

        # Load en-US reference
        en_us_file = locales_dir / "en-US" / "translation.toml"
        with open(en_us_file, "rb") as f:
            en_us_data = tomllib.load(f)

        # Flatten and count
        def flatten(d, parent=""):
            items = {}
            for k, v in d.items():
                key = f"{parent}.{k}" if parent else k
                if isinstance(v, dict):
                    items.update(flatten(v, key))
                else:
                    items[key] = v
            return items

        en_us_flat = flatten(en_us_data)
        target_flat = flatten(target_data)

        # Count translated (not equal to en-US)
        translated = sum(1 for k in en_us_flat if k in target_flat and target_flat[k] != en_us_flat[k])
        total = len(en_us_flat)

        return (translated / total * 100) if total > 0 else 0.0

    except Exception as e:
        print(f"Warning: Could not calculate completion for {language}: {e}")
        return None


def find_error_line(output: str) -> str:
    lines = [line.strip() for line in output.splitlines() if line.strip()]
    if not lines:
        return "Unknown error"
    error_line = next((line for line in lines if "Error" in line), lines[-1])
    return error_line[:300]


def write_log(log_file: Path, cmd: list[str], stdout: str | bytes | None, stderr: str | bytes | None) -> str:
    api_key = cmd[cmd.index("--api-key") + 1]

    def as_text(stream: str | bytes | None) -> str:
        if isinstance(stream, bytes):
            stream = stream.decode("utf-8", errors="replace")
        return (stream or "").replace(api_key, "***")

    output = as_text(stdout) + as_text(stderr)
    shown_cmd = " ".join("***" if part == api_key else part for part in cmd)
    log_file.write_text(
        f"$ {shown_cmd}\n\n--- stdout ---\n{as_text(stdout)}\n--- stderr ---\n{as_text(stderr)}",
        encoding="utf-8",
    )
    return output


def translate_language(
    language: str,
    api_key: str,
    batch_size: int,
    timeout: int,
    skip_verification: bool,
    include_existing: bool,
    model: str,
    log_dir: Path,
    keys_file: Path | None = None,
    only_keys: bool = False,
) -> tuple[str, bool, str]:
    """
    Translate a single language.
    Returns: (language_code, success, message)
    """
    safe_print(f"[{language}] Starting translation...")

    cmd = [
        "python3",
        "scripts/translations/auto_translate.py",
        language,
        "--api-key",
        api_key,
        "--batch-size",
        str(batch_size),
        "--timeout",
        str(timeout),
        "--model",
        model,
    ]

    if skip_verification:
        cmd.append("--skip-verification")

    if include_existing:
        cmd.append("--include-existing")

    if keys_file:
        cmd.extend(["--keys-file", str(keys_file)])
        if only_keys:
            cmd.append("--only-keys")

    log_file = log_dir / f"{language}.log"

    try:
        result = subprocess.run(
            cmd,
            capture_output=True,
            text=True,
            timeout=timeout * 5,  # Overall timeout = 5x per-batch timeout
        )
        output = write_log(log_file, cmd, result.stdout, result.stderr)

        if result.returncode == 0:
            # Check if nothing to translate
            if "Nothing to translate!" in result.stdout:
                safe_print(f"[{language}] ✓ Already complete")
                return (language, True, "Already complete")
            safe_print(f"[{language}] ✓ Success")
            return (language, True, "Success")
        else:
            error_msg = find_error_line(output)
            safe_print(f"[{language}] ✗ Failed: {error_msg} (log: {log_file})")
            return (language, False, error_msg)

    except subprocess.TimeoutExpired as e:
        write_log(log_file, cmd, e.stdout, e.stderr)
        safe_print(f"[{language}] ✗ Timeout exceeded (log: {log_file})")
        return (language, False, "Timeout exceeded")
    except Exception as e:
        safe_print(f"[{language}] ✗ Error: {str(e)}")
        return (language, False, str(e))


def main():
    parser = argparse.ArgumentParser(
        description="Bulk auto-translate all languages using OpenAI API",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Translate all languages with 10 parallel threads
  python3 bulk_auto_translate.py --parallel 10

  # Translate only incomplete languages (< 95%)
  python3 bulk_auto_translate.py --parallel 5 --threshold 95

  # Translate specific languages only
  python3 bulk_auto_translate.py --languages de-DE fr-FR es-ES --parallel 3

  # Dry run to see what would be translated
  python3 bulk_auto_translate.py --dry-run

  # Retranslate keys whose en-US text changed since they were translated
  python3 bulk_auto_translate.py --mode changed --parallel 5

  # Missing and changed keys in one pass
  python3 bulk_auto_translate.py --mode all --parallel 5

Note: Requires OPENAI_API_KEY environment variable or --api-key argument.
""",
    )

    parser.add_argument("--api-key", help="OpenAI API key (or set OPENAI_API_KEY env var)")
    parser.add_argument(
        "--stale-report",
        help="Use a stale_translations.py --json report instead of scanning git (e.g. when git can't run here)",
    )
    parser.add_argument(
        "--mode",
        choices=["missing", "changed", "all"],
        default="missing",
        help="missing: keys absent from the language (default); changed: keys whose en-US text "
        "changed since they were translated, found via git history; all: both",
    )
    parser.add_argument(
        "--model",
        default="gpt-5.5",
        help="OpenAI model (default: gpt-5.5; gpt-5.6-sol/terra/luna if your org has 5.6 access)",
    )
    parser.add_argument(
        "--parallel",
        type=int,
        default=1,
        help="Number of parallel translation threads (default: 1)",
    )
    parser.add_argument(
        "--batch-size",
        type=int,
        default=500,
        help="Entries per batch for translation (default: 500)",
    )
    parser.add_argument(
        "--timeout",
        type=int,
        default=600,
        help="Timeout per batch in seconds (default: 600)",
    )
    parser.add_argument(
        "--threshold",
        type=float,
        default=0.0,
        help="Only translate languages below this completion %% (default: 0 = all)",
    )
    parser.add_argument(
        "--languages",
        nargs="+",
        help="Translate only specific languages (e.g., de-DE fr-FR)",
    )
    parser.add_argument(
        "--locales-dir",
        default="frontend/public/locales",
        help="Path to locales directory",
    )
    parser.add_argument(
        "--skip-verification",
        action="store_true",
        help="Skip final completion verification for each language",
    )
    parser.add_argument(
        "--include-existing",
        action="store_true",
        help="Also retranslate existing keys that match English (default: only translate missing keys)",
    )
    parser.add_argument(
        "--log-dir",
        default="translation_logs",
        help="Directory for each language's full pipeline output, one timestamped folder per run "
        "(default: translation_logs)",
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Show what would be translated without actually translating",
    )

    args = parser.parse_args()

    # Verify API key (unless dry run)
    api_key = args.api_key or os.environ.get("OPENAI_API_KEY")
    if not args.dry_run and not api_key:
        print("Error: OpenAI API key required. Provide via --api-key or OPENAI_API_KEY environment variable")
        sys.exit(1)

    locales_dir = Path(args.locales_dir)

    # Get languages to translate
    if args.languages:
        languages = args.languages
        print(f"Translating specified languages: {', '.join(languages)}")
    else:
        languages = get_all_languages(locales_dir)
        print(f"Found {len(languages)} languages (excluding en-US)")

    if not languages:
        print("No languages to translate!")
        sys.exit(0)

    # Filter by completion threshold
    if args.threshold > 0:
        print(f"\nFiltering languages below {args.threshold}% completion...")
        filtered = []
        for lang in languages:
            completion = get_language_completion(locales_dir, lang)
            if completion is None:
                filtered.append(lang)  # Include if can't determine
                print(f"  {lang}: Unknown completion - will translate")
            elif completion < args.threshold:
                filtered.append(lang)
                print(f"  {lang}: {completion:.1f}% - will translate")
            else:
                print(f"  {lang}: {completion:.1f}% - skipping (above threshold)")

        languages = filtered

        if not languages:
            print("\nNo languages below threshold!")
            sys.exit(0)

    stale: dict[str, list[dict]] = {}
    if args.mode != "missing" and args.stale_report:
        print(f"\nLoading changed keys from {args.stale_report}...")
        with open(args.stale_report, encoding="utf-8") as f:
            stale = {lang: entries for lang, entries in json.load(f).items() if lang in languages}
    elif args.mode != "missing":
        print("\nScanning git history for en-US changes not yet reflected in translations...")
        repo = Path(git("rev-parse", "--show-toplevel").strip())
        stale = find_stale(repo, [lang for lang in languages if lang != "en-US"])
    if args.mode != "missing":
        for lang in languages:
            print(f"  {lang}: {len(stale.get(lang, []))} changed")
        if args.mode == "changed":
            languages = [lang for lang in languages if stale.get(lang)]
            if not languages:
                print("\nNo changed translations found!")
                sys.exit(0)

    print(f"\n{'=' * 60}")
    print("Bulk Translation Configuration")
    print(f"{'=' * 60}")
    print(f"Mode: {args.mode}")
    print(f"Languages to translate: {len(languages)}")
    print(f"Model: {args.model}")
    print(f"Parallel threads: {args.parallel}")
    print(f"Batch size: {args.batch_size}")
    print(f"Timeout per batch: {args.timeout}s")
    if args.threshold > 0:
        print(f"Completion threshold: {args.threshold}%")
    print(f"{'=' * 60}\n")

    if args.dry_run:
        print("DRY RUN - Languages that would be translated:")
        for lang in languages:
            completion = get_language_completion(locales_dir, lang)
            comp_str = f"{completion:.1f}%" if completion is not None else "Unknown"
            changed_str = f", {len(stale.get(lang, []))} changed" if args.mode != "missing" else ""
            print(f"  - {lang} ({comp_str}{changed_str})")
        print(f"\nTotal: {len(languages)} languages")
        sys.exit(0)

    start_time = time.time()

    log_root = Path(args.log_dir)
    log_root.mkdir(parents=True, exist_ok=True)
    log_dir = Path(tempfile.mkdtemp(prefix=datetime.now().strftime("%Y%m%d_%H%M%S_"), dir=log_root))
    print(f"Logging each language's output to {log_dir}/\n")

    # Stale key lists are handed to auto_translate.py as files
    keys_dir = Path(tempfile.mkdtemp(prefix="stale_keys_"))
    keys_files: dict[str, Path] = {}
    for lang, entries in stale.items():
        if entries:
            keys_files[lang] = keys_dir / f"{lang}.json"
            keys_files[lang].write_text(json.dumps([e["key"] for e in entries]), encoding="utf-8")

    # Translate in parallel
    results = {"success": [], "failed": [], "already_complete": []}

    with ThreadPoolExecutor(max_workers=args.parallel) as executor:
        futures = {
            executor.submit(
                translate_language,
                lang,
                api_key,
                args.batch_size,
                args.timeout,
                args.skip_verification,
                args.include_existing,
                args.model,
                log_dir,
                keys_files.get(lang),
                args.mode == "changed",
            ): lang
            for lang in languages
        }

        for future in as_completed(futures):
            language, success, message = future.result()

            if success:
                if message == "Already complete":
                    results["already_complete"].append(language)
                else:
                    results["success"].append(language)
            else:
                results["failed"].append((language, message))

    elapsed = time.time() - start_time
    shutil.rmtree(keys_dir, ignore_errors=True)

    # Print summary
    print("\n" + "=" * 60)
    print("Bulk Translation Summary")
    print("=" * 60)
    print(f"Total languages: {len(languages)}")
    print(f"Successful: {len(results['success'])}")
    print(f"Already complete: {len(results['already_complete'])}")
    print(f"Failed: {len(results['failed'])}")
    print(f"Time elapsed: {elapsed:.1f} seconds ({elapsed / 60:.1f} minutes)")
    print("=" * 60)

    if results["success"]:
        print(f"\n✅ Successfully translated ({len(results['success'])}):")
        for lang in sorted(results["success"]):
            print(f"  - {lang}")

    if results["already_complete"]:
        print(f"\n✓ Already complete ({len(results['already_complete'])}):")
        for lang in sorted(results["already_complete"]):
            print(f"  - {lang}")

    if results["failed"]:
        print(f"\n❌ Failed ({len(results['failed'])}):")
        for lang, msg in sorted(results["failed"]):
            print(f"  - {lang}: {msg}")
            print(f"    log: {log_dir / f'{lang}.log'}")
        sys.exit(1)

    print("\n✅ Bulk translation completed successfully!")


if __name__ == "__main__":
    main()