[3.13] gh-130320: gettext: Test fallback translations (GH-130321) (GH-132359)

(cherry picked from commit 4d58c8cb24)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-04-10 14:20:36 +02:00 committed by GitHub
parent fc85256f89
commit 6c8d527937
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -797,6 +797,62 @@ class GettextCacheTestCase(GettextBaseTest):
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