po-from-transifex.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. import glob
  3. import subprocess
  4. from pathlib import Path
  5. from translation_utils import get_translations, init_sync_dir
  6. RESOURCE_NAME = "mc.pot"
  7. SCRIPT_DIR = Path(__file__).parent
  8. SOURCE_DIR = SCRIPT_DIR.parent.parent.parent
  9. PO_DIR = SOURCE_DIR / "po"
  10. def strip_message_locations(work_dir: Path):
  11. for po_file in (work_dir / filename for filename in glob.glob("*.po", root_dir=work_dir)):
  12. po_file.write_text(
  13. "".join(line for line in po_file.read_text().splitlines(keepends=True) if not line.startswith("#:"))
  14. )
  15. def copy_translations_to_source_dir(source_dir: Path, target_dir: Path):
  16. for po_file in (source_dir / filename for filename in glob.glob("*.po", root_dir=source_dir)):
  17. (target_dir / po_file.name).write_text(po_file.read_text())
  18. def update_linguas(po_dir: Path):
  19. translations = get_translations(po_dir)
  20. (po_dir / "LINGUAS").write_text("# List of available translations\n" + "\n".join(translations) + "\n")
  21. sync_dir = init_sync_dir(SCRIPT_DIR, RESOURCE_NAME)
  22. subprocess.run(("tx", "pull", "--all", "--force"), cwd=sync_dir, check=True)
  23. strip_message_locations(sync_dir)
  24. copy_translations_to_source_dir(sync_dir, PO_DIR)
  25. update_linguas(PO_DIR)