aboutsummaryrefslogtreecommitdiffstats
path: root/manage_translations.py
blob: ce1e1606b7b867b2cd3bafd6d10dcb5664fbbfe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
"""
Translation management script for yt-local

Usage:
    python manage_translations.py extract  # Extract strings to messages.pot
    python manage_translations.py init es  # Initialize Spanish translation
    python manage_translations.py update   # Update all translations
    python manage_translations.py compile  # Compile translations to .mo files
"""
import sys
import os
import subprocess


def run_command(cmd):
    """Run a shell command and print output"""
    print(f"Running: {' '.join(cmd)}")
    result = subprocess.run(cmd, capture_output=True, text=True)
    if result.stdout:
        print(result.stdout)
    if result.stderr:
        print(result.stderr, file=sys.stderr)
    return result.returncode


def extract():
    """Extract translatable strings from source code"""
    print("Extracting translatable strings...")
    return run_command([
        'pybabel', 'extract',
        '-F', 'babel.cfg',
        '-k', 'lazy_gettext',
        '-k', '_l',
        '-o', 'translations/messages.pot',
        '.'
    ])


def init(language):
    """Initialize a new language translation"""
    print(f"Initializing {language} translation...")
    return run_command([
        'pybabel', 'init',
        '-i', 'translations/messages.pot',
        '-d', 'translations',
        '-l', language
    ])


def update():
    """Update existing translations with new strings"""
    print("Updating translations...")
    return run_command([
        'pybabel', 'update',
        '-i', 'translations/messages.pot',
        '-d', 'translations'
    ])


def compile_translations():
    """Compile .po files to .mo files"""
    print("Compiling translations...")
    return run_command([
        'pybabel', 'compile',
        '-d', 'translations'
    ])


def main():
    if len(sys.argv) < 2:
        print(__doc__)
        sys.exit(1)

    command = sys.argv[1]

    if command == 'extract':
        sys.exit(extract())
    elif command == 'init':
        if len(sys.argv) < 3:
            print("Error: Please specify a language code (e.g., es, fr, de)")
            sys.exit(1)
        sys.exit(init(sys.argv[2]))
    elif command == 'update':
        sys.exit(update())
    elif command == 'compile':
        sys.exit(compile_translations())
    else:
        print(f"Unknown command: {command}")
        print(__doc__)
        sys.exit(1)


if __name__ == '__main__':
    main()