]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Make pgettext search plurals when translation is not found (#1085)
authorTomas R <tomas.roun8@gmail.com>
Thu, 11 Jul 2024 06:11:04 +0000 (08:11 +0200)
committerGitHub <noreply@github.com>
Thu, 11 Jul 2024 06:11:04 +0000 (09:11 +0300)
pgettext can now find the following translation
when using `pgettext("ctx", "foo")`:
```
msgctxt "ctx"
msgid "foo"
msgid_plural "foos"
msgstr[0] "foo translated"
```

The upstream CPython PR is  https://github.com/python/cpython/pull/107118

babel/support.py
tests/test_support.py

index e6ee3780515bd4f6e30db8cde8fc08dfa79d2a6a..7dcda5c6313e5822cfc71b7b9102c8058aecef02 100644 (file)
@@ -466,10 +466,12 @@ class NullTranslations(gettext.NullTranslations):
         missing = object()
         tmsg = self._catalog.get(ctxt_msg_id, missing)
         if tmsg is missing:
-            if self._fallback:
-                return self._fallback.pgettext(context, message)
-            return message
-        return tmsg
+            tmsg = self._catalog.get((ctxt_msg_id, self.plural(1)), missing)
+        if tmsg is not missing:
+            return tmsg
+        if self._fallback:
+            return self._fallback.pgettext(context, message)
+        return message
 
     def lpgettext(self, context: str, message: str) -> str | bytes | object:
         """Equivalent to ``pgettext()``, but the translation is returned in the
index 12627205177aa430c6e61752054172c6fd17107e..366c29412efe5b53bbd70cc4a30e4edbf73feed3 100644 (file)
@@ -71,6 +71,14 @@ class TranslationsTestCase(unittest.TestCase):
         self.assertEqualTypeToo('Voh', self.translations.gettext('foo'))
         self.assertEqualTypeToo('VohCTX', self.translations.pgettext('foo',
                                                                      'foo'))
+        self.assertEqualTypeToo('VohCTX1', self.translations.pgettext('foo',
+                                                                      'foo1'))
+
+    def test_pgettext_fallback(self):
+        fallback = self.translations._fallback
+        self.translations._fallback = support.NullTranslations()
+        assert self.translations.pgettext('foo', 'bar') == 'bar'
+        self.translations._fallback = fallback
 
     def test_upgettext(self):
         self.assertEqualTypeToo('Voh', self.translations.ugettext('foo'))