translation_utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import glob
  2. from pathlib import Path
  3. def get_config_file(root_dir: Path, resource: str, name: str) -> Path:
  4. return root_dir / "config.d" / resource / name
  5. def init_sync_dir(root_dir: Path, resource: str) -> Path:
  6. tx_dir = root_dir / "var.d" / resource / ".tx"
  7. tx_dir.mkdir(parents=True, exist_ok=True)
  8. (tx_dir / "config").write_text(get_config_file(root_dir, resource, "tx.config").read_text())
  9. return tx_dir.parent
  10. def create_po4a_config(sync_dir: Path, script_dir: Path, source_dir: Path, resource: str) -> Path:
  11. langs = get_translations(sync_dir)
  12. config = get_config_file(script_dir, resource, "po4a.cfg").read_text()
  13. config = config.replace("@translations@", " ".join(f"{lang}:var.d/$master/{lang}.po" for lang in langs))
  14. config = config.replace("@resources@", " ".join(f"{lang}:@srcdir@/doc/hints/l10n/mc.hint.{lang}" for lang in langs))
  15. config = config.replace("@srcdir@", str(source_dir))
  16. config_path = sync_dir / "po4a.cfg"
  17. config_path.write_text(config)
  18. return config_path
  19. def get_translations(root_dir: Path) -> list[str]:
  20. return sorted(Path(filename).name.removesuffix(".po") for filename in glob.glob("*.po", root_dir=root_dir))