96 lines
2.4 KiB
Python
96 lines
2.4 KiB
Python
#!/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()
|