From 76cc34ae38cee9c0b0dc3229db59fb93a7ddd849 Mon Sep 17 00:00:00 2001 From: Serhii Tereshchenko Date: Mon, 14 Oct 2024 11:34:40 +0300 Subject: [PATCH] fix: Fix invalid ngettext usage in `Throttled` exception. Format should be called after ngettext. If you have overridden `extra_detail_singular` or `extra_detail_plural`, you should now replace them with a single `def extra_detail` override. Refs: https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#pluralization --- rest_framework/exceptions.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 09f111102e..cba5757a5e 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -226,8 +226,6 @@ def __init__(self, media_type, detail=None, code=None): class Throttled(APIException): status_code = status.HTTP_429_TOO_MANY_REQUESTS default_detail = _('Request was throttled.') - extra_detail_singular = _('Expected available in {wait} second.') - extra_detail_plural = _('Expected available in {wait} seconds.') default_code = 'throttled' def __init__(self, wait=None, detail=None, code=None): @@ -235,14 +233,17 @@ def __init__(self, wait=None, detail=None, code=None): detail = force_str(self.default_detail) if wait is not None: wait = math.ceil(wait) - detail = ' '.join(( - detail, - force_str(ngettext(self.extra_detail_singular.format(wait=wait), - self.extra_detail_plural.format(wait=wait), - wait)))) + detail = " ".join((detail, force_str(self.extra_detail(wait)))) self.wait = wait super().__init__(detail, code) + def extra_detail(self, wait): + return ngettext( + 'Expected available in {wait} second.', + 'Expected available in {wait} seconds.', + wait, + ).format(wait=wait) + def server_error(request, *args, **kwargs): """