https://github.com/python/cpython/commit/4d58c8cb247b585051bfbacd79f77aafd18986b7
commit: 4d58c8cb247b585051bfbacd79f77aafd18986b7
branch: main
author: Tomas R. <tomas.ro...@gmail.com>
committer: serhiy-storchaka <storch...@gmail.com>
date: 2025-04-10T14:56:21+03:00
summary:

gh-130320: gettext: Test fallback translations (GH-130321)

files:
M Lib/test/test_gettext.py

diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index d2f18f1dfede77..61bbd0dba43a48 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -748,6 +748,62 @@ def test_cache(self):
         self.assertEqual(t.__class__, DummyGNUTranslations)
 
 
+class FallbackTranslations(gettext.NullTranslations):
+    def gettext(self, message):
+        return f'gettext: {message}'
+
+    def ngettext(self, msgid1, msgid2, n):
+        return f'ngettext: {msgid1}, {msgid2}, {n}'
+
+    def pgettext(self, context, message):
+        return f'pgettext: {context}, {message}'
+
+    def npgettext(self, context, msgid1, msgid2, n):
+        return f'npgettext: {context}, {msgid1}, {msgid2}, {n}'
+
+
+class FallbackTestCase(GettextBaseTest):
+    def test_null_translations_fallback(self):
+        t = gettext.NullTranslations()
+        t.add_fallback(FallbackTranslations())
+        self.assertEqual(t.gettext('foo'), 'gettext: foo')
+        self.assertEqual(t.ngettext('foo', 'foos', 1),
+                         'ngettext: foo, foos, 1')
+        self.assertEqual(t.pgettext('context', 'foo'),
+                         'pgettext: context, foo')
+        self.assertEqual(t.npgettext('context', 'foo', 'foos', 1),
+                         'npgettext: context, foo, foos, 1')
+
+    def test_gnu_translations_fallback(self):
+        with open(MOFILE, 'rb') as fp:
+            t = gettext.GNUTranslations(fp)
+        t.add_fallback(FallbackTranslations())
+        self.assertEqual(t.gettext('foo'), 'gettext: foo')
+        self.assertEqual(t.ngettext('foo', 'foos', 1),
+                         'ngettext: foo, foos, 1')
+        self.assertEqual(t.pgettext('context', 'foo'),
+                         'pgettext: context, foo')
+        self.assertEqual(t.npgettext('context', 'foo', 'foos', 1),
+                         'npgettext: context, foo, foos, 1')
+
+    def test_nested_fallbacks(self):
+        class NestedFallback(gettext.NullTranslations):
+            def gettext(self, message):
+                if message == 'foo':
+                    return 'fallback'
+                return super().gettext(message)
+
+        fallback1 = NestedFallback()
+        fallback2 = FallbackTranslations()
+        t = gettext.NullTranslations()
+        t.add_fallback(fallback1)
+        t.add_fallback(fallback2)
+
+        self.assertEqual(fallback1.gettext('bar'), 'gettext: bar')
+        self.assertEqual(t.gettext('foo'), 'fallback')
+        self.assertEqual(t.gettext('bar'), 'gettext: bar')
+
+
 class ExpandLangTestCase(unittest.TestCase):
     def test_expand_lang(self):
         # Test all combinations of territory, charset and

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to