update_translations.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. import argparse
  3. import json
  4. import os
  5. from common.basedir import BASEDIR
  6. UI_DIR = os.path.join(BASEDIR, "selfdrive", "ui")
  7. TRANSLATIONS_DIR = os.path.join(UI_DIR, "translations")
  8. LANGUAGES_FILE = os.path.join(TRANSLATIONS_DIR, "languages.json")
  9. def update_translations(vanish=False, plural_only=None, translations_dir=TRANSLATIONS_DIR):
  10. if plural_only is None:
  11. plural_only = []
  12. with open(LANGUAGES_FILE, "r") as f:
  13. translation_files = json.load(f)
  14. for file in translation_files.values():
  15. tr_file = os.path.join(translations_dir, f"{file}.ts")
  16. args = f"lupdate -locations none -recursive {UI_DIR} -ts {tr_file}"
  17. if vanish:
  18. args += " -no-obsolete"
  19. if file in plural_only:
  20. args += " -pluralonly"
  21. ret = os.system(args)
  22. assert ret == 0
  23. if __name__ == "__main__":
  24. parser = argparse.ArgumentParser(description="Update translation files for UI",
  25. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  26. parser.add_argument("--vanish", action="store_true", help="Remove translations with source text no longer found")
  27. parser.add_argument("--plural-only", type=str, nargs="*", default=["main_en"], help="Translation codes to only create plural translations for (ie. the base language)")
  28. args = parser.parse_args()
  29. update_translations(args.vanish, args.plural_only)