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
|
|
|
|
|
|
|
|
|
|
|
|
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')]
|
|
|
|
directories.extend(Path(config.path) / 'locale' for config in apps.get_app_configs())
|
|
|
|
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
|
|
|
|
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
|