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