fix: clear translation cache

- Cache was not getting cleared, since it's user-specific
- Remove unused implementation from gettext
This commit is contained in:
barredterra 2023-12-26 23:17:31 +01:00
parent 3a76fb2253
commit 483fa15c8f
5 changed files with 19 additions and 28 deletions

View file

@ -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:

View file

@ -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()

View file

@ -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("%", "%")

View file

@ -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()

View file

@ -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)]