rusackas commented on code in PR #41780:
URL: https://github.com/apache/superset/pull/41780#discussion_r3685673734
##########
superset/translations/utils.py:
##########
@@ -24,9 +25,43 @@
# Global caching for JSON language packs
ALL_LANGUAGE_PACKS: dict[str, dict[str, Any]] = {"en": {}}
+# Global caching for language pack content hashes, used to build
+# content-addressed (cache-busting) asset URLs. A locale with no pack file
+# is cached as None so repeated requests don't re-stat the filesystem or
+# re-log the same warning.
+ALL_LANGUAGE_PACK_VERSIONS: dict[str, Optional[str]] = {}
+
DIR = os.path.dirname(os.path.abspath(__file__))
+def get_language_pack_filename(locale: str) -> str:
+ """Resolve the on-disk JSON pack for a locale (empty pack for English)"""
+ if not locale or locale == "en":
+ # Forcing a dummy, quasi-empty language pack for English since the file
+ # in the en directory contains data with empty mappings
+ return DIR + "/empty_language_pack.json"
+ return DIR + f"/{locale}/LC_MESSAGES/messages.json"
+
+
+def get_language_pack_version(locale: str) -> Optional[str]:
+ """Get/cache a short content hash of the language pack file
+
+ The hash is embedded in the pack's asset URL so browsers can cache it
+ as immutable and pick up a fresh copy whenever translations change.
+ Returns None when the pack file cannot be read.
+ """
+ if locale in ALL_LANGUAGE_PACK_VERSIONS:
+ return ALL_LANGUAGE_PACK_VERSIONS[locale]
+ try:
+ with open(get_language_pack_filename(locale), "rb") as f:
+ version = hashlib.sha256(f.read()).hexdigest()[:12]
+ except OSError:
+ logger.warning("No language pack file to version for locale %s",
locale)
+ version = None
+ ALL_LANGUAGE_PACK_VERSIONS[locale] = version
+ return version
Review Comment:
Same tradeoff as any content-hashed static asset in this app, the bundles
get the same treatment via the webpack manifest. Translation catalogs are build
artifacts that ship with a release, they don't change under a running worker
without a redeploy, and a redeploy restarts the process and clears this cache
along with it. I don't think we need runtime invalidation here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]