2019-01-14 09:33:47 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
from asgiref.local import Local
|
|
|
|
|
2019-01-14 09:33:47 +08:00
|
|
|
from django.apps import apps
|
2020-11-05 19:18:20 +08:00
|
|
|
from django.utils.autoreload import is_django_module
|
2020-04-19 04:32:19 +08:00
|
|
|
|
|
|
|
|
2019-01-14 09:33:47 +08:00
|
|
|
def watch_for_translation_changes(sender, **kwargs):
|
|
|
|
"""Register file watchers for .mo files in potential locale paths."""
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
if settings.USE_I18N:
|
|
|
|
directories = [Path('locale')]
|
2020-04-19 04:32:19 +08:00
|
|
|
directories.extend(
|
|
|
|
Path(config.path) / 'locale'
|
|
|
|
for config in apps.get_app_configs()
|
2020-11-05 19:18:20 +08:00
|
|
|
if not is_django_module(config.module)
|
2020-04-19 04:32:19 +08:00
|
|
|
)
|
2019-01-14 09:33:47 +08:00
|
|
|
directories.extend(Path(p) for p in settings.LOCALE_PATHS)
|
|
|
|
for path in directories:
|
2019-07-22 05:28:39 +08:00
|
|
|
sender.watch_dir(path, '**/*.mo')
|
2019-01-14 09:33:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def translation_file_changed(sender, file_path, **kwargs):
|
|
|
|
"""Clear the internal translations cache if a .mo file is modified."""
|
|
|
|
if file_path.suffix == '.mo':
|
|
|
|
import gettext
|
2020-07-24 14:25:47 +08:00
|
|
|
|
2019-01-14 09:33:47 +08:00
|
|
|
from django.utils.translation import trans_real
|
|
|
|
gettext._translations = {}
|
|
|
|
trans_real._translations = {}
|
|
|
|
trans_real._default = None
|
2019-04-12 21:15:18 +08:00
|
|
|
trans_real._active = Local()
|
2019-01-14 09:33:47 +08:00
|
|
|
return True
|