jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1332467?usp=email )

Change subject: i18n: Clear bundle cache when switching packages
......................................................................

i18n: Clear bundle cache when switching packages

Bundle lookups are cached by language and directory but load data from the
global messages package. Invalidate the cache when changing packages to
prevent stale translations from the previous source.

Change-Id: I852728fe1fd512c1f8034508a44e1abae3ccc938
---
M pywikibot/i18n.py
M tests/i18n_tests.py
2 files changed, 12 insertions(+), 0 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 7cfd82e..b8e7f90 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -359,6 +359,7 @@
     global _messages_available
     _messages_package_name = package_name
     _messages_available = None
+    _get_bundle.cache_clear()


 def messages_available() -> bool:
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 9d9871f..d09e47f 100755
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -274,6 +274,17 @@
     net = False
     message_package = 'tests.i18n'

+    def testSwitchMessagesPackage(self) -> None:
+        """Test that switching packages clears cached translations."""
+        self.addCleanup(i18n._get_bundle.cache_clear)
+        i18n._get_bundle.cache_clear()
+        self.assertEqual(i18n.twtranslate('en', 'test-localized'),
+                         'test-localized EN')
+
+        i18n.set_messages_package('pywikibot.scripts.i18n')
+        with self.assertRaises(TranslationError):
+            i18n.twtranslate('en', 'test-localized', fallback=False)
+
     def testLocalized(self) -> None:
         """Test fully localized entry."""
         self.assertEqual(i18n.twtranslate('en', 'test-localized'),

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1332467?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I852728fe1fd512c1f8034508a44e1abae3ccc938
Gerrit-Change-Number: 1332467
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to