reloader.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from pathlib import Path
  2. from asgiref.local import Local
  3. from django.apps import apps
  4. from django.utils.autoreload import is_django_module
  5. def watch_for_translation_changes(sender, **kwargs):
  6. """Register file watchers for .mo files in potential locale paths."""
  7. from django.conf import settings
  8. if settings.USE_I18N:
  9. directories = [Path('locale')]
  10. directories.extend(
  11. Path(config.path) / 'locale'
  12. for config in apps.get_app_configs()
  13. if not is_django_module(config.module)
  14. )
  15. directories.extend(Path(p) for p in settings.LOCALE_PATHS)
  16. for path in directories:
  17. sender.watch_dir(path, '**/*.mo')
  18. def translation_file_changed(sender, file_path, **kwargs):
  19. """Clear the internal translations cache if a .mo file is modified."""
  20. if file_path.suffix == '.mo':
  21. import gettext
  22. from django.utils.translation import trans_real
  23. gettext._translations = {}
  24. trans_real._translations = {}
  25. trans_real._default = None
  26. trans_real._active = Local()
  27. return True