Created
July 9, 2025 07:55
-
-
Save SebCorbin/91d75c1eff53464ad378ec52fa8459f1 to your computer and use it in GitHub Desktop.
Clean translations on commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| repos: | |
| - repo: local | |
| hooks: | |
| - id: unclean-translations-files | |
| name: Check for unclean translations files | |
| entry: bash -c '(cd src && ./manage.py makemessages && ./manage.py check_translations_files)' | |
| pass_filenames: false | |
| language: system | |
| types_or: [ html, python ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pathlib import Path | |
| import polib | |
| from django.core.management import BaseCommand | |
| class Command(BaseCommand): | |
| @staticmethod | |
| def find_po_files(): | |
| base_path = Path("locale") | |
| return list(base_path.glob("*/LC_MESSAGES/django.po")) | |
| @staticmethod | |
| def check_po_file(filename): | |
| po = polib.pofile(filename) | |
| errors = {} | |
| if entries := po.fuzzy_entries(): | |
| errors["fuzzy"] = entries | |
| if entries := po.obsolete_entries(): | |
| errors["obsolete"] = entries | |
| if "fr" in Path(filename).parts and (entries := po.untranslated_entries()): | |
| errors["untranslated"] = entries | |
| return errors | |
| def handle(self, *args, **options): | |
| po_files = self.find_po_files() | |
| fail = False | |
| for po_file in po_files: | |
| errors = self.check_po_file(po_file) | |
| if errors: | |
| self.stdout.write(f"\nIn {po_file}:") | |
| for err_type, err_items in errors.items(): | |
| for err_item in err_items: | |
| self.stdout.write(f"[{err_type}] {po_file}:{err_item.linenum}") | |
| fail = True | |
| if fail: | |
| raise SystemExit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment