reloader.py 974 B

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