diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index c257812452..bd6813ad06 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -94,6 +94,7 @@ def clear_cache(context): "Clear cache, doctype cache and defaults" import frappe.sessions from frappe.desk.notifications import clear_notifications + from frappe.translate import clear_cache as clear_translations from frappe.website.utils import clear_website_cache for site in context.sites: @@ -102,6 +103,7 @@ def clear_cache(context): frappe.clear_cache() clear_notifications() clear_website_cache() + clear_translations() finally: frappe.destroy() if not context.sites: diff --git a/frappe/core/doctype/translation/test_translation.py b/frappe/core/doctype/translation/test_translation.py index e2d028fbd4..98e8503c83 100644 --- a/frappe/core/doctype/translation/test_translation.py +++ b/frappe/core/doctype/translation/test_translation.py @@ -11,10 +11,7 @@ class TestTranslation(FrappeTestCase): def tearDown(self): frappe.local.lang = "en" - if frappe.conf.use_gettext: - from frappe.gettext.translate import clear_cache - else: - from frappe.translate import clear_cache + from frappe.translate import clear_cache clear_cache() diff --git a/frappe/gettext/translate.py b/frappe/gettext/translate.py index e81a257733..f2c4876738 100644 --- a/frappe/gettext/translate.py +++ b/frappe/gettext/translate.py @@ -16,9 +16,6 @@ from frappe.utils import get_bench_path DEFAULT_LANG = "en" PO_DIR = "locale" # po and pot files go into [app]/locale POT_FILE = "main.pot" # the app's pot file is always main.pot -MERGED_TRANSLATION_KEY = "gettext_merged_translations" -APP_TRANSLATION_KEY = "gettext_app_translations" -USER_TRANSLATION_KEY = "gettext_user_translations" def new_catalog(app: str, locale: str | None = None) -> Catalog: @@ -277,18 +274,5 @@ def get_translations_from_mo(lang, app): return translations -def clear_cache(): - """Clear all translation assets from :meth:`frappe.cache`""" - cache = frappe.cache() - cache.delete_key("langinfo") - - # clear translations saved in boot cache - cache.delete_key("bootinfo") - cache.delete_key("translation_assets", shared=True) - cache.delete_key(APP_TRANSLATION_KEY, shared=True) - cache.delete_key(USER_TRANSLATION_KEY) - cache.delete_key(MERGED_TRANSLATION_KEY) - - def escape_percent(s: str): return s.replace("%", "%") diff --git a/frappe/tests/test_translate.py b/frappe/tests/test_translate.py index 656ed375ea..54e51ab0d5 100644 --- a/frappe/tests/test_translate.py +++ b/frappe/tests/test_translate.py @@ -10,6 +10,8 @@ import frappe.translate from frappe import _ from frappe.tests.utils import FrappeTestCase from frappe.translate import ( + MERGED_TRANSLATION_KEY, + USER_TRANSLATION_KEY, clear_cache, extract_javascript, extract_messages_from_javascript_code, @@ -45,6 +47,17 @@ class TestTranslate(FrappeTestCase): if self._testMethodName in self.guest_sessions_required: frappe.set_user("Administrator") + def test_clear_cache(self): + _("Trigger caching") + + self.assertIsNotNone(frappe.cache.hget(USER_TRANSLATION_KEY, frappe.local.lang)) + self.assertIsNotNone(frappe.cache.hget(MERGED_TRANSLATION_KEY, frappe.local.lang)) + + clear_cache() + + self.assertIsNone(frappe.cache.hget(USER_TRANSLATION_KEY, frappe.local.lang)) + self.assertIsNone(frappe.cache.hget(MERGED_TRANSLATION_KEY, frappe.local.lang)) + def test_extract_message_from_file(self): data = frappe.translate.get_messages_from_file(translation_string_file) bench_path = get_bench_path() diff --git a/frappe/translate.py b/frappe/translate.py index f974d6d2bb..f1137e953d 100644 --- a/frappe/translate.py +++ b/frappe/translate.py @@ -264,13 +264,9 @@ def get_user_translations(lang): def clear_cache(): """Clear all translation assets from :meth:`frappe.cache`""" - frappe.cache.delete_key("langinfo") - - # clear translations saved in boot cache - frappe.cache.delete_key("bootinfo") - frappe.cache.delete_key("translation_assets") - frappe.cache.delete_key(USER_TRANSLATION_KEY) - frappe.cache.delete_key(MERGED_TRANSLATION_KEY) + frappe.cache.delete_value( + keys=["bootinfo", USER_TRANSLATION_KEY, MERGED_TRANSLATION_KEY], + ) def get_messages_for_app(app, deduplicate=True): @@ -1019,7 +1015,6 @@ def import_translations(lang, path): def migrate_translations(source_app, target_app): """Migrate target-app-specific translations from source-app to target-app""" - clear_cache() strings_in_source_app = [m[1] for m in frappe.translate.get_messages_for_app(source_app)] strings_in_target_app = [m[1] for m in frappe.translate.get_messages_for_app(target_app)]